Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

align component in JDialog

Posted on 2006-07-05
2
Medium Priority
?
952 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 195 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 180 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

661 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