Reputation: 1856
does anyone know how to make a GUI button open up a new JPanel in java? its not on google. its to show an about panel. thanks for the help!
Upvotes: 0
Views: 8913
Reputation: 4104
You can show panel using an undecorated JDialog
public static void main(String args[])
{
final JDialog bwin = new JDialog();
bwin.addWindowFocusListener(new WindowFocusListener()
{
@Override
public void windowLostFocus(WindowEvent e)
{
bwin.setVisible(false);
bwin.dispose();
}
@Override
public void windowGainedFocus(WindowEvent e)
{
}
});
bwin.setUndecorated(true);
JLabel label = new JLabel("About");
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(label);
panel.setPreferredSize(new Dimension(200,200));
bwin.add(panel);
bwin.pack();
bwin.setVisible(true);
}
Upvotes: 1
Reputation: 12112
I guess JDialog is what you need.
See this for details : How to Make Dialogs
Here is a sample :
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class CreateDialogFromOptionPane {
public static void main(final String[] args) {
JFrame parent = new JFrame();
JButton button = new JButton();
button.setText("Click me to show dialog!");
parent.add(button);
parent.pack();
parent.setVisible(true);
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane optionPane = new JOptionPane("Is this what you need?", JOptionPane.QUESTION_MESSAGE,JOptionPane.YES_NO_OPTION);
JDialog dialog = optionPane.createDialog("Dialog");
dialog.setVisible(true);
}
});
}
}
Upvotes: 6
Reputation: 280
first you would need to create an event handler for your button, then in your handler you should create your panel and make it visible. if you want more of a popup you should use like:
JOptionPane.showMessageDialog(frame, "This is my message");
that will create a warning message, you could also create your own costume dialog i would suggest reading this
Upvotes: 2