Solved

align component in JDialog

Posted on 2006-07-05
2
914 Views
Last Modified: 2008-02-01

hello there,

I have a JDialog with a textfield and a button on it.the problem is i cannot align it properly under each other in the center of the panel.my code is below.please help.

public final static Dimension hpad10 = new Dimension(10,10);
    public final static Dimension vpad20 = new Dimension(5,30);
    public final static Dimension vpad7 = new Dimension(1, 20);
    public final static Dimension vpad4 = new Dimension(1, 4);
    public final static Insets insets = new Insets(5, 10, 0, 10);
Dimension screen =       Toolkit.getDefaultToolkit().getScreenSize();
    public CityFileChooser(PictMenuFrameDemo OwnerForm)
    {
      cityField = new JTextField(18) {
          public Dimension getMaximumSize() {
            return new Dimension(getPreferredSize().width, getPreferredSize().height);
          }
      };
      cityField.setAlignmentY(JComponent.TOP_ALIGNMENT);
      cityField.setEnabled(true);
      cityField.setEditable(true);

      // Create show button
      showButton = new JButton("Edit City Map");
      showButton.addActionListener(this);
        showButton.setMnemonic('s');

      JPanel control1 = new InsetPanel(insets);
      control1.setLayout(null);
      control1.setBorder(BorderFactory.createTitledBorder("City Name"));
      
      control1.setLayout(new BoxLayout(control1, BoxLayout.Y_AXIS));
      control1.add(Box.createRigidArea(vpad20));
      control1.add(cityField);
      control1.add(Box.createRigidArea(hpad10));
      control1.add(Box.createRigidArea(vpad7));
      control1.add(showButton);
      control1.add(Box.createRigidArea(vpad7));

      getContentPane().add(control1);
      setSize(225,160);
      setResizable(false);
      setLocation((screen.width - 325)/2,((screen.height-383)/2));
0
Comment
Question by:zolf
2 Comments
 
LVL 3

Accepted Solution

by:
chandru_in earned 65 total points
ID: 17041682
Why don't you use a Layout manager?

GridBagLayout should a good choice, it is very flexible.

See: http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html
0
 
LVL 8

Assisted Solution

by:colr__
colr__ earned 60 total points
ID: 17041749
GridBagLayout is definetaly the choice, but its a bit harder to pickup than the rest of the layout managers. Once you're comfortable with it though, you wont use anything else.

You might also want to consider GridLayout, which simply places components in a grid. You'd just create a grid with 1 column and the number of rows you need, then add your components one at a time and they are placed in the grid.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
micro services vs rest web services 16 105
Weekend adv creator 3 38
how to add new optional parameter to JSP 1 41
hibernate example for saving data 19 39
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question