0

I have a JPanel like this:

GridBagConstraints constPanelInfo = new GridBagConstraints();
    panelInfo = new JPanel(new GridBagLayout());
    JLabel sensor1 = new JLabel("Sensor: ");
    constPanelInfo.gridx = 0;
    constPanelInfo.gridy = 0;
    constPanelInfo.weightx = 0.0;
    constPanelInfo.fill = GridBagConstraints.NONE;
    panelInfo.add(sensor1, constPanelInfo);
    constPanelInfo.gridx = 1;
    constPanelInfo.gridy = 0;
    constPanelInfo.weightx = 1.0;
    constPanelInfo.fill = GridBagConstraints.HORIZONTAL;
    campoSensor.setPreferredSize(new Dimension(100, campoSensor.getPreferredSize().height));
    panelInfo.add(campoSensor, constPanelInfo);

where campoSensor is defined as:

private JTextField campoSensor = new JTextField();

...but the JTextField is not scaled to 100 px weight as I want: it stays the same size it had before writting the setPreferredSize method. Any idea here?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Roman Rdgz
  • 12,836
  • 41
  • 131
  • 207
  • 3
    It would be better to use the `JTextField(int columns)` constructor than presume you can guess how wide this GUI element should be. – Andrew Thompson Jun 22 '11 at 10:34
  • Can you show the part of the code that adds `panelInfo` to a `JFrame`or `JDialog`? Did you call `pack()` on that frame or dialog? – jfpoilpret Jun 22 '11 at 11:35

2 Answers2

0

Try to set constPanelInfo.iPadX=100; instead of setting preferred size.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
0

Hopefully this is what you're looking for... setting

constPanelInfo.weightx = 0;

for the sensor should keep it at the specified 100px. i.e If all the weights are zero, all the extra space appears between the grids of the cell and the left and right edges.

Remember that if the weigths are set and because of

constPanelInfo.fill = GridBagConstraints.HORIZONTAL;

The campoSensor is going to fill the full width of the cell regardless of the preferred size. This width will probably be determined by the size of the JFrame that your panel is sitting in.

Steve
  • 363
  • 2
  • 11
  • Also, there's good information here: http://download.oracle.com/javase/tutorial/uiswing/layout/gridbag.html – Steve Jun 22 '11 at 10:31
  • JTextField have been resized now. Nevertheless, it is looking like: BORDER - SENSOR:......... JTextField ....... - BORDER how can i delete those spaces? – Roman Rdgz Jun 23 '11 at 07:03