Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Desktop test 1

Status
Not open for further replies.

borbjo

Programmer
Mar 29, 2002
33
0
0
NO
Check out the code below. Save it as DesktopTest.java, compile & run - it's pretty straightforward.

It's a desktop with 3 internal frames. One contains a JButton, while two of them contains a custom JComponent that is a black square which turns blue when the mouse is over it.

The problem is - mouseClicked() isn't caught on the custom component if the JInternalFrame doesn't have focus - but the button does! .. also - mouseEntered() / mouseExited() doesn't care about the focus ..

Can anyone explain this to me? Or maybe show me what I need to do to make the 'squares' respond on first-click ?

Here goes:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DesktopTest extends JFrame {

public DesktopTest() {
setSize(600,600);

JDesktopPane desktop = new JDesktopPane();

JInternalFrame frame1 = new JInternalFrame();
frame1.setSize(200,200);
frame1.setLocation(100,100);
frame1.setVisible(true);
frame1.getContentPane().add(new SquarePanel("Square 1"));

JInternalFrame frame2 = new JInternalFrame();
frame2.setSize(200,200);
frame2.setLocation(300,400);
frame2.setVisible(true);
JButton button = new JButton("Click me!");
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked!");
}
});
frame2.getContentPane().add(button);


JInternalFrame frame3 = new JInternalFrame();
frame3.setSize(200,200);
frame3.setLocation(200,300);

frame3.getContentPane().add(new SquarePanel("Square 2"));
frame3.setVisible(true);

desktop.add(frame1);
desktop.add(frame2);
desktop.add(frame3);

Container c = this.getContentPane();
c.add(desktop);
}

class SquarePanel extends JPanel implements MouseListener {
private OneSquare one;

public SquarePanel(String name) {
one = new OneSquare(name);
one.addMouseListener(this);
this.add(one);
}

public void mouseClicked(MouseEvent e) {
System.out.println(one + " clicked!");
}

public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {
one.setMouseOver(true);
one.repaint();
}
public void mouseExited(MouseEvent e) {
one.setMouseOver(false);
one.repaint();
}
}

class OneSquare extends JComponent {
private String name;
private boolean mouseover = false;
public OneSquare(String name) {
this.name = name;
setPreferredSize(new Dimension(100,100));
}

public void setMouseOver(boolean mouseover) {
this.mouseover = mouseover;
}

public String toString() { return name; }

public void paintComponent(Graphics g) {
super.paintComponent(g);
if(mouseover) {
g.setColor(Color.blue);
} else {
g.setColor(Color.black);
}
g.fillRect(0,0,100,100);
g.setColor(Color.white);
g.drawString(name,10,10);
}
}

public static void main(String args[]) {
DesktopTest t = new DesktopTest();
t.show();
}

}
 
I took your code and ran it as is, and it worked fine. Each of the squares responded to the mouse click whether the frame had focus or not.

I'm using the Sun JDK 1.4.0 on Windows 2000. What are you using?
 
Oops. Made a mistake. I was running under JDK 1.3. When I switched to 1.4, it no longer worked.

You might need to extend JButton instead of JPanel for the SquarePanel, and handle the Mouse events as you do the third button.
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class SquarePanel extends JPanel implements MouseListener {
private OneSquare one;

public SquarePanel(String name) {
one = new OneSquare(name);
one.addMouseListener(this);
this.add(one);
}

public void mouseClicked(MouseEvent e) {
System.out.println(one + " clicked!");
}

public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {
one.setMouseOver(true);
one.repaint();
}
public void mouseExited(MouseEvent e) {
one.setMouseOver(false);
one.repaint();
}
}

class OneSquare extends JComponent {
private String name;
private boolean mouseover = false;
public OneSquare(String name) {
this.name = name;
setPreferredSize(new Dimension(100,100));
}

public void setMouseOver(boolean mouseover) {
this.mouseover = mouseover;
}

public String toString() { return name; }

public void paintComponent(Graphics g) {
super.paintComponent(g);
if(mouseover) {
g.setColor(Color.blue);
} else {
g.setColor(Color.black);
}
g.fillRect(0,0,100,100);
g.setColor(Color.white);
g.drawString(name,10,10);
}
// } a close bracket should be removed
public static void main(String args[]) {
DesktopTest t = new DesktopTest();
t.show();
}
}
 
Neither borbjos, nor prospers program worked for me, running j2sdk1.4.2_rc2 on linux 2.6
 
The reason why the first click on the InternalFrame does not work is because the first click is used to activate/focus on the InternalFrame. A work around could be to add the following :
In the method InternalFrameActivated(), you can do ... whatever you want. On the first mousePress on the internalframe this method will be called. Subsequent mousePresses will call your original mousePressed() method. I tested this code with jdk1.4.1_02-b06
Code:
        InternalFrameListener intListener = new InternalFrameListener() {
          public void internalFrameActivated(InternalFrameEvent e) {
            System.out.println("internalFrameActivated");
          }
          public void internalFrameOpened(InternalFrameEvent e) {
            System.out.println("internalFrameOpened");
          }
          public void internalFrameClosed(InternalFrameEvent e) {
            System.out.println("internalFrameClosed");
          }
          public void internalFrameClosing(InternalFrameEvent e) {
            System.out.println("internalFrameClosing");
          }
          public void internalFrameDeactivated(InternalFrameEvent e) {
            System.out.println("internalFrameDeactivated");
          }
          public void internalFrameDeiconified(InternalFrameEvent e) {
            System.out.println("internalFrameDeiconified");
          }
          public void internalFrameIconified(InternalFrameEvent e) {
            System.out.println("internalFrameIconified");
          }
        };
        frame1.addInternalFrameListener(intListener);
 
Hey - this works for me when importing javax.swing.event.*;

(still j2sdk1.4.2_rc2 / linux 2.6).
I give you an asterix since this most often forgotten by the XXXXX. ( :censored: )
 
Thanks for answering guys!

The reason why the first click on the InternalFrame does not work is because the first click is used to activate/focus on the InternalFrame.

Yes, I got that part too .. but what I didn't get is why it works with a JButton then? A JButton in the same place would receive the click.

Also, using the InternalFrameActivated() is a good idea, but it won't be an easy solution to implement - since my original app can contain multiple squares in a JInternalFrame, so I will have to find the one that should be 'clicked' ..

Maybe there's a way to forward the MouseEvent to the inner components some way?

Also, extending JButton would be a good solution if it didn't mess up my custom component that much :(

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top