Skip to main content

What Object for manage a form?

5 replies [Last post]
fanny983
Offline
Joined: 2010-03-14
Points: 0

Hi! I have a problem. I must manage a form (for example with element name, surname, age, etc.) but I don't know what's the best object for this situation. I think about JBoxLayout and GridBagLayout but it's very hard! Someone can suggest me a solution?
Thank you! :)

Message was edited by: fanny983

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
rturnbull
Offline
Joined: 2005-08-27
Points: 0

I use GridBagLayout. There can be a bit of a sharp learning curve, but once
you get the hang of it, you can do just about anything.

You could also look at JGoodies forms. Quite a few people use that.
http://jgoodies.com/

fanny983
Offline
Joined: 2010-03-14
Points: 0

Ok, I use GridBagConstraints in this way

private GridBagConstraints lastConstraints = null;
private GridBagConstraints middleConstraints = null;
private GridBagConstraints labelConstraints = null;

public FormUtility() {
// Set up the constraints for the "last" field in each
// row first, then copy and modify those constraints.

// weightx is 1.0 for fields, 0.0 for labels
// gridwidth is REMAINDER for fields, 1 for labels
lastConstraints = new GridBagConstraints();

// Stretch components horizontally (but not vertically)
lastConstraints.fill = GridBagConstraints.HORIZONTAL;

// Components that are too short or narrow for their space
// Should be pinned to the northwest (upper left) corner
lastConstraints.anchor = GridBagConstraints.NORTHWEST;

// Give the "last" component as much space as possible
lastConstraints.weightx = 0.0;

// Give the "last" component the remainder of the row
lastConstraints.gridwidth = GridBagConstraints.REMAINDER;

// Add a little padding
lastConstraints.insets = new Insets(1, 1, 1, 1);

// Now for the "middle" field components
middleConstraints = (GridBagConstraints) lastConstraints.clone();

// These still get as much space as possible, but do
// not close out a row
middleConstraints.gridwidth = GridBagConstraints.RELATIVE;

// And finally the "label" constrains, typically to be
// used for the first component on each row
labelConstraints = (GridBagConstraints) lastConstraints.clone();

// Give these as little space as necessary
labelConstraints.weightx = 0.0;
labelConstraints.gridwidth = 1;
}

but there is not space between lastConstraints and middleConstraints.
Any suggestions?

Thanks!

rturnbull
Offline
Joined: 2005-08-27
Points: 0

Without the full code it's hard to be sure what you are doing, but the usual way
is to have one set of constraints and change them for each grid entry.

If you use seperate constraints for each field you will need to set gridx and gridy
specifically for each useage.

If you could supply a small runnable example, we could be of more help. Don't worry about
field entry, just the layout.

fanny983
Offline
Joined: 2010-03-14
Points: 0

Hi, I have dimension and position problem with GridBagConstraints.
Follow my code:

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class HomePage extends JFrame {

JDesktopPane jdpDesktop;

public HomePage(){
//set della finestra principale
super("SME");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0, 0, screenSize.width, screenSize.height);

jdpDesktop = new JDesktopPane();

setContentPane(jdpDesktop);
setJMenuBar(createMenuBar());
// Make dragging faster by setting drag mode to Outline
jdpDesktop.putClientProperty("JDesktopPane.dragMode", "outline");
setVisible(true);

}

protected JMenuBar createMenuBar() {
JMenuBar menuBar;
JMenu submenu;
JMenuItem menuItem;

//Create the menu bar.
menuBar = new JMenuBar();

JMenu menu = new JMenu("Operazioni");
menuBar.add(menu);

//a group of JMenuItems
menuItem = new JMenuItem("Nuovo Militare", KeyEvent.VK_N);
menuItem.setActionCommand("1");

menuItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
Invoice_Generate newMil = new Invoice_Generate(jdpDesktop);
}
});

menu.add(menuItem);

menuItem = new JMenuItem("Ricerca Militari", KeyEvent.VK_R);
menuItem.setActionCommand("2");
//menuItem.addActionListener(this);
menu.add(menuItem);

menuItem = new JMenuItem("Elimina Militare", KeyEvent.VK_E);
menuItem.setActionCommand("3");
//menuItem.addActionListener(this);
menu.add(menuItem);

menu.addSeparator();

//a submenu
submenu = new JMenu("Report Excel");
submenu.setMnemonic(KeyEvent.VK_R);

menuItem = new JMenuItem("Crea graduatoria", KeyEvent.VK_G);
menuItem.setActionCommand("4");
//menuItem.addActionListener(this);
submenu.add(menuItem);

menuItem = new JMenuItem("Report di selezione", KeyEvent.VK_S);
menuItem.setActionCommand("5");
//menuItem.addActionListener(this);
submenu.add(menuItem);

menuItem = new JMenuItem("Militari selezionati e certificati", KeyEvent.VK_C);
menuItem.setActionCommand("6");
//menuItem.addActionListener(this);
submenu.add(menuItem);

menu.add(submenu);
//end submenu

menu.addSeparator();

menuItem = new JMenuItem("Esci");
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.setActionCommand("7");

menuItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
quit();
}
});

menu.add(menuItem);

return menuBar;
}

/**
* esce dall'applicazione
*/
protected void quit() {
System.exit(0);
}

}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------
public class Invoice_Generate extends JFrame {

private static final long serialVersionUID = 8742948000520383074L;

MyInternalFrame frame;
JPanel pannello;

public Invoice_Generate(){
JDesktopPane jdpDesktop = new JDesktopPane();
setContentPane(jdpDesktop);
jdpDesktop.putClientProperty("JDesktopPane.dragMode", "outline");

frame = new MyInternalFrame();
//riempio il form
createForm();
frame.setVisible(true);
// Every JInternalFrame must be added to content pane using JDesktopPane
jdpDesktop.add(frame);
try {
frame.setMaximum(true);
frame.setSelected(true);
} catch(Exception e) {
e.printStackTrace();
}
}

class MyInternalFrame extends JInternalFrame {

private static final long serialVersionUID = 3555060319667760594L;

public MyInternalFrame() {
super("Nuovo Militare", false, // resizable
true, // closable
false, // maximizable
false);// iconifiable
setSize(300, 300);
}
}

/**
* Creo il form per l'inserimento del nuovo militare
* @param jdpDesktop
*/
public void createForm() {
pannello = new JPanel();
pannello.setPreferredSize(new Dimension(50,50));
pannello.setLayout(new GridBagLayout());
FormUtility formUtility = new FormUtility();

formUtility.addLabel(pannello, new JLabel(), "prova1", 0, 0, 5);
formUtility.addField(pannello, new JComboBox(), "CboProva1", 1, 0, 1, 1, 10, true, 0);

formUtility.addLabel(pannello, new JLabel(), "prova2", 0, 1, 5);
formUtility.addField(pannello, new JTextField(), "TxtProva2", 1, 1, 1, 1, 10, true, 200);

formUtility.addLabel(pannello, new JLabel(), "prova3", 0, 2, 5);
formUtility.addField(pannello, new JTextField(), "TxtProva3", 1, 2, 1, 1, 10, true, 200);

formUtility.addLabel(pannello, new JLabel(), "prova4", 0, 3, 5);
formUtility.addField(pannello, new JTextField(), "TxtProva4", 1, 3, 1, 1, 10, true, 200);

formUtility.addLabel(pannello, new JLabel(), "prova5", 0, 4, 5);
formUtility.addField(pannello, new JComboBox(), "TxtProva5", 1, 4, 1, 1, 10, true, 0);

formUtility.addField(pannello, new JCheckBox(), "ChkProva6", 2, 4, 1, 1, 5, true, 0);
formUtility.addLabel(pannello, new JLabel(), "prova6", 3, 4, 100);

formUtility.addLabel(pannello, new JLabel(), "prova7", 0, 6, 5);
formUtility.addField(pannello, new JTextField(), "TxtProva7", 1, 6, 1, 1, 10, true, 200);

formUtility.addLabel(pannello, new JLabel(), "prova8", 0, 8, 5);
formUtility.addField(pannello, new JTextArea(), "Lstprova8", 1, 8, 1, 1, 1, false, 0);

formUtility.addField(pannello, new JButton("+"), "BtnProva9", 2, 8, 1, 1, 5, true, 5);
formUtility.addField(pannello, new JButton("Assegna"), "BtnProva9", 3, 8, 1, 1, 100, true, 5);

formUtility.addLabel(pannello, new JLabel(), "prova10", 0, 9, 5);
formUtility.addField(pannello, new JComboBox(), "CboProva10", 1, 9, 1, 1, 1, true, 0);

formUtility.addLabel(pannello, new JLabel(), "prova11", 0, 10, 5);
formUtility.addField(pannello, new JTextArea(), "TxtProva11", 1, 10, 1, 1, 1, true, 0);

formUtility.addField(pannello, new JButton("+"), "BtnProva12", 2, 10, 1, 1, 5, true, 5);
formUtility.addField(pannello, new JButton("Assegna"), "BtnProva13", 3, 10, 1, 1, 100, true, 5);

formUtility.addLabel(pannello, new JLabel(), "Prova14", 0, 11, 5);
formUtility.addField(pannello, new JComboBox(), "CboProva14", 1, 11, 1, 1, 1, true, 0);

formUtility.addField(pannello, new JCheckBox(), "ChkProva15", 2, 11, 1, 1, 5, true, 0);
formUtility.addLabel(pannello, new JLabel(), "Prova15", 3, 11, 100);

formUtility.addLabel(pannello, new JLabel(), "prova16", 0, 12, 5);
formUtility.addField(pannello, new JComboBox(), "CboProva16", 1, 12, 1, 1, 1, true, 0);

formUtility.addLabel(pannello, new JLabel(), "prova17", 0, 13, 5);
formUtility.addField(pannello, new JComboBox(), "CboProva17", 1, 13, 1, 1, 1, true, 0);

formUtility.addLabel(pannello, new JLabel(), "prova18", 0, 14, 5);
formUtility.addField(pannello, new JComboBox(), "CboProva18", 1, 14, 1, 1, 1, true, 0);

formUtility.addLabel(pannello, new JLabel(), "prova19", 0, 15, 5);
formUtility.addField(pannello, new JTextField(), "TxtProva19", 1, 15, 1, 1, 1, true, 0);

// formUtility.addLabel(pannello, new JLabel(), "prova20", 5, 0, 5);
// formUtility.addField(pannello, new JTextArea(), "TxtProva20", 6, 0, 1, 1, 1, true, 0);
//
// formUtility.addField(pannello, new JButton("+"), "BtnProva21", 7, 0, 1, 1, 5, true, 5);
// formUtility.addField(pannello, new JButton("Assegna"), "BtnProva22", 8, 0, 1, 1, 0, true, 5);
//
// formUtility.addLabel(pannello, new JLabel(), "prova23", 5, 1, 5);
// formUtility.addField(pannello, new JTextArea(), "TxtProva23", 6, 1, 1, 1, 1, true, 0);
//
// formUtility.addField(pannello, new JButton("+"), "BtnProva24", 7, 1, 1, 1, 5, true, 5);
// formUtility.addField(pannello, new JButton("Assegna"), "BtnProva25", 8, 1, 1, 1, 0, true, 5);
//
// formUtility.addLabel(pannello, new JLabel(), "prova26", 5, 2, 5);
// formUtility.addField(pannello, new JTextField(), "TxtProva26", 6, 2, 1, 1, 1, true, 0);
//
// formUtility.addLabel(pannello, new JLabel(), "prova27", 5, 3, 5);
// formUtility.addField(pannello, new JTextField(), "TxtProva27", 6, 3, 1, 1, 1, true, 0);
//
// formUtility.addLabel(pannello, new JLabel(), "prova28", 5, 4, 5);
// formUtility.addField(pannello, new JTextField(), "TxtProva28", 6, 4, 1, 1, 1, true, 0);
//
// Box sizeBox = Box.createVerticalBox();
// ButtonGroup group = new ButtonGroup();
// JRadioButton button1 = new JRadioButton("Si");
// JRadioButton button2 = new JRadioButton("No");
// group.add(button1);
// group.add(button2);
// sizeBox.add(button1);
// sizeBox.add(button2);
// JTextField text = new JTextField();
// sizeBox.add(text);
// text.setName("TxtCertificato");
// sizeBox.setBorder(BorderFactory.createTitledBorder("prova29"));
// formUtility.addField(pannello, sizeBox, "OptProva29", 5, 5, 3, 1, 1, true, 0);
//
// sizeBox = Box.createVerticalBox();
// group = new ButtonGroup();
// button1 = new JRadioButton("Si");
// button2 = new JRadioButton("No");
// group.add(button1);
// group.add(button2);
// sizeBox.add(button1);
// sizeBox.add(button2);
// text = new JTextField();
// text.setName("TxtSpecializzato");
// sizeBox.add(text);
// sizeBox.setBorder(BorderFactory.createTitledBorder("Prova30"));
// formUtility.addField(pannello, sizeBox, "OptProva30", 5, 6, 3, 1, 1, true, 0);
//
// formUtility.addField(pannello, new JCheckBox(), "ChkProva31", 5, 7, 1, 1, 5, true, 0);
// formUtility.addLabel(pannello, new JLabel(), "prova31", 6, 7, 0);
//
// sizeBox = Box.createVerticalBox();
// group = new ButtonGroup();
// button1 = new JRadioButton("a");
// button2 = new JRadioButton("aa");
// JRadioButton button3 = new JRadioButton("aaa");
// JRadioButton button4 = new JRadioButton("aaaa");
// group.add(button1);
// group.add(button2);
// group.add(button3);
// group.add(button4);
// sizeBox.add(button1);
// sizeBox.add(button2);
// sizeBox.add(button3);
// sizeBox.add(button4);
// text = new JTextField();
// text.setName("TxtParere");
// sizeBox.add(text);
// sizeBox.setBorder(BorderFactory.createTitledBorder("prova32"));
// formUtility.addField(pannello, sizeBox, "OptProva32", 5, 8, 3, 1, 1, true, 0);
//
// formUtility.addLabel(pannello, new JLabel(), "prova33", 5, 9, 5);
// formUtility.addField(pannello, new JTextField(), "TxtProva33", 6, 9, 1, 1, 1, true, 0);
//

frame.setContentPane(pannello);
}
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class FormUtility {
/**
* Grid bag constraints for fields and labels
*/
private GridBagConstraints gridConstraints = null;

public FormUtility() {
// Set up the constraints for the "last" field in each
// row first, then copy and modify those constraints.

// weightx is 1.0 for fields, 0.0 for labels
// gridwidth is REMAINDER for fields, 1 for labels
gridConstraints = new GridBagConstraints();

// Stretch components horizontally (but not vertically)
gridConstraints.fill = GridBagConstraints.HORIZONTAL;

// Components that are too short or narrow for their space
// Should be pinned to the northwest (upper left) corner
gridConstraints.anchor = GridBagConstraints.NORTHWEST;

// Give the "last" component as much space as possible
gridConstraints.weightx = 0.0;

// Give the "last" component the remainder of the row
gridConstraints.gridwidth = GridBagConstraints.REMAINDER;

// Add a little padding
gridConstraints.insets = new Insets(1, 1, 1, 1);

}

public void addField(Container p, Component c, String nome, int x, int y, int width, int height, int padding, boolean enabled, int w){
if(c instanceof JComboBox){
((JComboBox) c).setName(nome);
((JComboBox) c).setEnabled(enabled);
((JComboBox) c).setPreferredSize(new Dimension(100,20));
}else if(c instanceof JTextArea){
((JTextArea) c).setName(nome);
((JTextArea) c).setLineWrap(true);
((JTextArea) c).setWrapStyleWord(true);
((JTextArea) c).setLocation(20, 40);
((JTextArea) c).setEditable(false);
((JTextArea) c).setName(nome);
((JTextArea) c).setEnabled(enabled);

JScrollPane barreChat = new JScrollPane(((JTextArea) c));
barreChat.setMinimumSize(new Dimension(200, 60));
barreChat.setPreferredSize(new Dimension(200, 60));
c = barreChat;
}else if(c instanceof JTextField){
((JTextField) c).setName(nome);
((JTextField) c).setEnabled(enabled);
((JTextField) c).setPreferredSize(new Dimension(w,20));
}else if(c instanceof JCheckBox){
((JCheckBox) c).setName(nome);
((JCheckBox) c).setEnabled(enabled);
}

gridConstraints.gridx = x;
gridConstraints.gridy = y;
gridConstraints.gridwidth = width;
gridConstraints.gridheight = height;
gridConstraints.weightx = 0.0;
gridConstraints.weighty = 0.0;
gridConstraints.insets = new Insets(5, 5, 5, padding);
//gridConstraints.anchor = align;
gridConstraints.fill = GridBagConstraints.HORIZONTAL;
p.add(c, gridConstraints);
}

public void addLabel(Container p, Component c, String text, int x, int y, int padding){
((JLabel) c).setText(text);

gridConstraints.gridx = x;
gridConstraints.gridy = y;
gridConstraints.anchor = gridConstraints.CENTER;
gridConstraints.insets = new Insets(5, 5, 5, padding);
p.add(c, gridConstraints);
}
}

------------------------------------------------------------------------------------------------------------------------------

so, the layout of the previous code is good, but if I remove block comment the layout is terrible because the cells fit to the content!!!
Help me please!
What's wrong? :(

Thank you very much!

Correctly code
Message was edited by: fanny983

fanny983
Offline
Joined: 2010-03-14
Points: 0

I resolved! Thank you! :)