Solved

align component in JDialog

Posted on 2006-07-05
2
928 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

749 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