0

as the title says what I want to achieve is to pop up an option message using JOptionPane such that the question and the buttons be placed on the same line. I'm reading the following tutorials but it seems to be quite difficult:

In a sense the idea is to have the following confirmation message layout:


The products are going to be removed from the database.

Are you sure you want to perform it? |YES| |NO|


where YES and NO should be buttons. (The text is not the real one, it's only to give the flavor of the message).

Any comments or hints will be welcomed.

Thanks a lot in advance.

pafede2
  • 1,626
  • 4
  • 23
  • 40

1 Answers1

1

Try this

   JOptionPane.showConfirmDialog(null,
                        getCustomPanel(), // this will return a Panel design on your Own
                        "JOptionPane Example : ",
                        JOptionPane.OK_CANCEL_OPTION,
                        JOptionPane.PLAIN_MESSAGE);
//and your Custom Panel
 private JPanel getCustomPanel() {
        JPanel panel = new JPanel();
        JLabel label = new JLabel("Text Message:");
        panel.setLayout(null);
        JButton okbtn=new JButton("ok");
        label.setBounds(10,20,200,40); //x,y,width,height
        okbtn.setBounds(220,20,80,40); //x,y,width,height
        panel.add(label);
        panel.add(okbtn);
        return panel;
    }
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
  • Hi, first of all thanks for answering. Indeed it's useful, the only two issues are that in addition to the custom okbtn the message also draw the default buttons and on the other hand the showConfirmDialog return value is still based on the default buttons set by JOptionPane. – pafede2 Sep 21 '15 at 08:42
  • Using JOptionPane.showOptionDialog(null, getCustomPanel(), "Hello", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new Object[] {}, null); removes the default buttons from the message. Ref.: http://stackoverflow.com/questions/14126975/joptionpane-without-button – pafede2 Sep 21 '15 at 10:46