Scrollbar code - Need help

V

Vasu

Hi !

I've coded the following, which represents two scrollbar horizontal
and vertical, and two strings s and s1 values each 50 represented as
one on top of the other where s represents the position of
selectionpoint of scrollbar horizontal and s1 represents position of
vertical one's.

Now the problem is that even if I change the situation of
selectionpoint of any scrollbar Vertical or Horizontal, only string s
changes but not the s1, whereas principly s should change for
Horizontal and s1 should change for Vertical, which is not happening.

Can anybody explain the required change in the code.

Will be grateful to him/her.

Thanks
Vasu
"
import java.awt.*;
import java.applet.*;
public class ScrllApplet extends Applet
{
Scrollbar scrollbar1;
Scrollbar scrollbar2;
String s, s1;

public void init()
{
BorderLayout layout = new BorderLayout();
setLayout(layout);
scrollbar1 = new Scrollbar(Scrollbar.HORIZONTAL,50, 0, 1,100);
scrollbar2 = new Scrollbar(Scrollbar.VERTICAL, 50, 0, 1,100);
add("North", scrollbar1);
add("East", scrollbar2);
s = "50";
s1= "50";

Font font = new Font("TimesRoman", Font.BOLD, 72);
setFont(font);
resize(200, 200);
}
public void paint(Graphics g)
{
g.drawString(s, 60, 120);
g.drawString(s1, 60, 190);
}
public boolean handleEvent(Event evt)
{
if (evt.target instanceof Scrollbar)
{
scrollbar1 = (Scrollbar)evt.target;
int value = scrollbar1.getValue();
s = String.valueOf(value);
repaint();
return true;
}
else if (evt.target instanceof Scrollbar)
{
scrollbar2 = (Scrollbar)evt.target;
int value1 = scrollbar2.getValue();
s1 = String.valueOf(value1);
repaint();
return true;
}
else
{
boolean result = super.handleEvent(evt);
return result;
}
}
}
"
 
V

Vasu

Why AWT in this day and age?  (You will probably get more help
with Swing!)

Hi Andrew!

It may sound you bizarre but one has to learn step-by-step. However,
its not the question of AWT or Swing, it's the question of a code
which is not producing desired result. I hope you will help me out
pointing where I'm wrong in coding.

Thanks anyway for your taking time.
Vasu
 
F

Fred

Hi !

I've coded the following, which represents two scrollbar horizontal
and vertical, and two strings s and s1 values each 50 represented as
one on top of the other where s represents the position of
selectionpoint of scrollbar horizontal and s1 represents position of
vertical one's.

Now the problem is that even if I change the situation of
selectionpoint of any scrollbar Vertical or Horizontal, only string s
changes but not the s1, whereas principly s should change for
Horizontal and s1 should change for Vertical, which is not happening.

Can anybody explain the required change in the code.

Will be grateful to him/her.

Thanks
Vasu
"
import java.awt.*;
import java.applet.*;
public class ScrllApplet extends Applet
{
        Scrollbar scrollbar1;
        Scrollbar scrollbar2;
        String s, s1;

public void init()
{
        BorderLayout layout = new BorderLayout();
        setLayout(layout);
        scrollbar1 = new Scrollbar(Scrollbar.HORIZONTAL,50, 0, 1,100);
        scrollbar2 = new Scrollbar(Scrollbar.VERTICAL,  50, 0, 1,100);
        add("North", scrollbar1);
        add("East",  scrollbar2);
        s = "50";
        s1= "50";

               Font font = new Font("TimesRoman", Font.BOLD, 72);
               setFont(font);
              resize(200, 200);}

public void paint(Graphics g)
{
        g.drawString(s, 60, 120);
        g.drawString(s1, 60, 190);}

public boolean handleEvent(Event evt)
{
        if (evt.target instanceof Scrollbar)
        {
                scrollbar1 = (Scrollbar)evt.target;
                int value = scrollbar1.getValue();
                s = String.valueOf(value);
        repaint();
                return true;
        }
        else if (evt.target instanceof Scrollbar)

This "else" clause will never be executed -
it is identical to the "if" clause.
Both scrollbars will invoke the "if" clause.
 
A

Andrew Thompson

....
It may sound you bizarre but one has to learn step-by-step.

That is quite logical, however..
..However,
its not the question of AWT or Swing, it's the question of a code
which is not producing desired result.

...these types of problems are usually best sorted out
while you are still coding command line apps., noting
that the code problem (identified by the other person
that replied) was a pure logic problem, rather than a
problem with a GUI, as such.

And in terms of step-by-step.

Most people would not consider AWT a logical step towards
most things of worth. True, AWT underlies Swing and an
understanding of a number of AWT (non-Component) classes
and layouts is helpful, but most people who look to code
rich client Java apps. generally go directly to using
Swing (where you have things like split panes, tables,
trees and HTML rendering).

OTOH if your interest is mostly in getting work in Java,
avoid rich client programming altogether and concentrate on
the non-GUI J2SE classes, and J2EE. Most of Java programming
(about 95%) has gone to the server-side.
.. I hope you will help me out
pointing where I'm wrong in coding.

I suspect the other reply has already covered that.

Sorry, when I sent that first reply I was not at a machine
where I could run the code. In front of me in an IDE, I
might have been able to see what was wrong.
Thanks anyway for your taking time.

No worries. Best of luck with your learning, but take a tip
from me, don't spend too much time learning AWT.
 
L

Lew

Andrew said:
No worries. Best of luck with your learning, but take a tip
from me, don't spend too much time learning AWT.

More specifically, the AWT visual components, like Frame as opposed to Swing's
JFrame. Other parts of the AWT library are still quite viable, e.g.,
java.awt.event.ActionListener and its implementing classes. Of course, many
of those are Swing classes themselves. In fact, Swing components inherit from
AWT components, so really when you're using Swing you are using part of AWT,
just the more modern part.

But that's just being lawyerly. Colloquially, when someone speaks of "using
Swing" vs. "using AWT", they're talking about the replacement components that
Swing provides in preference to cognate (and usually ancestral) AWT
components. Under that interpretation, Andrew's advice is the way to go.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top