aspose file tools*
The moose likes Beginning Java and the fly likes complex dialog layout? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "complex dialog layout?" Watch "complex dialog layout?" New topic
Author

complex dialog layout?

Chris Pat
Ranch Hand

Joined: Aug 27, 2005
Posts: 156
Hello
Is/how it possible to create a layout in a dialog that allows single record form entry with the labels and text edit fields left-right with a cancel/commit button AND also have below it a listing scrollpane? On the scrollpane how do I build the scrollable client dynamically from an iterator of objects? It is uniquely easy in html, but how in swing? tia.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
a few nested panels should do it
panel1(GridLayout) for your labels/textfields
panlel2(FlowLayout) for your buttons
panel3(BorderLayout) - panel1(CENTER),panel2(SOUTH)
panel4(BorderLayout) - panel3(NORTH), JScrollPane(JList)(CENTER)
now add panel4 to the dialog

create a JList, using a DefaultListModel, then use dlm.addElement(...)
to add items to the JList
Chris Pat
Ranch Hand

Joined: Aug 27, 2005
Posts: 156
Hello
Thank you, however I cant understand why the code below is failing. All I am trying to do is add the JPanel to another JPanel. What syntax could be wrong? No matter what random cast I put before it it claims an identifier missing. I know from the jd that JPanel inherits the add method from container, but that does not help me. Can you? and more broadly what dont I understand? I created the objects and I am trying to add one object which is a descendent of a Component to another class that inherits the ability to add Components. tia.

public class Dialog2
extends JDialog {
JPanel panel1 = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
JPanel formFields =new JPanel(new GridLayout(3,2));
JPanel buttons = new JPanel(new FlowLayout());
JPanel tmp = new JPanel(new BorderLayout());
tmp.add(formFields);
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> tmp.add(formFields);

must be inside a method
Chris Pat
Ranch Hand

Joined: Aug 27, 2005
Posts: 156
Hi Michael
Thanks. It is late and I feel stupid. Hopefully this is not as obvious. Why does the code below display the dialog but not the panels? tia.

package ui;

import java.awt.*;
import javax.swing.*;

public class Dialog2
extends JDialog {
JPanel panel1 = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
JPanel formFields =new JPanel(new GridLayout(3,2));
JPanel buttons = new JPanel(new FlowLayout());
JPanel tmp = new JPanel(new BorderLayout());
JPanel finalPanel = new JPanel(new BorderLayout());
JPanel listing = new JPanel();

public Dialog2(Frame frame, String title, boolean modal) {
super(frame, title, modal);
try {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jbInit();
pack();
}
catch (Exception exception) {
exception.printStackTrace();
}

formFields.add(new JLabel("Label"),new JTextField("Data"));
buttons.add(new JButton("Cancel"),new JButton("Commit"));

tmp.add(formFields,BorderLayout.CENTER);
tmp.add(buttons,BorderLayout.SOUTH);
finalPanel.add(tmp,BorderLayout.NORTH);
finalPanel.add(listing,BorderLayout.SOUTH);

}

public Dialog2() {
this(new Frame(), "Dialog2", true);
}

private void jbInit() throws Exception {
panel1.setLayout(borderLayout1);
getContentPane().add(panel1);
}

public static void main (String[] args) {
Dialog2 dlg2 = new Dialog2();
dlg2.setLocation(200,200);
dlg2.setSize(250,300);
dlg2.setVisible(true);
}
}
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Why does the code below display the dialog but not the panels?

you add all your components to finalPanel
you add panel1 to the dialog
you haven't added finalPanel to panel1
add this line to the end of the constructor
panel1.add(finalPanel);

formFields.add(new JLabel("Label"),new JTextField("Data"));
is this one of the newer java versions?
normally the add would be
formFields.add(new JLabel("Label"));
formFields.add(new JTextField("Data"));

you are mixing awt/swing components (using Frame) - this could lead to
problems later on - change to JFrame

I see you had an attempt at the code tags (similar to my first go at it)
click the code button
when the tags appear - paste your code between the tags
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: complex dialog layout?
 
Similar Threads
Converting from null Layout to Layouts
default layouts
Manually sizing JScrollPane
how to add scrollpane for a jframe?
Center a JPanel in JScrollPane and add a JToolBar