views:

84

answers:

3

I have a GridBagLayout, but one label (maxSizeLbl) goes crazy and an other one (maxDateLbl) isnt visible, anyone knows what I've done wrong?

Here is the Picture:

alt text

And here is the Code:

import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class SearchGUI {
    public static void main(String args[]) {
        JFrame frame = new JFrame("Search Files");
        JPanel panel = new JPanel();
        JTextField rootTF = new JTextField(20);
        JTextField containsTF = new JTextField(20);
        JTextField containsNotTF = new JTextField(20);
        JTextField minSizeTF = new JTextField(10);
        JTextField maxSizeTF = new JTextField(10);
        JTextField  suffixTF = new JTextField(10);
        JTextField  prefixTF= new JTextField(10);
        JTextField minDateTF = new JTextField(10);
        JTextField maxDateTF = new JTextField(10);
        JCheckBox hiddenCB = new JCheckBox("search hidden");
        JCheckBox recursiveCB = new JCheckBox("search subfolders");
        JButton searchBtn = new JButton("search");
        JLabel rootLbl = new JLabel("search in: ", JLabel.CENTER);
        JLabel containsLbl = new JLabel("Filename contains: ", JLabel.CENTER);
        JLabel containsNotLbl = new JLabel(" Filename contains not", JLabel.CENTER);
        JLabel minSizeLbl = new JLabel("min. Size", JLabel.CENTER);
        JLabel maxSizeLbl = new JLabel("max. Size", JLabel.CENTER);
        JLabel suffixLbl = new JLabel("Filetypes", JLabel.CENTER);
        JLabel prefixLbl = new JLabel("begins with", JLabel.CENTER);
        JLabel minDateLbl = new JLabel("min. Date", JLabel.CENTER);
        JLabel maxDateLbl = new JLabel("max Date", JLabel.CENTER);

        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 
        Container c = frame.getContentPane();
        GridBagLayout gbl = new GridBagLayout();
        c.setLayout(gbl);


//      addComponent(c, gbl, );
        //                                                              x  y  w  h  wx   wy
        addComponent(c, gbl,                rootLbl, 0, 0, 2, 1, 1.0, 0.0);
        addComponent(c, gbl,                 rootTF, 0, 1, 2, 1, 1.0, 0.0);
        addComponent(c, gbl,         containsLbl, 0, 2, 2, 1, 1.0, 0.0);
        addComponent(c, gbl,          containsTF, 0, 3, 2, 1, 1.0, 0.0);
        addComponent(c, gbl,    containsNotLbl, 0, 4, 2, 1, 1.0, 0.0);
        addComponent(c, gbl,     containsNotTF, 0, 5, 2, 1, 1.0, 0.0);
        addComponent(c, gbl,           minSizeLbl, 0, 6, 1, 1, 1.0, 0.0);
        addComponent(c, gbl,          maxSizeLbl, 1, 6, 1, 1, 1.0, 0.0);
        addComponent(c, gbl,            minSizeTF, 0, 7, 1, 1, 1.0, 0.0);
        addComponent(c, gbl,           maxSizeTF, 1, 7, 1, 1, 1.0, 0.0);
        addComponent(c, gbl,                suffixLbl, 0, 8, 2, 1, 1.0, 0.0);
        addComponent(c, gbl,                 suffixTF, 0, 9, 2, 1, 1.0, 0.0);
        addComponent(c, gbl,            minDateLbl, 0, 10, 1, 1, 1.0, 0.0);
        addComponent(c, gbl,            maxSizeLbl, 1, 10, 1, 1, 1.0, 0.0);
        addComponent(c, gbl,             minDateTF, 0, 11, 1, 1, 1.0, 0.0);
        addComponent(c, gbl,            maxDateTF, 1, 11, 1, 1, 1.0, 0.0);
        addComponent(c, gbl,                searchBtn, 0, 12, 2, 1, 1.0, 0.0);

        frame.setSize(gbl.preferredLayoutSize(c));
        frame.setVisible(true);

    }
    static void addComponent( Container cont, 
            GridBagLayout gbl, 
            Component c, 
            int x, int y, 
            int width, int height, 
            double weightx, double weighty ) 
    { 
                GridBagConstraints gbc = new GridBagConstraints(); 
                gbc.fill = GridBagConstraints.BOTH; 
                gbc.gridx = x; gbc.gridy = y; 
                gbc.gridwidth = width; gbc.gridheight = height; 
                gbc.weightx = weightx; gbc.weighty = weighty; 
                gbl.setConstraints( c, gbc ); 
                cont.add( c ); 
    } 

}

Thanks in advance.

+1  A: 

Make sure you thoroughly study the indispensable GridBagLayout tutorial at http://madbean.com/anim/totallygridbag/ first.

It seems like you don't use any IDE. Eclipse shows a curious yellow squiggly line below maxDateLbl--it's not being used. And if you click on maxSizeLbl, you'll see it's being used twice.

Replace the second maxSizeLbl with maxDateLbl.

And watch the tutorial again.

Geoffrey Zheng
+1  A: 

Including Geoffrey's discovery about maxSizeLbl/maxDateLbl, this is what worked for me and assumes the field labels occupy the left side of the text field and not the top. (Only the relevant section of code shown.)


addComponent(c, gbl,        rootLbl, 0, 0, 1, 1, 1.0, 0.0);
addComponent(c, gbl,         rootTF, 1, 0, 3, 1, 1.0, 0.0);

addComponent(c, gbl,    containsLbl, 0, 1, 1, 1, 1.0, 0.0);
addComponent(c, gbl,     containsTF, 1, 1, 3, 1, 1.0, 0.0);

addComponent(c, gbl, containsNotLbl, 0, 2, 1, 1, 1.0, 0.0);
addComponent(c, gbl,  containsNotTF, 1, 2, 3, 1, 1.0, 0.0);

addComponent(c, gbl,     minSizeLbl, 0, 3, 1, 1, 1.0, 0.0);
addComponent(c, gbl,      minSizeTF, 1, 3, 1, 1, 1.0, 0.0);
addComponent(c, gbl,     maxSizeLbl, 2, 3, 1, 1, 1.0, 0.0);
addComponent(c, gbl,      maxSizeTF, 3, 3, 1, 1, 1.0, 0.0);

addComponent(c, gbl,      suffixLbl, 0, 4, 1, 1, 1.0, 0.0);
addComponent(c, gbl,       suffixTF, 1, 4, 3, 1, 1.0, 0.0);

addComponent(c, gbl,     minDateLbl, 0, 5, 1, 1, 1.0, 0.0);
addComponent(c, gbl,      minDateTF, 1, 5, 1, 1, 1.0, 0.0);
addComponent(c, gbl,     maxDateLbl, 2, 5, 1, 1, 1.0, 0.0);
addComponent(c, gbl,      maxDateTF, 3, 5, 1, 1, 1.0, 0.0);

addComponent(c, gbl,      searchBtn, 0, 6, 4, 1, 1.0, 0.0);

jtp
I forgot to add that you may want to want to use "frame.pack();" as a replacement for the "frame.setSize(gbl.preferredLayoutSize(c));" statement, otherwise some of the viewport is clipped.
jtp
+1  A: 

You added the maxSizeLbl twice (instead of maxDateLbl)

Virat Kadaru