Layout customization of Joptionpane

Asked

Viewed 335 times

1

I have the following structure of my Joptionpane, with horizontal typing fields:

Imagem

How to vertical the input fields?

Here below is the class code:

import javax.swing.*; 

public class JOptionPaneMultiInput {
   public static void main(String[] args) {
      JTextField fieldNome = new JTextField(5);
      JTextField fieldTelefone = new JTextField(5);
      JTextField fieldEmail = new JTextField(5);
      JPanel myPanel = new JPanel();
      myPanel.add(new JLabel("Digite Nome:"));
      myPanel.add(fieldNome);
      myPanel.add(Box.createHorizontalStrut(15)); // a spacer
      myPanel.add(new JLabel("Digite Telefone:"));
      myPanel.add(fieldTelefone);
      myPanel.add(Box.createHorizontalStrut(15)); // a spacer
      myPanel.add(new JLabel("Digite Email:"));
      myPanel.add(fieldEmail);

      int result = JOptionPane.showConfirmDialog(null, myPanel, 
               "Entrada de valores", JOptionPane.OK_CANCEL_OPTION);
      if (result == JOptionPane.OK_OPTION) {
         System.out.println("Nome value: " + fieldNome.getText());
         System.out.println("Telefone value: " + fieldTelefone.getText());
         System.out.println("Email value: " + fieldEmail.getText());
      }

   }
}
  • 2

    Pedro, you need to add one LayoutManager in his JPanel. There are many options, the LayoutManager ideal will depend on ease and taste. Have a look at the list: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html. Recommend or the GridLayout or the SpringLayout.

  • Are you using any IDE?

  • @Patrick I’m using the eclipse

  • Ever tried to ride a JPanel on the prototyper and then just call it?

  • @Patrick with the tip Wakim I think I got....

1 answer

1


With the help of @Wakim with the use of Gridlayout I managed, as a result in the figure below:

Imagem

Code added below, resulting in the fields in the vertical position:

 GridLayout experimentLayout = new GridLayout(0,1);
 myPanel.setLayout(experimentLayout);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.