Clicky

Snippet ID=8239901 attached show a formlayout & formbuilder method used to build a form.  He rows are added "" as I append components.  So as not to confuse the column definition, I want to add 2nd panel at the bottom of the first for a button bar.  All are onto the same frame.  Because of the handicapness I have with the forms layout methodologies I have chosen to do it this way.  May be a expert has a better approch in standard Java practice.  

Snippet ID=8239906 attached shows the button bar builder.  It doen't have to be this style of building buttons once I can build a button group at the bottom.

Please help me!
 public class SimpleSwingX_SQLite_VE2  { 	public static String[] contacts; 	public static String gc, kex, mimporter; 	public static JTextField custno, addr1, addr2, city, zip; 	public static JComboBox cb2, cbstates; 	private JFrame frame; 			     public static void main(String[] args) { //		ContactsForSQLite_VE gc = new ContactsForSQLite_VE(); //		gc.Contacts2(); 		SimpleSwingX_SQLite_VE2 test = new SimpleSwingX_SQLite_VE2(); 		test.XXX(); }  	public void XXX() {  // DEFINE COMPONENTS 		FocusEventsX kex = new FocusEventsX(); 	    String[] elements = {"Dell Corp.","Domino Pizza","IBM","Microsoft","Shipping & Trading","Wallmart"}; 		cb2 = new JComboBox(elements); //		cb2 = new JComboBox(contacts); 		cb2.setBackground(Color.white); 		cb2.setForeground(Color.black); 		cb2.setSelectedItem(null); 		cb2.addFocusListener(kex); 		AutoCompleteDecorator.decorate(cb2);  // strict is default  		custno = new JTextField(25); 		custno.addFocusListener(kex); 	    addr1 = new JTextField(25); 		addr1.addFocusListener(kex); 		addr2 = new JTextField(); 		city  = new JTextField(); 		zip   = new JTextField(); 	    String states[] = {"AL", "AK","AZ","AR","CA","CO","CT","DE","FL","GA","GU","HI","ID","IL","IN","IA","KS","KY","LA","MA","MD","MS","MI","MN","MI","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","PR","RI","SC","SD","TN","TX","UT","VT","VI","VA","WA","WV","WI","WY"}; 	    cbstates  = new JComboBox(states); 		AutoCompleteDecorator.decorate(cbstates);  // strict is default // DEFINE FRAME 	    frame = new JFrame(); 	    frame.setTitle("Make a Selection A");         frame.setVisible(true); 	    JFrame.setDefaultLookAndFeelDecorated(true); // DEFINE/BUILD FORMLAYOUT USING FORMLAYOUT & FORMBUILDER 		FormLayout layout = new FormLayout("r:p, 5dlu, 40dlu, r:4dlu, 20dlu, 5dlu, 50dlu, 4dlu, 50dlu",  				""); 		DefaultFormBuilder builder = new DefaultFormBuilder(layout); 		builder.setDefaultDialogBorder();  		CellConstraints cc = new CellConstraints();          builder.append("Select",	cb2,4);         builder.nextLine(); 		builder.append("Cust No", 	custno);         builder.nextLine(); 		builder.append("Address", 	addr1);         builder.nextLine(); 		builder.append("", 			addr2);         builder.nextLine(); 		builder.append("City", 		city);         builder.nextLine(); 		builder.append("States", 	cbstates);         builder.nextLine(); 		builder.append("Zip", 		zip);         builder.nextLine(); 		JPanel p = new JPanel(); 		p.setSize(50, 50); 		builder.append(p); 		frame.add(builder.getPanel()); 		frame.pack(); 	}                             
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 

Select allOpen in new window

 public class ButtonBarBuilderExample1 { private JButton b1,b2,b3,b4;      public static void main(String[] a){     	ActionPerformer ap = new ActionPerformer();     	ButtonBarBuilder builder = new ButtonBarBuilder(new FormDebugPanel());     	JButton b1 = new JButton("Items");     	b1.addActionListener(ap);         builder.addGridded(b1);         builder.addRelatedGap();     	JButton b2 = new JButton("Table");     	b2.addActionListener(ap);         builder.addGridded(b2);         builder.addRelatedGap();     	JButton b3 = new JButton("Save");     	b3.addActionListener(ap);         builder.addGridded(b3);         builder.addRelatedGap();     	JButton b4 = new JButton("Send");     	b4.addActionListener(ap);         builder.addGridded(b4);     	JButton b5 = new JButton("Delete");     	b5.addActionListener(ap);         builder.addGridded(b5); //        builder.addGlue(); //        builder.addGriddedButtons(new JButton[]{new JButton("Ok"), new JButton("Cancel"), new JButton("Apply")});                  JPanel p = new JPanel();         p.add(builder.getPanel());         JFrame f = new JFrame("FormLayout: Button Bar Builder Example");         f.setDefaultCloseOperation(2);         f.setSize(600,100);         f.add(p);         f.setVisible(true);     }                             
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 

Select allOpen in new window

asked 12/13/2011 03:36

vanavah's gravatar image

vanavah ♦♦


11 Answers:
I'd think the normal way would be to have BorderLayout with central panel which should
keep all the boxes which user has to fill and at the bottom you'll have the buttonPanel
added with BorderLayout.SOUTH parameter and
your buttonPanel will have FloaLayout and accommodate all butoons - that would be
the simplese and most convenienet layout
link

answered

for_yan's gravatar image

for_yan


jsut say something like that:

JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(b1);
buttonPanel.add(b2);
...

Then place your big form on one JPanel
like

JPanel mainPanel = new JPanel();

//add your form to this mainPanel

Container c = this.getContentPane();
c.setLayout(new BorderLayout());
c.add(mainPanel);
c.add(buttonPanel, BorderLayout.SOUTH);








link

answered 2011-12-14 at 00:09:09

for_yan's gravatar image

for_yan

Okay that seems ot be a solution.  I incorporated it in a simple code of what I cam doing.  For the main I am not using a JPanel but the formlayout method.  Nothing appears in the main frame and the buttons at the bottom is extremely small.  The problem with the formlayout method is that you have to divide the form iinto columns and this seems like an very difficualt task when you have various fields and buttons at the bottom at different widths.  So in my humble opinion, it would be bet to have more than one panels with formlayout and a separate panel for the buttons group/
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
package formslayout;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public class FormTest2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JPanel buttonPanel = new JPanel();
		JButton b1 = new JButton();
		JButton b2 = new JButton();
		buttonPanel.setLayout(new FlowLayout());
		buttonPanel.add(b1);
		buttonPanel.add(b2);

		FormLayout layout = new FormLayout("r:p, 5dlu, 40dlu, r:4dlu, 20dlu", "");
		DefaultFormBuilder builder = new DefaultFormBuilder(layout);
		builder.setDefaultDialogBorder();

		CellConstraints cc = new CellConstraints(); 
		JComboBox cb1 = new JComboBox();
		JTextField tf1 = new JTextField();
		JTextField tf2 = new JTextField();

		builder.append("Select",	cb1,3);
	 	builder.nextLine();
	 	builder.appendSeparator();
	 	builder.append("Customer Name",	tf1);
	 	builder.nextLine();
	 	builder.append("Address",		tf2);
	 	builder.appendSeparator();

		JFrame f = new JFrame();
		f.setSize(400, 300);
		Container c = builder.getContainer();
		c.setLayout(new BorderLayout());
		c.add(mainPanel);
		c.add(buttonPanel, BorderLayout.SOUTH);
		f.add(c);
		f.setVisible(true);

	}

}
link

answered 2011-12-14 at 00:21:22

vanavah's gravatar image

vanavah


You don't need to use FormLayout,

Use GridLayout to make columns and rows of your elements.
This is simple, and works nicely in most cases.
link

answered 2011-12-14 at 01:20:03

for_yan's gravatar image

for_yan

Okay I will try it.
link

answered 2011-12-14 at 01:25:38

vanavah's gravatar image

vanavah

Do you mean GridBagLayout which is more powerful and has constraints and coordinates, which I would prefer?
link

answered 2011-12-14 at 01:36:19

vanavah's gravatar image

vanavah

I would start with GridLayout - it is simpler and often you stilll can create with it what you need
link

answered 2011-12-14 at 01:56:16

for_yan's gravatar image

for_yan

using combinations of FlowLayout, BorderLayout anfd GridLayout you usually can create a lot of differently shaped forms
link

answered 2011-12-14 at 02:17:58

for_yan's gravatar image

for_yan

Ive done what you suggested and studied the many samples of the gridlayout at Java2s but they don't have a xy constraint.  How do you specify that in gridlayout that is why I said gridbaglayout.
link

answered 2011-12-14 at 02:21:16

vanavah's gravatar image

vanavah

A disadvantage for screen design it seems to (I could be wrong as I am new to Java) is the properties I check say "The container is divided into equal-sized rectangles, and one component is placed in each rectangle."  For placing fields on a frame, in my humble opinion, it don't seems to be a good solution for screen design.  it will work however for very simple designs as you pointed out.
link

answered 2011-12-14 at 04:08:48

vanavah's gravatar image

vanavah

It is true that GridLayout requires component to be equal, but it does not specify that these component should be functionaly identcal.
For example with GridLayout you can split your real eatse into two equal parts. One may be taken by some panel which
will have BorderLayout and its mani central (Center) part will be taken by textarea and on top (North) will be panell which will
keep a number of buttons (in Flow layout). On bottom part (South) will be panel which will keep two textfields in flow layout
or say three panels in gridlayout and one of them wiould be button antother would be textfield and third one lable

At the same part you second half will be split by two panles in Gridlayou vertically in three panles. Lower panel wll be your cusrtom panel
where you'll drraw something with your own paint metho. The middles one will be solit into 6 panels with GriDLayour(3,2)  and each one wuld have
 label and bu tton in Flowlayout, and so son aso on


Use looking at all of it will never even guess that you had gridlayout betwenn the firs two halves, and it will be as diverse as one cam imagine.
Ana dall ust with Flowlayout, BorderLayout and Gridlayout - without any additional
complexity, which GrodBagLayout will bring.

Yes, there are some acse where you would still want GridBag, but they are really rare - jus uunderstand taht you can embed
all these panles and split theme again and again as you wnat and it be as sophisticated as you need
 
link

answered 2011-12-14 at 04:26:47

for_yan's gravatar image

for_yan

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×13

Asked: 12/13/2011 03:36

Seen: 341 times

Last updated: 12/16/2011 07:53