Solved

align component in JDialog

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

759 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

24 Experts available now in Live!

Get 1:1 Help Now