• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Newbie, Swing GridLayout

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

}
 
Ranch Hand
Posts: 1143
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1143
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Let me tell you a story about a man named Jed. He made this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic