permaculture playing cards*
The moose likes Swing / AWT / SWT and the fly likes mouselistener for Scrollbar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "mouselistener for Scrollbar" Watch "mouselistener for Scrollbar" New topic
Author

mouselistener for Scrollbar

deepak yadav
Greenhorn

Joined: Oct 04, 2001
Posts: 17
i made a class which implements MouseListener , in that class i created few Scrollbars , & added mouseListener to them , my problem is when i perform some action, like dragging or clicking the ends on Scrollbar only mousePressed method is called , why other methods like mouseReleased not called ?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Could you post some code so we can take a look at it? That would make it easier to debug.

------------------
Happy Coding,
Gregg Bolinger


GenRocket - Experts at Building Test Data
deepak yadav
Greenhorn

Joined: Oct 04, 2001
Posts: 17
The whole code is as follows
//---------- start code -------
import java.awt.*;
import java.awt.event.*;
public class FrameExample implements MouseListener {
private Frame f;
private Panel p1,p2;
private Label l1, l2, l3;
private Scrollbar s1, s2, s3;
private TextField t1, t2, t3;
private Button b1;

public FrameExample() {
f = new Frame("Hello Out There!");
p1 = new Panel(new GridLayout(3,3));
p2 = new Panel();

l1 = new Label("Red");
l1.setBackground(Color.red);
p1.add(l1);

l2 = new Label("Blue");
l2.setBackground(Color.blue);
p1.add(l2);

l3 = new Label("Green");
l3.setBackground(Color.green);
p1.add(l3);

s1 = new Scrollbar(Scrollbar.HORIZONTAL, 0, 50, 0, 255);
s1.addMouseListener(this);
p1.add(s1);

s2 = new Scrollbar(Scrollbar.HORIZONTAL, 0, 50, 0, 255);
s2.addMouseListener(this);
p1.add(s2);
s3 = new Scrollbar(Scrollbar.HORIZONTAL, 0, 50, 0, 255);
s3.addMouseListener(this);
p1.add(s3);

t1 = new TextField(0);
p1.add(t1);

t2 = new TextField(0);
p1.add(t2);

t3 = new TextField(0);
p1.add(t3);


f.add(p1, BorderLayout.NORTH);
p2. setBackground(new Color(0, 0, 0));
b1 = new Button("Close");
b1.addMouseListener(this);
f.add(b1, BorderLayout.SOUTH);
f.add(p2);
}

public void launchFrame() {
f.setSize(170, 170);
f.setBackground (Color.blue);
f.setVisible(true);
}


public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) {
Object obj = e.getSource();
System.out.println(obj);
if (obj instanceof Scrollbar ) {
if (obj == s1) {
t1.setText("" + s1.getValue());
}
else
if (obj == s2) {
t2.setText("" + s2.getValue());
}
else
if (obj == s3) {
t3.setText("" + s3.getValue());
}

p2.setBackground(new Color(s1.getValue(), s2.getValue(), s3.getValue()));
f.repaint();
}
else
if (obj instanceof Button ) {
f.dispose();
System.exit(0);
}


}
public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }



public static void main(String args[]) {
FrameExample guiWindow = new FrameExample();
guiWindow.launchFrame();
}
}
//---------- end code ---------
if i move everything written in mouseReleased method to mousePressed method things start working . i am using jdk1.3.1_01 on Win NT
regards
_______
deepak

Originally posted by Gregg Bolinger:
Could you post some code so we can take a look at it? That would make it easier to debug.

deepak yadav
Greenhorn

Joined: Oct 04, 2001
Posts: 17
hi
i posted a reply but it didn't appeared even after 7 hrs so posting again
the code goes here
// ---------- start of code -----------
import java.awt.*;
import java.awt.event.*;
public class FrameExample implements MouseListener {
private Frame f;
private Panel p1,p2;
private Label l1, l2, l3;
private Scrollbar s1, s2, s3;
private TextField t1, t2, t3;
private Button b1;

public FrameExample() {
f = new Frame("Hello Out There!");
p1 = new Panel(new GridLayout(3,3));
p2 = new Panel();

l1 = new Label("Red");
l1.setBackground(Color.red);
p1.add(l1);

l2 = new Label("Blue");
l2.setBackground(Color.blue);
p1.add(l2);

l3 = new Label("Green");
l3.setBackground(Color.green);
p1.add(l3);

s1 = new Scrollbar(Scrollbar.HORIZONTAL, 0, 50, 0, 255);
s1.addMouseListener(this);
p1.add(s1);

s2 = new Scrollbar(Scrollbar.HORIZONTAL, 0, 50, 0, 255);
s2.addMouseListener(this);
p1.add(s2);
s3 = new Scrollbar(Scrollbar.HORIZONTAL, 0, 50, 0, 255);
s3.addMouseListener(this);
p1.add(s3);

t1 = new TextField(0);
p1.add(t1);

t2 = new TextField(0);
p1.add(t2);

t3 = new TextField(0);
p1.add(t3);


f.add(p1, BorderLayout.NORTH);
p2. setBackground(new Color(0, 0, 0));
b1 = new Button("Close");
b1.addMouseListener(this);
f.add(b1, BorderLayout.SOUTH);
f.add(p2);
}

public void launchFrame() {
f.setSize(170, 170);
f.setBackground (Color.blue);
f.setVisible(true);
}


public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) {
Object obj = e.getSource();
System.out.println(obj);
if (obj instanceof Scrollbar ) {
if (obj == s1) {
t1.setText("" + s1.getValue());
}
else
if (obj == s2) {
t2.setText("" + s2.getValue());
}
else
if (obj == s3) {
t3.setText("" + s3.getValue());
}

p2.setBackground(new Color(s1.getValue(), s2.getValue(), s3.getValue()));
f.repaint();
}
else
if (obj instanceof Button ) {
f.dispose();
System.exit(0);
}


}
public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }



public static void main(String args[]) {
FrameExample guiWindow = new FrameExample();
guiWindow.launchFrame();
}
}
// ----------- end of code ------------
if the code written in mouseReleased method is moved to mousePressed method , things work fine. i am using jdk1.3.1_01 on win NT
_______
deepak
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: mouselistener for Scrollbar