Vassilis De
Vassilis De

Reputation: 373

JPanel doesn't appear inside JOptionPane

I have a JDesktopPane that contains a button. When I click on it, I want to appear a customized JOptionPane in which I added a JLabel and a JTable.

The problem is that I don't get anything inside JOptionPane

Here is my code:

class Viewer extends JPanel{
  public Viewer(){
    JLabel l = new JLabel("Hi");
    JTable t = new JTable(2,2);
    JPanel p = new JPanel();
    p.add(l,BorderLayout.NORTH);
    p.add(t,BorderLayout.CENTER);
  }
}

public class JOptionPanesWithJPanel extends JFrame{
  JPanel desktoppane;
  public JOptionPanesWithJPanel(){
    desktoppane = new JPanel();
    int inset = 50;
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setBounds(inset, inset, screenSize.width  - inset*2, screenSize.height - inset*2);
    JPanel butPanel = new JPanel();
    JButton but = new JButton("click");
    butPanel.setVisible(true);
    butPanel.add(but);
    desktoppane.add(butPanel);
    getContentPane().add(desktoppane, BorderLayout.CENTER);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setVisible(true);

    but.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
            Viewer v = new Viewer();
            //make JOptionPane resisable
            UIManager.put("OptionPane.minimumSize",new Dimension(150,150));
            JOptionPane.showMessageDialog(null, v, null, JOptionPane.PLAIN_MESSAGE);
        }
    });
}

public static void main(String[] args) {
    for(javax.swing.UIManager.LookAndFeelInfo lookAndFeelInfo : javax.swing.UIManager.getInstalledLookAndFeels()){
        if("Windows".equals(lookAndFeelInfo.getName())){
            try {
                javax.swing.UIManager.setLookAndFeel(lookAndFeelInfo.getClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                System.out.println(ex);
            }
            break;
        }
    }
    JOptionPanesWithJPanel j = new JOptionPanesWithJPanel();
  }
}

What am I supposed to do to appear the panel inside JOptionPane? Thank you in advance.

PS: The reason I use JoptionPane is to forbid user clicking anywhere else in JDesktopPane (and also the button) unless he closes the current window.

Upvotes: 0

Views: 483

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

You're adding your components to a second panel, but not adding the panel to the Viewer...

    public Viewer() {
        JLabel l = new JLabel("Hi");
        JTable t = new JTable(2, 2);
        JPanel p = new JPanel(); // <-- Create the new panel
        p.add(l, BorderLayout.NORTH);
        p.add(t, BorderLayout.CENTER);
        // And now what??
    }

Without much more evidence, I'd throw out the second panel and just add the components to the Viewer directly...

    public Viewer() {
        setLayout(new BorderLayout());
        JLabel l = new JLabel("Hi");
        JTable t = new JTable(2, 2);
        add(l, BorderLayout.NORTH);
        add(t, BorderLayout.CENTER);
    }

Nit picks...

This...

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(inset, inset, screenSize.width - inset * 2, screenSize.height - inset * 2);

Is a really poor way to size/position you frame. Not everybody has the taskbar/dock the same size or at the same position and you shouldn't rely on "magic" numbers (numbers you make up), instead, you should find away to make appropriate determinations of the current state of the environment.

See How to set present screensize in Java Swing? for more details about detecting the screen size and viewable area of the screen

Upvotes: 2

Related Questions