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.