Reputation: 75
In my program I try to show the JFrame
when I put the mouse on JLabel
and close the JFrame
when I remove the mouse from JLabel
.
How can I do it?
I tried below way, but I am getting flashing windows continuously(popup and close continuously)
public class NewJFrame extends javax.swing.JFrame {
NewJFrame1 frame = new NewJFrame1();
public NewJFrame() {
initComponents();
}
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
//======================================================================
jLabel1.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent e)
{
frame.setVisible(true);
}
});
jLabel1.addMouseListener(new MouseAdapter()
{
public void mouseExited(MouseEvent e)
{
frame.setVisible(false); //Hide window
}
});
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Testing ");
//======================================================================
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
public javax.swing.JLabel jLabel1;
}
In between these lines//============
I have the main code
How to display JFrame
when I put mouse on JLabel
and how to close the JFrame
when I remove the mouse from the JLabel
?
When I remove the below code , and when I place the mouse on JLabel
I am getting JFrame
popup , but I need to close the JFrame popup when I remove the mouse from JLabel
.
jLabel1.addMouseListener(new MouseAdapter()
{
public void mouseExited(MouseEvent e)
{
frame.setVisible(false); //Hide window
}
});
Upvotes: 0
Views: 171
Reputation: 18812
You need to add the JLabel (as well as the other components) to the JFrame. Once you do that, and the JLabel shows in the JFrame, you can use the JLabel's listeners.
EDITED:
A. Here is you code with some minimal changes to show / hide a 2nd JFrame:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class NewJFrame extends javax.swing.JFrame {
javax.swing.JFrame frame ;
//this will not compile and not needed
//NewJFrame1 frame = new NewJFrame1();
public NewJFrame() {
initComponents();
}
private void initComponents() {
frame = getAJFrame();
//set a layout manger
getContentPane().setLayout(new GridLayout(3, 1));
setLocationRelativeTo(null);
jLabel1 = new javax.swing.JLabel();
//add component
getContentPane().add(jLabel1);
JTextField jTextField1 = new javax.swing.JTextField();
//add component
getContentPane().add(jTextField1);
JButton jButton1 = new javax.swing.JButton();
//add component
getContentPane().add(jButton1);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.addMouseListener(new MouseAdapter()
{
@Override
public void mouseEntered(MouseEvent e)
{
frame.setVisible(true);
frame.pack();
}
});
jLabel1.addMouseListener(new MouseAdapter()
{
@Override
public void mouseExited(MouseEvent e)
{
frame.setVisible(false); //Hide window
}
});
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Testing ");
//set a size to the frame
setPreferredSize(new Dimension(200,100));
pack();
}
/**
*@return
*/
private JFrame getAJFrame() {
JFrame f = new JFrame("A JFrame");
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
f.getContentPane().setPreferredSize(new Dimension(150,150));
f.getContentPane().setBackground(Color.BLUE);
setVisible(false);
pack();
return f;
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
NewJFrame frame = new NewJFrame();
frame.setVisible(true);
}
});
}
public javax.swing.JLabel jLabel1;
}
B. If you are trying to show / hide NewJFrame itself: you will not be able to make the JFrame visible again, using mouseEntered. When the JFrame (and JLabel within it) are setVisible(false) it will not generate mouse events. The frame becomes invisible when mouse exits the JLabel. You will need to make it visible again using a different technique.
C. See The Use of Multiple JFrames: Good or Bad Practice?
Upvotes: 1
Reputation: 44496
Here is the code of the main method:
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
Main ex = new Main();
ex.setVisible(true);
});
}
Then add components to panel with:
JPanel panel = new JPanel();
this.add(panel);
JLabel jLabel1 = new JLabel("Label");
JTextField jTextField1 = new JTextField("Field");
JButton jButton1 = new JButton("Button");
panel.add(jLabel1);
panel.add(jTextField1);
panel.add(jButton1);
Then create the new splashing JFrame and set the current one as well. Since your main class extends JFrame, you can use the keyword this
.
JFrame frame = new JFrame();
frame.setSize(300, 200);
frame.setVisible(false);
this.setTitle("Title");
this.setSize(300, 200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
And finally let the listeners do the job from your code.
Upvotes: 1