participate


Swing - Modify layout of JOptionPane
<<   Back to Forum  |   Give us Feedback
This topic has 4 replies on 1 page.
Romulas1991
Posts:27
Registered: 4/29/09
Modify layout of JOptionPane   
Nov 7, 2009 10:02 PM

 
I am trying to modify the layout of JOptionPane.showOptionDialog(), is this possible? Here is my code:

import javax.swing.*;
 
public class Test {
 
	public static void main(String[] args) {
		
		JTextField height = new JTextField(3);
		JTextField width = new JTextField(3);
		JTextField mines = new JTextField(3);
		
		String s0 = "Height:";
		String s1 = "Width:";
		String s2 = "Mines:";
		
		Object[] options = new Object[] {s0,height,s1,width,s2,mines};
		JOptionPane.showOptionDialog(null, options, "Custom Field", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, null);
		
		System.out.println("height = " + height.getText());
		System.out.println("width = " + width.getText());
		System.out.println("mines = " + mines.getText());
		
	}
	
}

This produces a JOptionPane with the following layout

Height:
____________
Width:
____________
Mines:
____________
okButton cancelButton

My goal is to produce the following layout

Height:________ okButton
Width:________ cancelButton
Mines:________

Is this possible?
 
DarrylBurke
Posts:15,598
Registered: 7/2/07
Re: Modify layout of JOptionPane   
Nov 7, 2009 10:15 PM (reply 1 of 4)  (In reply to original post )

 
Is this possible?
Very little is impossible

The tough way: create and set your own OptionPaneUI

The easier way: create and display your own modal JDialog.

db
 
Encephalopathic
Posts:13,715
Registered: 12/20/07
Re: Modify layout of JOptionPane   
Nov 8, 2009 4:50 AM (reply 2 of 4)  (In reply to original post )

 
I second Darryl's second recommendation: create your own JPanel with its own OK and Cancel buttons and place it in a JDialog.
 
Romulas1991
Posts:27
Registered: 4/29/09
Re: Modify layout of JOptionPane   
Nov 9, 2009 1:26 PM (reply 3 of 4)  (In reply to #2 )

 
Wow, awesome! I didn't even think about it that way. So, I would put my JPanel in the Object[] array?
 
jduprez
Posts:1,169
Registered: 10/2/98
Re: Modify layout of JOptionPane   
Nov 10, 2009 12:34 AM (reply 4 of 4)  (In reply to #3 )

 
Wow, awesome! I didn't even think about it that way. So, I would put my JPanel in the Object[] array?

If you think about the options or even the message argument of the JOptionPane.showXxxDialog() methods, no: I think they meant creating a JDialog, and add your custom panel as the dialog's contentPane.
Note that this is one of the rare cases (in my humble but stubborn opinion) where creating a JDialog is preferrable to tweaking the (otherwise preferred way) JOptionPane into something it was not designed for.

Note that you might experiment with the DEFAULT_OPTION option type, which is supposed to not provide the YES/NOOK/CANCEL row, and provide your own panel, or array of panels, but that would be awkward to have to deal manually with the OK and Cancel buttons (which JOptionPane is designed for), at this point it's merely clearer to use a plain JDialog.
 
This topic has 4 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums