Reputation: 710
I am trying create a JOptionPane
just like this. I try many Layout Manager but (GridBagLayout
, GridLayout
, AbsoluteLayout
) but i am not succeed yet.
For example if i use GridBagLayout
, ipadx
really helpfull but JTextFields
width depends in their text length.
If i use GridLayout
, every objects width belongs to columns width.
If i use AbsoluteLayout
, JOptionPane
not expand itself to configured JPanel
bounds which carry objects.
I need fixed object widths, heights and JOptionPane
should expand itself when we create it with JPanel
which carry items.
Which Layout Manager optimum for this mission? I will focus on it if you suggest.
--EDIT--
GridLayout example;
JPanel panel = new JPanel( new GridLayout(8, 2) );
JLabel inforForNameLabel = new JLabel();
inforForNameLabel.setText("Long Label Name....................................");
final JTextField userNameTextField = new JTextField();
userNameTextField.setText("aaaaaaaaaaaaa");
userNameTextField.setEditable(false);
JLabel inforForNameSurname = new JLabel();
inforForNameSurname.setText("Long Label Name..................................");
final JTextField NameSurnameTextField = new JTextField();
NameSurnameTextField.setText("aaaaaaaaaaaa aaaaaaaaaa");
JLabel inforForStatsCombobox = new JLabel();
inforForStatsCombobox.setText("Long Label Name.................................");
JComboBox statComboBox = new JComboBox();
DefaultComboBoxModel dt = new DefaultComboBoxModel();
statComboBox.setModel(dt);
dt.addElement("USER.....");
dt.addElement("ADMIN");
statComboBox.setSelectedItem("USER.....");
JLabel inforForAuthCombobox = new JLabel();
inforForAuthCombobox.setText("Long Label Name.................................");
String[] authComboBoxObjects = { "READ", "READ/WRITE"};
final JComboBox authComboBox = new JComboBox(authComboBoxObjects);
authComboBox.setSelectedItem("READ/WRITE");
panel.add(inforForNameLabel);
panel.add(userNameTextField);
panel.add(inforForNameSurname);
panel.add(NameSurnameTextField);
panel.add(inforForStatsCombobox);
panel.add(statComboBox);
panel.add(inforForAuthCombobox);
panel.add(authComboBox);
String[] buttons = { "EXIT", "OK" };
int rc = JOptionPane.showOptionDialog(null, panel,"User Config", JOptionPane.INFORMATION_MESSAGE,JOptionPane.QUESTION_MESSAGE, null, buttons, buttons[1]);
if(rc == 0 || rc == -1){
System.out.println(userNameTextField.getText());
}
else{
System.out.println("Cancel...");
}
Upvotes: 1
Views: 632
Reputation: 710
Thanks for the routed comments to @ControlAltDel and @Gorbles
Below codes build OptionPane which desired format at the Question;
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel inforForNameLabel = new JLabel();
inforForNameLabel.setText("Long Label Name.............................");
inforForNameLabel.setAlignmentX(LEFT_ALIGNMENT);
JLabel inforForNameLabel2 = new JLabel();
inforForNameLabel2.setText("Long Label Name............................");
inforForNameLabel2.setAlignmentX(LEFT_ALIGNMENT);
final JTextField userNameTextField = new JTextField();
userNameTextField.setText("aaaaaaaaaaaaa");
userNameTextField.setEditable(false);
userNameTextField.setPreferredSize(new Dimension(345, 30));
userNameTextField.setMaximumSize(new Dimension(345, 30));
userNameTextField.setAlignmentX(LEFT_ALIGNMENT);
JLabel inforForNameSurname = new JLabel();
inforForNameSurname.setText("Long Label Nameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
inforForNameSurname.setAlignmentX(LEFT_ALIGNMENT);
final JTextField NameSurnameTextField = new JTextField();
NameSurnameTextField.setText("aaaaaaaaaaaa aaaaaaaaaa");
NameSurnameTextField.setPreferredSize(new Dimension(345, 30));
NameSurnameTextField.setMaximumSize(new Dimension(345, 30));
NameSurnameTextField.setAlignmentX(LEFT_ALIGNMENT);
JLabel inforForStatsCombobox = new JLabel();
inforForStatsCombobox.setText("Long Label Name....................");
inforForStatsCombobox.setAlignmentX(LEFT_ALIGNMENT);
JComboBox statComboBox = new JComboBox();
DefaultComboBoxModel dt = new DefaultComboBoxModel();
statComboBox.setModel(dt);
dt.addElement("USER.....");
dt.addElement("ADMIN");
statComboBox.setSelectedItem("USER.....");
statComboBox.setPreferredSize(new Dimension(200, 30));
statComboBox.setMaximumSize(new Dimension(200, 30));
statComboBox.setAlignmentX(LEFT_ALIGNMENT);
JLabel inforForAuthCombobox = new JLabel();
inforForAuthCombobox.setText("Long Label Name................................");
inforForAuthCombobox.setAlignmentX(LEFT_ALIGNMENT);
String[] authComboBoxObjects = { "READ", "READ/WRITE"};
final JComboBox authComboBox = new JComboBox(authComboBoxObjects);
authComboBox.setSelectedItem("READ/WRITE");
authComboBox.setPreferredSize(new Dimension(200, 30));
authComboBox.setMaximumSize(new Dimension(200, 30));
authComboBox.setAlignmentX(LEFT_ALIGNMENT);
panel.add(inforForNameLabel);
panel.add(inforForNameLabel2);
panel.add(userNameTextField);
panel.add(inforForNameSurname);
panel.add(NameSurnameTextField);
panel.add(inforForStatsCombobox);
panel.add(statComboBox);
panel.add(inforForAuthCombobox);
panel.add(authComboBox);
String[] buttons = { "EXIT", "OK" };
int rc = JOptionPane.showOptionDialog(null, panel,"User Config", JOptionPane.INFORMATION_MESSAGE,JOptionPane.QUESTION_MESSAGE, null, buttons, buttons[1]);
if(rc == 0 || rc == -1){
System.out.println("Cancel...");
}
else{
System.out.println(userNameTextField.getText());
}
Upvotes: 1
Reputation: 324147
Agreed a JDialog gives you total control.
i need to know how can i make same thing via JOptionPane
The UIManager seems to control the minimum size. Before displaying the option pane you can override by using:
Dimension size = UIManager.getDimension("OptionPane.minimumSize");
size.width = 100;
UIManager.put("OptionPane.minimumSize", size);
You can check out UIManager Defaults for a list of other properties controlled by the UIManager. Be aware that it is not guaranteed that all LAF's will manage the same properties.
Upvotes: 0