Solved

align component in JDialog

Posted on 2006-07-05
2
906 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 70
Python Assistance 7 73
Free Alternative to JIRA 4 91
Why method in Java which is called from Runnable run() doesn't need to be 'static'? 1 22
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now