Reputation: 69
I have 2 JPanels with GridBagLayouts to set the layouts. The only problem is that the components are all in the centre of the Panels.
I need these clusters of components to align to the top left of the JPanels they are added to.
I know you can align components in the layout by using the anchor variable but I want the entire grid anchored to the top left of the panel.
(I exaggerated the dimensions to make it clear what is wrong)
sample tools object:
package UserInterfaces.Sample.Interfaces;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
/**
*
* @author Edward Jenkins
*/
public class SampleTools extends JPanel {
// constants
private static final Dimension VALUE_SPINNER_SIZE = new Dimension(45, 20);
private static final Dimension DETAILS_TEXT_FIELD_SIZE
= new Dimension(180, 20);
private static final Dimension SAMPLE_DETAILS_SIZE = new Dimension(305, 135);
private static final Dimension SOUND_OPTIONS_SIZE = new Dimension(355, 135);
private static final Insets DEF_INSETS = new Insets(0, 0, 0, 0);
private static final Insets TITLE_LABEL_INSETS = new Insets(5, 4, 0, 0);
private static final Insets FIELD_INSETS = new Insets(0, 4, 0, 0);
private static final Insets CHECKBOX_INSETS = new Insets(0, 48, 0, 0);
private static final Font DEF_FONT = new Font("Default font", 0, 12);
private static final Font BOLD_FONT = new Font("Bold font", 1, 12);
private static final Font ITALIC_FONT = new Font("Italic font", 2, 12);
// instance variables
// this panel
private JToolBar sammpleToolBar;
private GridBagLayout toolsLayout;
private GridBagConstraints tc;
// basics
// Sample details (File name, bitrate, channels, etc.)
private JPanel sampleDetails;
private GridBagLayout sampleDetailsLayout;
private GridBagConstraints sdc;
private Border sampleDetailsBorder;
private JLabel sampleNameLabel;
private JTextField sampleNameField;
private JLabel fileNameLabel;
private JTextField fileNameField;
private JLabel sampleFormatTitleLabel;
private JLabel sampleFormatLabel;
private JLabel sampleLengthTitleLabel;
private JLabel sampleLengthLabel;
// sound options (volume, panning, etc.)
private JPanel soundOptions;
private GridBagLayout soundOptionsLayout;
private GridBagConstraints soc;
private Border soundOptionsBorder;
private JLabel defaultVolumeLabel;
private JSpinner defaultVolumeValue;
private SpinnerModel defVolumeSpinnerModel;
private JSlider defaultVolumeSlider;
private JLabel globalVolumeLabel;
private JSpinner globalVolumeValue;
private SpinnerModel globalVolumeSpinnerModel;
private JSlider globalVolumeSlider;
private JLabel defaultPanningLabel;
private JSpinner defaultPanningValue;
private SpinnerModel panSpinnerModel;
private JSlider defaultPanningSlider;
private JLabel usePanningLabel;
private JCheckBox panning;
private JLabel useSurroundLabel;
private JCheckBox surround;
// sampling tools (definging note frequency and C5 frequency)
private JPanel samplingTools;
private JTextField c5SampleRate;
private JComboBox sampleNotesCombo;
private JSpinner fineTuneSpinner;
private JPanel loopingTools;
private JPanel vibratoOptions;
private JPanel detuningOptins;
private JPanel ADSROptions;
private JButton resampleButton;
// consturctor
public SampleTools() {
// set layout
toolsLayout = new GridBagLayout();
tc = new GridBagConstraints();
tc.anchor = GridBagConstraints.NORTHWEST;
this.setLayout(toolsLayout);
// set the panels
createSampleDetailsPanel();
createSoundOptionsPanel();
sampleDetails.setPreferredSize(SAMPLE_DETAILS_SIZE);
soundOptions.setPreferredSize(SOUND_OPTIONS_SIZE);
//createSampleToolsPanel();
}
private void createSampleDetailsPanel() {
// set the layout
sampleDetailsLayout = new GridBagLayout();
sdc = new GridBagConstraints();
sdc.anchor = GridBagConstraints.WEST;
// set the border
sampleDetailsBorder
= BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
// set the border title
sampleDetailsBorder
= BorderFactory.createTitledBorder(sampleDetailsBorder,
"Sample details", 0, 0, BOLD_FONT);
// set the sample options JPanel
sampleDetails = new JPanel(sampleDetailsLayout, false);
// set details border
sampleDetails.setBorder(sampleDetailsBorder);
// set the sample name label
sampleNameLabel = new JLabel("Sample Name: ");
sampleNameLabel.setFont(DEF_FONT);
sdc.gridx = 0;
sdc.gridy = 0;
sdc.insets = TITLE_LABEL_INSETS;
sampleDetails.add(sampleNameLabel, sdc);
// set the sample name field
sampleNameField = new JTextField("");
sampleNameField.setToolTipText("Name of sample. ");
sampleNameField.setPreferredSize(DETAILS_TEXT_FIELD_SIZE);
sdc.gridx = 1;
sdc.gridy = 0;
sdc.insets = FIELD_INSETS;
sampleDetails.add(sampleNameField, sdc);
// set the file name label
fileNameLabel = new JLabel("Sample file Name: ");
fileNameLabel.setFont(DEF_FONT);
sdc.gridx = 0;
sdc.gridy = 1;
sdc.insets = TITLE_LABEL_INSETS;
sampleDetails.add(fileNameLabel, sdc);
// set the file name field
fileNameField = new JTextField("");
fileNameField.setToolTipText("File name of sample. "
+ "Mostly used with s3m samples.");
fileNameField.setPreferredSize(DETAILS_TEXT_FIELD_SIZE);
sdc.gridx = 1;
sdc.gridy = 1;
sdc.insets = FIELD_INSETS;
sampleDetails.add(fileNameField, sdc);
// set the sample format title label
sampleFormatTitleLabel = new JLabel("Sample format: ");
sampleFormatTitleLabel.setFont(DEF_FONT);
sdc.gridx = 0;
sdc.gridy = 2;
sdc.insets = TITLE_LABEL_INSETS;
sampleDetails.add(sampleFormatTitleLabel, sdc);
// set the sample format label
sampleFormatLabel = new JLabel("Signed 16-bit, sterero ");
sampleFormatLabel.setFont(ITALIC_FONT);
sdc.gridx = 1;
sdc.gridy = 2;
sdc.insets = TITLE_LABEL_INSETS;
sampleDetails.add(sampleFormatLabel, sdc);
// set the sample length title label
sampleLengthTitleLabel = new JLabel("Sample length: ");
sampleLengthTitleLabel.setFont(DEF_FONT);
sdc.gridx = 0;
sdc.gridy = 3;
sdc.insets = TITLE_LABEL_INSETS;
sampleDetails.add(sampleLengthTitleLabel, sdc);
// set the sample length label
sampleLengthLabel = new JLabel("65535");
sampleLengthLabel.setFont(ITALIC_FONT);
sdc.gridx = 1;
sdc.gridy = 3;
sdc.insets = TITLE_LABEL_INSETS;
sampleDetails.add(sampleLengthLabel, sdc);
// add to tools;
tc.gridx = 0;
this.add(sampleDetails, tc);
}
private void createSoundOptionsPanel() {
// set the layout
soundOptionsLayout = new GridBagLayout();
soc = new GridBagConstraints();
soc.anchor = GridBagConstraints.WEST;
// set the border
soundOptionsBorder
= BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
// set the border title
soundOptionsBorder
= BorderFactory.createTitledBorder(soundOptionsBorder,
"Sound options", 0, 0, BOLD_FONT);
// set the sample options JPanel
soundOptions = new JPanel(soundOptionsLayout, false);
// set options border
soundOptions.setBorder(soundOptionsBorder);
// set the default volume label
defaultVolumeLabel = new JLabel("Default volume: ");
defaultVolumeLabel.setFont(DEF_FONT);
soc.gridx = 0;
soc.gridy = 0;
soc.insets = TITLE_LABEL_INSETS;
soundOptions.add(defaultVolumeLabel, soc);
// set default volume spinner model
defVolumeSpinnerModel = new SpinnerNumberModel(64, 0, 64, 1);
// set the default volume value spinner
defaultVolumeValue = new JSpinner(defVolumeSpinnerModel);
defaultVolumeValue.setPreferredSize(VALUE_SPINNER_SIZE);
soc.gridx = 1;
soc.gridy = 0;
soc.insets = DEF_INSETS;
soundOptions.add(defaultVolumeValue, soc);
// set the default volume slider
defaultVolumeSlider = new JSlider(0, 64, 64);
soc.gridx = 2;
soc.gridy = 0;
soundOptions.add(defaultVolumeSlider, soc);
// set the global volume label
globalVolumeLabel = new JLabel("Global volume: ");
globalVolumeLabel.setFont(DEF_FONT);
soc.gridx = 0;
soc.gridy = 1;
soc.insets = TITLE_LABEL_INSETS;
soundOptions.add(globalVolumeLabel, soc);
// set global volume spinner model
globalVolumeSpinnerModel = new SpinnerNumberModel(64, 0, 64, 1);
// set the global volume value spinner
globalVolumeValue = new JSpinner(globalVolumeSpinnerModel);
globalVolumeValue.setPreferredSize(VALUE_SPINNER_SIZE);
soc.gridx = 1;
soc.gridy = 1;
soc.insets = DEF_INSETS;
soundOptions.add(globalVolumeValue, soc);
// set the global volume slider
globalVolumeSlider = new JSlider(0, 64, 64);
soc.gridx = 2;
soc.gridy = 1;
soundOptions.add(globalVolumeSlider, soc);
// set the use panning label
usePanningLabel = new JLabel("Use panning: ");
usePanningLabel.setFont(DEF_FONT);
soc.gridx = 0;
soc.gridy = 2;
soc.insets = TITLE_LABEL_INSETS;
soundOptions.add(usePanningLabel, soc);
// set the use panning checkbox
panning = new JCheckBox();
panning.setSelected(false);
soc.gridx = 1;
soc.gridy = 2;
soc.gridwidth = 2;
soc.insets = CHECKBOX_INSETS;
soundOptions.add(panning, soc);
// set the panning spinner model
panSpinnerModel = new SpinnerNumberModel(0, -128, 127, 1);
// set the default panning label
defaultPanningLabel = new JLabel("Default panning: ");
defaultPanningLabel.setFont(DEF_FONT);
soc.gridx = 0;
soc.gridy = 3;
soc.gridwidth = 1;
soc.insets = TITLE_LABEL_INSETS;
soundOptions.add(defaultPanningLabel, soc);
// set the default panning value spinner
defaultPanningValue = new JSpinner(panSpinnerModel);
defaultPanningValue.setPreferredSize(VALUE_SPINNER_SIZE);
defaultPanningValue.setEnabled(panning.isSelected());
soc.gridx = 1;
soc.gridy = 3;
soc.insets = DEF_INSETS;
soundOptions.add(defaultPanningValue, soc);
// set the default panning slider
defaultPanningSlider = new JSlider(-128, 127, 0);
defaultPanningSlider.setEnabled(panning.isSelected());
soc.gridx = 2;
soc.gridy = 3;
soundOptions.add(defaultPanningSlider, soc);
// set the use surround label
useSurroundLabel = new JLabel("Use surround: ");
useSurroundLabel.setFont(DEF_FONT);
soc.gridx = 0;
soc.gridy = 4;
soc.insets = TITLE_LABEL_INSETS;
soundOptions.add(useSurroundLabel, soc);
// set the use surround checkbox
surround = new JCheckBox();
surround.setSelected(false);
soc.gridx = 1;
soc.gridy = 4;
soc.gridwidth = 2;
soc.insets = CHECKBOX_INSETS;
soundOptions.add(surround, soc);
// add to tools
tc.gridx = 1;
this.add(soundOptions, tc);
}
private void createSampleToolsPanel() {
}
}
Main method:
/**
*
* @author Edward Jenkins
*/
public class testUIPanel {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JFrame testFrame = new JFrame();
testFrame.setLocationRelativeTo(null);
testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
testFrame.setTitle("Test User Interface");
testFrame.add(new SampleTools());
testFrame.pack();
testFrame.setVisible(true);
}
}
Upvotes: 0
Views: 1197
Reputation: 20914
You need a combination of values for the fields of GridBagConstraints. In particular anchor
and weightx
and weighty
.
Below code is a simple example showing two rows where each row contains a JLabel
and a JTextField
. Notes after the code.
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SampleTools {
private void createAndDisplayGui() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createSampledetailsPanel());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createSampledetailsPanel() {
JPanel sampledetailsPanel = new JPanel(new GridBagLayout());
sampledetailsPanel.setPreferredSize(new Dimension(500, 300));
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets.left = 10;
gbc.insets.top = 10;
JLabel nameLabel = new JLabel("Sample Name");
sampledetailsPanel.add(nameLabel, gbc);
gbc.gridx = 1;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1.0;
JTextField nameTextField = new JTextField(10);
sampledetailsPanel.add(nameTextField, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.weightx = 0.0;
gbc.weighty = 1.0;
JLabel fileLabel = new JLabel("Sample File");
sampledetailsPanel.add(fileLabel, gbc);
gbc.gridx = 1;
JTextField fileTextField = new JTextField(10);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1.0;
sampledetailsPanel.add(fileTextField, gbc);
return sampledetailsPanel;
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new SampleTools().createAndDisplayGui());
}
}
anchor
setting puts each component in the top, left corner of its cell.weightx
to 1 for the last component in each row.gridwidth
to GridBagConstraints.REMAINDER
for the last component in each row.weighty
to 1 for components in the last row.Here is a screen capture.
Refer to How to Use GridBagLayout
Upvotes: 2