So have a JFrame which contains 2 JPanels and one Button using the GridBagLayout. But the problem lies with the Panels which also use GridBagLayouts. JPanel p_addIng has has a 3x2 Grid with two JTextFields in the first column one the the size of the first is fine but the second one is way too thin.
Some disclaimers ahead: ing stands for ingredient, gbc for GridBagConstraints, everything with b_var means its a swing component b = JButton, tf = JTextField, p = JPanel, cb = JCombobox.
What i've already tried because of other posts:
gbc_addIng.fill = GridBagConstraints.HORIZONTAL; gbc_addIng.weightx = 1;
i saw also somewhere to use the pack() method but this was for the JFrame and didn't do anything to fix the problem.
Here's the code:
//making a new panel
//initializing the new Panel
p_addIng = new JPanel();
p_addIng.setName("Adding an Ingredient");
p_addIng.setPreferredSize(ingPanelDim);
p_addIng.setLayout(new GridBagLayout());
GridBagConstraints gbc_ing = new GridBagConstraints(0, 0, 1, 1, 1, 0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH
new Insets(0,0,0,0),10, 10);
//the components of the panel
//TextField to set the name of the Ingredient
gbc_ing.gridx = 0; //the position of the component on the panel
gbc_ing.gridy = 0;
tf_ingName = new JTextField();
tf_ingName.setPreferredSize(new Dimension(ingPanelDim.width / 2, ingPanelDim.height/2));
tf_ingName.addActionListener(this);
p_addIng.add(tf_ingName, gbc_ing); //adding the component to the Panel
//endOfTextFieldIngName
gbc_ing.gridx = 1;
gbc_ing.gridy = 0;
//button to add the ingredient
tf_amnt = new JTextField("Choose amount");
tf_amnt.setPreferredSize(new Dimension(ingPanelDim.width/4, ingPanelDim.height/2));
tf_amnt.addActionListener(this);
p_addIng.add(tf_amnt, gbc_ing);
//endOfButtonToAddIng
//button to add the ingredient
gbc_ing.gridx = 2;
gbc_ing.gridy = 0;
UnitOfMeasurement[] un = {UnitOfMeasurement.g, UnitOfMeasurement.kg, UnitOfMeasurement.Stück, UnitOfMeasurement.L, UnitOfMeasurement.ml, UnitOfMeasurement.cm};
cb_measurement = new JComboBox<UnitOfMeasurement>(un);
cb_measurement.setPreferredSize(new Dimension(ingPanelDim.width/4, ingPanelDim.height/2));
cb_measurement.addActionListener(this);
p_addIng.add(cb_measurement, gbc_ing);
//endOfButtonToAddIng
//button to add the ingredient
gbc_ing.gridx = 0;
gbc_ing.gridy = 1;
b_addIng = new JButton("Add Ingredient");
b_addIng.setPreferredSize(new Dimension(ingPanelDim.width/2, ingPanelDim.height/2));
b_addIng.addActionListener(this);
p_addIng.add(b_addIng, gbc_ing);
//endOfButtonToAddIng
//button to remove the ingredient
gbc_ing.gridx = 2;
gbc_ing.gridy = 1;
b_removeIng = new JButton("Remove Ingredient");
b_removeIng.setPreferredSize(new Dimension(ingPanelDim.width/2, ingPanelDim.height/2));
b_removeIng.addActionListener(this);
p_addIng.add(b_removeIng, gbc_ing);
//endOfButtonToRemoveIng
//
frame.pack();
frame.add(p_addIng,gbc_frame);
//
Here ,as wished, is the fully functional class the only thing thats missing is the enumeration UnitOfMeasurement which contains the elements as shown in the list for cb_meausrement:
package WindowDesign;
import Food.UnitOfMeasurement;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
public class StartingWindow implements ActionListener {
//window dimension
private static final int WIDTH = 500;
private static final int HEIGHT = 200;
//
//the JFrame
private JFrame frame;
//JComponents
//adding an Ingredient
private JPanel p_addIng;
private Dimension ingPanelDim = new Dimension(WIDTH/2, HEIGHT/2);
private JButton b_addIng; //a button to add an Ingredient
private JButton b_removeIng;
private JTextField tf_ingName;
private JTextField tf_amnt;
private JComboBox<UnitOfMeasurement> cb_measurement;
//
//adding a Recipe
private JPanel p_addRecipe;
private JButton b_addRecipe; // add a Recipe to the list of Recipes
private JButton b_removeRecipe;
private JButton b_buildRecipe;
private JButton b_clearRecipe;
private JTextField tf_recipeName;
private JTextField tf_ingredient;
private JComboBox<UnitOfMeasurement> cb_measurementR;
//
//
private JButton b_findCombination; //find the right combination of things to buy
private JButton b_exit; //exit the program
public StartingWindow(String name) {
frame = new JFrame(name);
frame.setLayout(new GridBagLayout());
//constraints
GridBagConstraints gbc_frame = new GridBagConstraints();
frame.setSize(WIDTH, HEIGHT);
frame.setBackground(Color.LIGHT_GRAY);
gbc_frame.ipadx = 10;
gbc_frame.ipady = 10;
gbc_frame.weightx = 1;
gbc_frame.fill = GridBagConstraints.BOTH;
//Adding the Panels
gbc_frame.gridx = 0;
gbc_frame.gridy = 0;
implementIngredientPanel(gbc_frame);
gbc_frame.gridx += 1;
gbc_frame.gridy = 0;
implementRecipePanel(gbc_frame);
//
gbc_frame.gridx = 0;
gbc_frame.gridy = 1;
implementStartingPanel(gbc_frame);
/*
b_exit = new JButton("exit");
b_exit.addActionListener(this);
*/
/*
frame.add(b_findCombination);
frame.add(b_addIng);
frame.add(b_addRecipe);
frame.add(b_exit);
*/
frame.setResizable(false);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.requestFocus();
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
//helper function to organize code
private void implementIngredientPanel(GridBagConstraints gbc_frame){
//making a new panel
//initializing the new Panel
p_addIng = new JPanel();
p_addIng.setName("Adding an Ingredient");
p_addIng.setPreferredSize(ingPanelDim);
p_addIng.setLayout(new GridBagLayout());
GridBagConstraints gbc_ing = new GridBagConstraints(0, 0, 1, 1, 1, 0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0,0,0,0),10, 10);
//the components of the panel
//TextField to set the name of the Ingredient
gbc_ing.gridx = 0; //the position of the component on the panel
gbc_ing.gridy = 0;
tf_ingName = new JTextField();
tf_ingName.setPreferredSize(new Dimension(ingPanelDim.width / 2, ingPanelDim.height/2));
tf_ingName.addActionListener(this);
p_addIng.add(tf_ingName, gbc_ing); //adding the component to the Panel
//endOfTextFieldIngName
gbc_ing.gridx = 1;
gbc_ing.gridy = 0;
//button to add the ingredient
tf_amnt = new JTextField("Choose amount");
tf_amnt.setPreferredSize(new Dimension(ingPanelDim.width/4, ingPanelDim.height/2));
tf_amnt.addActionListener(this);
p_addIng.add(tf_amnt, gbc_ing);
//endOfButtonToAddIng
//button to add the ingredient
gbc_ing.gridx = 2;
gbc_ing.gridy = 0;
UnitOfMeasurement[] un = {UnitOfMeasurement.g, UnitOfMeasurement.kg, UnitOfMeasurement.Stück, UnitOfMeasurement.L, UnitOfMeasurement.ml, UnitOfMeasurement.cm};
cb_measurement = new JComboBox<UnitOfMeasurement>(un);
cb_measurement.setPreferredSize(new Dimension(ingPanelDim.width/4, ingPanelDim.height/2));
cb_measurement.addActionListener(this);
p_addIng.add(cb_measurement, gbc_ing);
//endOfButtonToAddIng
//button to add the ingredient
gbc_ing.gridx = 0;
gbc_ing.gridy = 1;
b_addIng = new JButton("Add Ingredient");
b_addIng.setPreferredSize(new Dimension(ingPanelDim.width/2, ingPanelDim.height/2));
b_addIng.addActionListener(this);
p_addIng.add(b_addIng, gbc_ing);
//endOfButtonToAddIng
//button to remove the ingredient
gbc_ing.gridx = 2;
gbc_ing.gridy = 1;
b_removeIng = new JButton("Remove Ingredient");
b_removeIng.setPreferredSize(new Dimension(ingPanelDim.width/2, ingPanelDim.height/2));
b_removeIng.addActionListener(this);
p_addIng.add(b_removeIng, gbc_ing);
//endOfButtonToRemoveIng
//
frame.pack();
frame.add(p_addIng,gbc_frame);
//
}
private void implementRecipePanel(GridBagConstraints gbc_frame){
//Adding a Panel to add
p_addRecipe = new JPanel();
p_addRecipe.setName("Adding an Ingredient");
p_addRecipe.setPreferredSize(new Dimension(WIDTH/2, HEIGHT/2));
p_addRecipe.setLayout(new GridBagLayout());
GridBagConstraints gbc_recipe = new GridBagConstraints();
//the components:
//add Button to Add a Recipe
b_addRecipe = new JButton("Add Recipe");
b_addRecipe.addActionListener(this);
p_addRecipe.add(b_addRecipe, gbc_recipe );
//
frame.add(p_addRecipe, gbc_frame);
}
private void implementStartingPanel(GridBagConstraints c){
b_findCombination = new JButton("go shopping!");
b_findCombination.addActionListener(this);
c.fill = GridBagConstraints.HORIZONTAL;
frame.add(b_findCombination, c);
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
}
}