I am working on a GUI for a project at school. I am using a GridBagLayout in swing.
I want to have a label indicating the input(a type of file @ x = 0, y = 0), followed by another label(the actual file name once selected @ x = 1, y = 0
), followed by a browse button for a file chooser( @ x = 2, y = 0
). The label at (1,0)
is initially blank, however I want the area that the text will occupy to take up some space when the label contains no text. I also want the space between the label at (0,0)
and the button at (2,0)
to remain constant.
To achieve this, I'm trying to put the label onto a panel and then play with the layouts. However I can't seam to achieve the desired results. Could anyone offer some suggestions? The next three rows of the GridBagLayout will be laid out exactly the same way.
Here is a link to a screen shot of the GUI.
calibrationFileSelectionValueLabel = new JLabel("",Label.LEFT);
calibrationFileSelectionValueLabel.setName("calibrationFileSelection");
calibrationFileSelectionValueLabel.setMinimumSize(new Dimension(100,0));
calibrationFileSelectionValuePanel = new JPanel();
calibrationFileSelectionValuePanel.setBorder(BorderFactory.createEtchedBorder());
calibrationFileSelectionValuePanel.add(calibrationFileSelectionValueLabel);
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.NONE;
filesPanel.add(calibrationFileLabel,c);
c.gridy = 1;
filesPanel.add(frequencyFileLabel,c);
c.gridy = 2;
filesPanel.add(sampleFileLabel,c);
c.gridy = 3;
filesPanel.add(outputFileLabel,c);
c.gridx = 1;
c.gridy = 0;
c.fill = GridBagConstraints.BOTH;
// filesPanel.add(calibrationFileSelection,c);
filesPanel.add(calibrationFileSelectionValuePanel,c);
c.gridy = 1;
// filesPanel.add(frequencyFileSelection,c);
filesPanel.add(frequencyFileSelectionValueLabel,c);
c.gridy = 2;
// filesPanel.add(sampleFileSelection,c);
filesPanel.add(sampleFileSelectionValueLabel,c);
c.gridy = 3;
// filesPanel.add(outputFileSelection,c);
filesPanel.add(outputFileSelectionValueLabel,c);
c.gridx = 2;
c.gridy = 0;
c.fill = GridBagConstraints.NONE;
filesPanel.add(calibrationFileSelectionButton,c);
c.gridy = 1;
filesPanel.add(frequencyFileSelectionButton,c);
c.gridy = 2;
filesPanel.add(sampleFileSelectionButton,c);
c.gridy = 3;
filesPanel.add(createOutputFileButton,c);
panelForFilesPanelBorder = new JPanel();
panelForFilesPanelBorder.setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(5,10,5,10), BorderFactory.createEtchedBorder()));
panelForFilesPanelBorder.add(filesPanel);
buttonsPanel = new JPanel();
buttonsPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
buttonsPanel.setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(5,10,10,10), BorderFactory.createEtchedBorder()));
buttonsPanel.add(startButton);
buttonsPanel.add(stopButton);
basePanel.add(panelForFilesPanelBorder);
basePanel.add(numericInputPanel);
basePanel.add(buttonsPanel);
setMinimumSize(Dimension d)
which should do what you want. – WidescreenGridBagLayout
setting minimum size only, will not work. Check my answer for details and point me out if anything i have explained is wrong :) – Wheatworm