File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Newbie, Swing GridLayout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Newbie, Swing GridLayout" Watch "Newbie, Swing GridLayout" New topic
Author

Newbie, Swing GridLayout

Dave Guenthner
Greenhorn

Joined: Jan 25, 2003
Posts: 4
I am struggling with correctly building a GUI form with Swing. I am using Sun One Forte Update 1 to build the form. I have a JFrame and a GridLayout. I have set up the Grid to support 2 Columns and 5 Rows. The problem is that all components are being added to the first column only?? Should I be adding panels or passing arguments when adding components to specify the column. If so, where do you find this in Sun One?
I have seen some variables in the IDE like alignmentx and alignmenty, but they seem to make no impact. I would appreciate any advice or suggestions, thanks. I have attached code.
Dave
/*
* HELP.java
*
* Created on February 4, 2003, 7:24 PM
*/
/**
*
* @author
*/
public class HELP extends javax.swing.JFrame {

/** Creates new form HELP */
public HELP() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox();
jSeparator1 = new javax.swing.JSeparator();
jLabel2 = new javax.swing.JLabel();
jComboBox2 = new javax.swing.JComboBox();
getContentPane().setLayout(new java.awt.GridLayout(5, 2));
setTitle("This is a test");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jLabel1.setText("Please make selection:");
getContentPane().add(jLabel1);
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Test", "Test2", "Test3" }));
getContentPane().add(jComboBox1);
getContentPane().add(jSeparator1);
jLabel2.setText("Please make second selection:");
getContentPane().add(jLabel2);
jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Test1", "Test2", "Test3" }));
getContentPane().add(jComboBox2);
pack();
}

/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new HELP().show();
}


// Variables declaration - do not modify
private javax.swing.JSeparator jSeparator1;
private javax.swing.JComboBox jComboBox2;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel1;
// End of variables declaration

}


Daveg
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1124

Hi Dave,
Unless I'm missing something, you are setting up a grid of 10 (ten) cells, but you are only adding 5 (five) components. Obviously, the way "GridLayout" works is to fill the first five cells in the grid -- which happen to be the cells in the first column.
Obviously this isn't what you want. So what do you want?
Even though the comment in the generated code warns you not to edit it, you can -- as long as you don't go back to the GUI builder/designer _after_ you've edited the code. So maybe define your grid as having one column only? [Just a suggestion.]
Hope this helps.
Good Luck,
Avi.
Dave Guenthner
Greenhorn

Joined: Jan 25, 2003
Posts: 4
Thanks for the response. I see what your saying. I kept adding components until it begain to fill blocks in the second column. So, my next question is, when I add a Jbutton for example, by default, it spans the entire block space. How can I manage to edit the dimensions so the button is much smaller and looks better. Some of the options in the IDE are maximum size, minimum size, preferred size. What should I focus on as far as managing size of components within a block? thanks in advance.
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1124

Dave,
The "GridLayout" makes each component large enough to fill the entire cell it is in -- that's why the "JButton" is big. As far as I know, you have two options:
1. Use a different layour manager -- "GridBagLayout" may be more suitable.
2. Put your "JButton" in a "JPanel" (which uses the "FlowLayout" layout manager -- that respects the "JButton"'s preferred size [by default]) and add the "JPanel" (with the "JButton" in it) to the "GridLayout".
Have you looked at the GUI trail in the "Java Tutorial":
http://java.sun.com/docs/books/tutorial/uiswing/
Or this tutorial (about layout managers):
http://developer.java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/
Hope this helps.
Good Luck,
Avi.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Newbie, Swing GridLayout