making a JPanel into a JOptionPane.OK_OPTION

14,477

You can simply pass the object of that JPanel within the JOptionPane. For example:

 JPanel panel = new JPanel();
 panel.add(new JButton("Click"));
 panel.add(new JTextField(20));
 panel.add(new JLabel("Label"));
 JOptionPane.showMessageDialog(null,panel,"Information",JOptionPane.INFORMATION_MESSAGE);

Above code will put the JPanel on JOptionPane message dialog. When you click OK the panel closes Off.

Share:
14,477
Jordan Atkinson
Author by

Jordan Atkinson

Studying BSc Computer Science at Teesside University knowledgable in Java, C#, LISP, Delphi(7), SQL, .NET(limited), Android

Updated on June 04, 2022

Comments

  • Jordan Atkinson
    Jordan Atkinson almost 2 years

    currently i have a class which extends JPanel and basically shows some information about an object passed into its constructor. There are various labels and Image icons on the screen and has a BorderLayout set.

    This panel is triggered when the user left clicks on an ImageIcon from the main GUI and shows up on the screen.

    i was wondering, how (if there is a way) i could implement the JOptionPane.OK_OPTION onto the whole Panel so that i dont have to close the panel using Event handling, as the screen is simply to show information and when the user has finished, they press okay and the panel should close off.

    thanks Jordan

  • Jordan Atkinson
    Jordan Atkinson about 11 years
    aah i didn't think i would be that simple, i tried variations of this and looked at the API's but couldn't work it out :). thank you :)
  • Vishal K
    Vishal K about 11 years
    My pleasure. And dont forget to mark the answer as accepted if it solved your problem . Look here to know how to accept an answer: meta.stackexchange.com/questions/5234/…