GridBagLayout: alignment and width when gridwidth is > 1 - java

In this JFrame with a GridBagLayout with 4 columns, the brown line should be the limit between columns 1 and 2, and OK and Cancel buttons should be on each side of this limit:
The problems:
OK + Cancel set is not centered with other buttons.
Left and right JTextArea don't have the same width.
Column 1 seems to have a zero width when I was expecting columns 1 and 2 to be equal.
The code used:
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class GblSO extends JFrame {
// Instance variables
GridBagConstraints gbc = new GridBagConstraints();
public GblSO() {
// Set frame
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridBagLayout());
// Text areas
JTextArea left = new JTextArea("Left!");
JTextArea right = new JTextArea("Right!");
setConstraints(1, 1, GridBagConstraints.BOTH, null);
addToFrame(left, 0, 1, 1, 5, GridBagConstraints.CENTER);
addToFrame(right, 3, 1, 1, 5, GridBagConstraints.CENTER);
// Transfer buttons
JButton addBtn = new JButton(">");
JButton rmvBtn = new JButton("<");
setConstraints(0, 0, GridBagConstraints.NONE, new Insets(3, 5, 3, 5));
addToFrame(addBtn, 1, 1, 2, 1, GridBagConstraints.CENTER);
addToFrame(rmvBtn, 1, 3, 2, 1, GridBagConstraints.CENTER);
// OK / Cancel buttons
JButton okBtn = new JButton("OK");
JButton canBtn = new JButton("Cancel");
setConstraints(0, 0, GridBagConstraints.NONE, new Insets(15, 4, 15, 4));
addToFrame(okBtn, 0, 6, 2, 1, GridBagConstraints.EAST);
addToFrame(canBtn, 2, 6, 2, 1, GridBagConstraints.WEST);
// Show
pack();
setVisible(true);
}
private void setConstraints(double weightx, double weighty, int fill, Insets insets) {
gbc.weightx = weightx; // how much cell resizes
gbc.weighty = weighty; // "
gbc.fill = fill; // how component fills cell
gbc.insets = (insets == null ? new Insets(0, 0, 0, 0) : insets);
}
private void addToFrame(Component comp,
int gridx, int gridy, int gridwidth, int gridheight, int anchor) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.anchor = anchor;
add(comp, gbc);
}
public static void main(String[] args) {
new GblSO();
}
}
For a test only: If I add the > and < buttons to the JFrame respectively in column 1 and 2, and don't span multiple columns, columns 1 and 2 are forced to have the same width and the bottom buttons set is now centered.
Code changed:
addToFrame(addBtn, 1, 1, 1, 1, GridBagConstraints.CENTER);
addToFrame(rmvBtn, 2, 3, 1, 1, GridBagConstraints.CENTER);
Result:
The two JTextArea have still a different width :-(, and obviously > and < are not going to be aligned!
How can I solve this problem so that buttons are centered, and the two JTextArea have the same width? Thanks by advance for your help.
(this code is inspired by this tutorial)

That is because you don't have any components added on each column, which is somehow needed for GridBagLayout to work as one would expect.
A few remarks now:
Did you noticed in the tutorial you linked to, the OK and Cancel buttons are aligned with the JTextArea, not the overall upper components?
When working with GridBagLayout it is better to instanciate a new GridBagConstraint for each component you're adding. It prevents you from forgetting to reset an attribute somewhere, which is difficult to troubleshoot.
Then, if you want your OK and Cancel buttons to be center-aligned with your upper components, the easiest would be to create two JPanel: one with the upper components, the other one with your buttons. You can still use GridBagLayout for the upper panel and the default one (FlowLayout) for the buttons. You would put them into a BorderLayout position CENTER and SOUTH and you'd be good to go. Note that the panels would be center-aligned, not the space between the OK / Cancel buttons and the < / > buttons.
Back to the solution to your problem: you have to add "empty" components (Container, JPanel, emtpy JLabel, ...) on the first (or last) line with weightx set to 1.0 so the cells are actually filled in X direction, and weighty set to 0.0 so they are not visible in Y direction (and vice-versa if you want to use gridheight instead of gridwidth).
...
setLayout(new GridBagLayout());
setConstraints(1, 0, GridBagConstraints.BOTH, null);
addToFrame(new Container(), 0, 0, 1, 1, GridBagConstraints.CENTER);
addToFrame(new Container(), 1, 0, 1, 1, GridBagConstraints.CENTER);
addToFrame(new Container(), 2, 0, 1, 1, GridBagConstraints.CENTER);
addToFrame(new Container(), 3, 0, 1, 1, GridBagConstraints.CENTER);
// Text areas
...
That way the cells will exist and you'll get the expected result.

Related

GridBag not displaying all components in row

I've implemented a JPanel using a GridBagLayout as follows:
fileSelectionDetails = new JPanel();
fileSelectionGridBagLayout = new GridBagLayout();
fileSelectionDetails.setLayout(fileSelectionGridBagLayout);
JLabel lblFile1 = new JLabel("File 1:");
JTextField txtFile1Path = new JTextField();
JButton btnBrowseFile1 = new JButton("Browse...");
addComponentToFileSelectionGrid(lblFile1, 0, 0, 1, 1, 20, 100, GridBagConstraints.NONE, GridBagConstraints.WEST);
addComponentToFileSelectionGrid(txtFile1Path, 1, 0, 3, 1, 60, 100, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
addComponentToFileSelectionGrid(btnBrowseFile1, 2, 0, 1, 1, 20, 100, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
private void addComponentToFileSelectionGrid(Component component, int gridX, int gridY,
int gridWidth, int gridHeight, int weightX,
int weightY, int fill, int anchor) {
GridBagConstraints constraint = new GridBagConstraints();
constraint.gridx = gridX;
constraint.gridy = gridY;
constraint.gridwidth = gridWidth;
constraint.gridheight = gridHeight;
constraint.weightx = weightX;
constraint.weighty = weightY;
constraint.fill = fill;
constraint.anchor = anchor;
fileSelectionGridBagLayout.setConstraints(component, constraint);
fileSelectionDetails.add(component);
}
I want to see my components laid out as follows:
However, what I'm actually seeing is:
i.e. the 'Browse...' button is missing! Why is this?
From your drawing, I’m guessing you don’t want relative widths at all. It appears you want the label and button to be their preferred sizes, and the JTextField to stretch to take up all of the width not used by the label and button.
As camickr suggested, you should give the JTextField a meaningful preferred size by initializing it with a column count, like new JTextField(20).
You can then take advantage of some useful aspects of GridBagLayout and GridBagConstraints:
The default value of gridx and gridy is RELATIVE, which means each component you add is placed to the right of the last one added. Which just happens to be exactly what you want. Therefore, you should not set gridx or gridy at all.
The default value of gridwidth and gridheight is 1. This is what you want. GridBagLayout cells are flexible, so setting one component’s gridwidth to 3 does not make it three times wider than a component whose gridwidth is 1. The width of a cell, or span of cells, depends entirely on what it contains.
When you add a component to a GridBagLayout, the GridBagConstraints object is cloned inside the GridBagLayout. This means you can safely reuse the same GridBagConstraints object over and over, changing just the fields that need to change.
With this knowledge, your code can be simplified to:
fileSelectionDetails = new JPanel(new GridBagLayout());
JLabel lblFile1 = new JLabel("File 1:");
JTextField txtFile1Path = new JTextField(20);
JButton btnBrowseFile1 = new JButton("Browse\u2026");
txtFile1Path.setMinimumSize(txtFile1Path.getPreferredSize());
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.weight = 0;
fileSelectionDetails.add(lblFile1, constraints);
constraints.weight = 1;
fileSelectionDetails.add(txtFile1Path, constraints);
constraints.weight = 0;
fileSelectionDetails.add(btnBrowseFile1, constraints);
You define gridWidth=3 for txtFile1Path but add btnBrowseFile1 at gridX=2. You must set addComponentToFileSelectionGrid(btnBrowseFile1, 4 ...etc.

setting componenets by using GridBagLayout

I am using GridBagLayout to locate components on panel but it is not working like it has to be. Location of components is not affecting by changing x and y values somebody please help explain what mistake i am making here Thanks in advance :)
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.*;
public class LMS extends JFrame {
JPanel mainPanel;
JLabel RegNum, Name, FatherNam, MotherNam, DateBirth, BloodGrp, Email, Gender, RegDate, Desig, photo;
JTextField RegNuumText, FatherNamText, MotherNamText, EmailText, DesigText;
JList BloodGrpList;
JSpinner DateSpi;
// Constructor
public LMS() {
this.setTitle("Library Managment System");
this.setVisible(true);
this.setSize(700, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
// this.setResizable(false);
mainPanel = new JPanel();
// GridBagLayout bag = new GridBagLayout();
mainPanel.setLayout(new GridBagLayout());
// Creating Labels
RegNum = new JLabel("Registration Number");
Name = new JLabel("Full Name");
FatherNam = new JLabel("Father's Name");
MotherNam = new JLabel("Mother's Name");
DateBirth = new JLabel("Date Of Birth");
BloodGrp = new JLabel("Blood Group");
Email = new JLabel("Email");
Gender = new JLabel("Gender");
RegDate = new JLabel("Registration Date");
Desig = new JLabel("Designation");
photo = new JLabel("Photo");
// creating Text Fields
RegNuumText = new JTextField(30);
FatherNamText = new JTextField();
MotherNamText = new JTextField();
EmailText = new JTextField();
DesigText = new JTextField();
// mainPanel.add(RegNum);
addComp(mainPanel, RegNum, 0, 0, 2, 1, GridBagConstraints.EAST, GridBagConstraints.NONE);
addComp(mainPanel, RegNuumText, 0, 1, 2, 1, GridBagConstraints.WEST, GridBagConstraints.NONE);
this.add(mainPanel);
}
private void addComp(JPanel thePanel, JComponent comp, int xPos,
int yPos, int compWidth, int compHeight, int place, int stretch) {
GridBagConstraints gridConstraints = new GridBagConstraints();
gridConstraints.gridx = xPos;
gridConstraints.gridy = yPos;
gridConstraints.gridwidth = compWidth;
gridConstraints.gridheight = compHeight;
gridConstraints.weightx = 1;
gridConstraints.weighty = 1;
gridConstraints.insets = new Insets(5, 5, 5, 5);
gridConstraints.anchor = place;
gridConstraints.fill = stretch;
thePanel.add(comp, gridConstraints);
}
public static void main(String[] args) {
new LMS();
}
}
GridBagLayout is not the easiest one, you have to play a little with it. Maybe these lines help you to achieve what you wanted. They will put the label in the upper left corner and the Textfield right behind it.
Note, that there are panels defined as place holders - they will fill the empty space.
addComp(mainPanel, RegNum, 0, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE);
addComp(mainPanel, RegNuumText, 1, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE);
addComp(mainPanel, new JPanel(), 2, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL);
addComp(mainPanel, new JPanel(), 0, 2, 1, 1, GridBagConstraints.WEST, GridBagConstraints.VERTICAL);
You also need to change your helper method a little:
gridConstraints.weightx = stretch == GridBagConstraints.NONE || stretch == GridBagConstraints.VERTICAL ? 0 : 1;
gridConstraints.weighty = stretch == GridBagConstraints.NONE || stretch == GridBagConstraints.HORIZONTAL ? 0 : 1;
I think this should do the trick. Maybe you should define two or three helper methods, so you don't need to set all parameters every time. From my experience, very rarely you will need to define the anchor and only some times you might want to stretch components. Most of the time, for me, it is just puting stuff into the right bags (x, y, sometimes: width, height).
Edit: maybe it would help to put setVisible() at the end of the definition, not the beginning.

I want to add separate panels (using JPanel) in my code using gridbag layout .Can anyone one suggest me with the right code.Thanks in advance

Hi I am unable to add panels separately to the components.Can you help me with the right code please.
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.*;
public class Example3 extends JPanel
{
GridBagConstraints constraints = new GridBagConstraints();
public Example3()
{
setLayout(new GridBagLayout());
constraints.weightx = 1.0;
constraints.weighty = 1.0;
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.insets=new Insets(5,5,5,5);
int x, y;
// Iwant to add panel to this section to the left side//
constraints.gridheight = 1;
addGB(new JLabel("label1"), x = 0, y = 0);
addGB(new JLabel("label2"), x = 0, y = 1);
addGB(new JLabel("label3"), x = 0, y = 2);
addGB(new JLabel("label4"), x = 0, y = 3);
addGB(new JLabel("label5"), x = 0, y = 4);
//i want another panel to add to this section to the bottom//
constraints.gridwidth=2;
addGB(new JTextField("txt1"), x=1, y=0);
addGB(new JButton("btn1"), x=1, y=1);
addGB(new JRadioButton("no"), x=1, y=2);
addGB(new JRadioButton("no"), x=1, y=2);
addGB(new JComboBox(), x=1, y=3);
addGB(new JTextField("txt3"), x=1, y=4);
addGB(new JButton("OK"), x=1, y=5);
//I want to add panels to this section in the center//
addGB(new JCheckBox("chk1"), x=3, y=0);
addGB(new JCheckBox("chk2"), x=3, y=1);
addGB(new JTextArea("txtar1"), x=3, y=2);
addGB(new JRadioButton("rbtn2"), x=3, y=3);
addGB(new JComboBox(), x=3, y=4);
addGB(new JButton("CANCEL"), x=3, y=5);
//I want to add panel to this section to right side//
addGB(new JCheckBox("chk3"), x=5, y=0);
addGB(new JCheckBox("chk4"), x=7, y=0);
}
void addGB(Component component, int x, int y)
{
constraints.gridx = x;
constraints.gridy = y;
add(component, constraints);
}
public static void main(String[] args)
{
JFrame frame = new JFrame("Welcome to Example3");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize(200, 200);
frame.setLocation(200, 200);
frame.setContentPane(new Example3());
frame.setVisible(true);
}
}
I want to add panels to labels separately and checkboxes separately using gridbag layout,JPanel.Can anyone suggest me with the right code please.
If you wont just to add JPanel you can add this code in your main
JFrame frame = new JFrame("Welcome to Example3");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
.....
//you miss those line
JPanel p = new JPanel();
frame.getContentPane().add(p);
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class PizzaGridBagLayout extends JFrame {
public static void main(String[] args) {
new PizzaGridBagLayout();
}
JTextField name = new JTextField(10), phone = new JTextField(10);JComboBox address = new JComboBox(new String[]{"ComboBox 1","hi","hello"});
JComboBox address1 = new JComboBox(new String[]{"ComboBox 2","hi","hello"});
JButton labels=new JButton("labels");
JLabel label1 = new JLabel("label1");
JLabel label2 = new JLabel("label2");
JLabel label3 = new JLabel("Label3");
JLabel label4 = new JLabel("Label4");
JLabel label5 = new JLabel("Label5");
JRadioButton yes = new JRadioButton("yes"),
no = new JRadioButton("no");
JCheckBox box1 = new JCheckBox("box1"), box2 = new JCheckBox("box2"),
box3 = new JCheckBox("box3");
JButton okButton = new JButton("OK"), closeButton = new JButton("Close");
public PizzaGridBagLayout() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridBagLayout());
//addItem(panel1, new JLabel(""), 0, 0, 1, 1, GridBagConstraints.CENTER);
// addItem(panel1, new JLabel(""), 1, 1, 1, 1, GridBagConstraints.CENTER);
// addItem(panel1, new JLabel(""), 1, 2, 1, 1, GridBagConstraints.CENTER);
addItem(panel1, name, 1, 2, 1, 0, GridBagConstraints.CENTER);
addItem(panel1, phone, 1, 2, 1, 1, GridBagConstraints.CENTER);
addItem(panel1, address, 1, 4, 1, 0, GridBagConstraints.CENTER);
addItem(panel1, address1, 1, 6, 1, 0, GridBagConstraints.CENTER);
Box sizeBox = Box.createVerticalBox();
//ButtonGroup sizeGroup = new ButtonGroup();
//sizeGroup.add(label1);
//sizeGroup.add(label2);
//sizeGroup.add(label3);
//sizeGroup.add(label4);
//sizeGroup.add(label5);
sizeBox.add(label1);
sizeBox.add(label2);
sizeBox.add(label3);
sizeBox.add(label4);
sizeBox.add(label5);
sizeBox.setBorder(BorderFactory.createTitledBorder(""));
addItem(panel1, sizeBox, 0, 3, 1, 1, GridBagConstraints.NORTH);
addItem(panel1, label1, 0, 3, 1, 1,GridBagConstraints.NORTH);
addItem(panel1, label2, 0, 4, 1, 1,GridBagConstraints.NORTH);
addItem(panel1, label3, 0, 5, 1, 1,GridBagConstraints.NORTH);
addItem(panel1, label4, 0, 6, 1, 1,GridBagConstraints.NORTH);
addItem(panel1, label5, 0, 7, 1, 1,GridBagConstraints.NORTH);
/*Box sizeBox1 = Box.createVerticalBox();
ButtonGroup sizeGroup1 = new ButtonGroup();
sizeBox1.add(labels);
addItem(panel1, sizeBox1, 0, 1, 1, 0, GridBagConstraints.NORTH);*/
Box styleBox = Box.createVerticalBox();
// ButtonGroup styleGroup = new ButtonGroup();
// styleGroup.add(yes);
// styleGroup.add(no);
styleBox.add(yes);
styleBox.add(no);
styleBox.setBorder(BorderFactory.createTitledBorder(""));
addItem(panel1, styleBox, 1, 3, 1, 1, GridBagConstraints.NORTH);
Box topBox = Box.createVerticalBox();
ButtonGroup topGroup = new ButtonGroup();
//topGroup.add(box1);
//topGroup.add(box2);
//topGroup.add(box3);
topBox.add(box1);
topBox.add(box2);
//topBox.add(box3);
topBox.setBorder(BorderFactory.createTitledBorder(""));
addItem(panel1, topBox, 2, 3, 1, 1, GridBagConstraints.NORTH);
Box buttonBox = Box.createHorizontalBox();
buttonBox.add(okButton);
addItem(panel1, buttonBox, 1, 6, 1, 1, GridBagConstraints.NORTH);
buttonBox.add(Box.createHorizontalStrut(20));
buttonBox.add(closeButton);
addItem(panel1, buttonBox, 2, 7, 1, 1, GridBagConstraints.NORTH);
this.add(panel1);
this.pack();
this.setVisible(true);
}
private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align) {
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = x;
gc.gridy = y;
gc.gridwidth = width;
gc.gridheight = height;
gc.weightx = 100.0;
gc.weighty = 100.0;
gc.insets = new Insets(2, 2, 2, 2);
gc.anchor = align;
gc.fill = GridBagConstraints.NONE;
p.add(c, gc);
}
}

About using GridBagLayout in Java

I'm learning how to use GridBagLayout. I created two buttons in a JFrame. I tried making it that one of them occupies one collumn (the default), and the other two collumns, thus being twice the size of the first one (I know I can acheive this using setPrefferredSize, but my intention is to learn how to use gridwidth and gridheight).
What's the problem? Thanks
import java.awt.*;
import javax.swing.*;
public class Main extends JFrame {
Main(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500,500);
JPanel panel1 = new JPanel(new GridBagLayout());
JButton b1,b2;
b1 = new JButton("button 1");
b2 = new JButton("button 2");
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridwidth = 1;
panel1.add(b1);
gbc.gridx = 2;
gbc.gridwidth = 2;
panel1.add(b2);
add(panel1);
setVisible(true);
}
public static void main(String[]args){
Main m = new Main();
}
}
It doesn't matter how many columns the second button's width.
Actually both buttons will be asked for their preferred width and the width will be set to them if it's enough space for them.
If it's less space then min width is used.
If there is extra space it's distributed between controls according to weights proportions.
You can try to set iPadX=100 for the first and iPadx=200 and set proportion iPadX=1 for the first and iPadx=2 for the second.
The problem is that all the columns of a GridBagLayout don't have the same width. The widths are computed based on the preferred size of the components they contain. So, you could use 3, 4 or 100 as the gridwidth for the second button, it wouldn't change anything.
You need to use fillx and weightx to change the way the buttons resize.
Try using GridBagConstraint in this way, hope this will help you.
import java.awt.*;
import javax.swing.*;
public class Main extends JFrame {
Main(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500,500);
JPanel panel1 = new JPanel(new GridBagLayout());
JButton b1,b2;
b1 = new JButton("button 1");
b2 = new JButton("button 2");
panel1.add(b1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
panel1.add(b2, new GridBagConstraints(1, 0, 1, 1, 2.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
add(panel1);
setVisible(true);
}
public static void main(String[]args){
Main m = new Main();
}
}
EDIT
Or change in your code Like this below:
gbc.gridx = 1;
gbc.gridwidth = 1;
panel1.add(b1, gbc);
gbc.gridx = 2;
gbc.gridwidth = 2;
gbc.fill = gbc.HORIZONTAL; //set fill property to HORIZONTAL
gbc.weightx= 2.0;
panel1.add(b2, gbc); //While adding button also add it with gbc

GUI pack and setVisible error

Im writing this for fun, this is not homework so please help me all you can. I am trying to make a "dice" that spits out various ski tricks that you can do and i cannot get my gui to come up or work.
public SkiDice20(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridBagLayout());
addItem(panel1, new JLabel("Name: "), 0, 0, 1, 1, GridBagConstraints.EAST);
addItem(panel1, newName, 1, 0, 2, 1, GridBagConstraints.WEST);
System.out.print(newName.getText());
Box slayBox = Box.createVerticalBox();
slayGroup.add(jump);
slayGroup.add(rail);
slayGroup.add(slope);
slayBox.add(jump);
slayBox.add(rail);
slayBox.add(slope);
slayBox.setBorder(BorderFactory.createTitledBorder("What are you Slaying?"));
addItem(panel1, slayBox, 0, 3, 1, 1, GridBagConstraints.NORTH);
Box skillBox = Box.createVerticalBox();
skillGroup.add(gaper);
skillGroup.add(nser);
skillGroup.add(am);
skillGroup.add(pro);
skillBox.add(gaper);
skillBox.add(nser);
skillBox.add(am);
skillBox.add(pro);
skillBox.setBorder(BorderFactory.
createTitledBorder("Skill?"));
addItem(panel1, skillBox, 1, 3, 1, 1, GridBagConstraints.NORTH);
Box buttonBox = Box.createHorizontalBox();
buttonBox.add(okButton);
buttonBox.add(Box.createHorizontalStrut(20));
buttonBox.add(completeButton);
addItem(panel1, buttonBox, 2, 4, 1, 1, GridBagConstraints.NORTH);
this.add(panel1);
this.pack();
this.setVisible(true);
And this is my main method.
public static void main(String[] args) {
String input = JOptionPane.showInputDialog ( "Number of people Skiing?" );
numberOfPeople = Integer.parseInt(input);
new SkiDice20();
checkSelection();
for (int i = 0; i < numberOfPeople; i++){
}
}
I am getting an error on these three lines and nothing else
this.add(panel1);
this.pack();
this.setVisible(true);
I know the cooardinates are screwed up right now, i took the code from an old project that i did but i forgot how i got this to work. Thank you for any help it is greatly appreciated
Needed to extend SkiDice20. After that it worked.

Categories