Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Custom Dialog Layout

Status
Not open for further replies.
Jan 20, 2005
180
0
0
US
I am trying to get an input dialog that puts labels on the left, input boxes on the right and the ok/cancel on the bottom. Ive gone over almost all ideas Ive ad that I thought would work.
labels[] are JLabel and textfields[] are JTextfield

This is what I thought would work, or the last thing I though would work.

Any help or advice would be appreciated.

Code:
final Box outmostBox = Box.createHorizontalBox();
getContentPane().add(outmostBox, BorderLayout.WEST);
outmostBox.add(Box.createHorizontalStrut(PADDING));
        
JSplitPane spanel = new JSplitPane();
spanel.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
spanel.setContinuousLayout(true);
        
JPanel lpanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
JPanel rpanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
        
for(int i = 0;i < NUM_ATTRIBS;++i) {           
    lpanel.add(labels[i]);
    rpanel.add(textfields[i]);            
}
 
spanel.add(lpanel,JSplitPane.LEFT);
spanel.add(rpanel,JSplitPane.RIGHT);
outmostBox.add(spanel);
 
Do you mean you should rather be using a JTable?
 
I don't like the idea of using tables for Layouts.

lotharious: what do you mean when you say it doesn't work?

Cheers,
Dian
 
I have stopped trying to build these kind of layouts using the native layout managers. I use this instead ( I still use BorderLayout and FlowLayout for top-level placement, but label/fields and complex layouts are much easier using MigLayout.

Tim
 
Well, no matter how I seem to put it out, everything comes out as one straight line or multiple lines wrapped. I finally got this to look somewhat like what I wanted but I dont understand why it does unless it is just wrapping the way I want.

In other words, I cant get the layout to layout at all.
Id rather not use an external layout though..
 
I have done a lot of work using Swing layouts. I have typically in the past used a GridLayouts to do labels/input fields.

If you have a BorderLayout on your top level planel, then add a subpanel with a GridLayout into its WEST area and another seubpanel with another GridLayout into its CENTER area, then you can put the labels into the first subpanel and the entry fields into the second. The buttons can go into a third subpanel with a FlowLayout put into the top panel's SOUTH area. Both GridLayouts should have one column with as many rows as entry fields/labels. Doing it this way allows the entry fields to expand with the top form's resizing.

If you want some of the entry fields to have a constrained size, then you'd have to put each entry component into the WEST area of another little BorderLayout panel which is then put into the GridLayout panel. This allows you to control the fields width; these fields are constrained leaving the rest free to grow with the top panel.

After I'd pulled most of my hair out over the years debugging and maintaining these horrendous multi-embedded panels-within-panels I decided to get to grips with the GridBagLayout which allows you to lay out fairly complex arrangments of components with just one layout. Assuming you can master the tricky constraints.

Much later I discovered MigLayout. This lets you use concepts borrowed from html tables to lay out your components. Yes, it's external. But so what? It's open source and you can use it in your commercial products. There's even a chance it'll be included in Java7.

It's up to you really. I'm just trying to save you some time.

But in any case. Don't use JSplitPane to separate your labels from your entry fields. That is not what it is intended for.

Code:
JPanel topPanel = new JPanel(new BorderLayout());
JPanel labelPanel = new JPanel(new GridLayout(3,1));
JPanel fieldPanel = new JPanel(new GridLayout(3,1));
JPabel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

labelPanel.add(new JLabel("label1:"));
labelPanel.add(new JLabel("label2:"));
labelPanel.add(new JLabel("label3:"));

fieldPanel.add(new JTextField());
fieldPanel.add(new JTextField());
fieldPanel.add(new JTextField());

buttonPanel.add(new JButton("OK"));
buttonPanel.add(new JButton("Cancel"));

topPanel.add(labelPanel, BorderLayout.WEST);
topPanel.add(fieldPanel, BorderLayout.CENTER);
topPanel.add(buttonPanel, BorderLayout.SOUTH);

You'd probably want to site the topPanel in the NORTH of the contentPane. This is only an example and getting it to look right for you may take some tweaking. And sometimes A LOT of tweaking. I've typed all this straight from my memory and it might not work just as I think it will - I don't do it like this anymore like I explained above. I hope it gets you further forward anyway.






Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top