Issues with jlabel/button formatting - java

The category labels 1, 2, 3, 4 and 5 don't seem to align with row I'm assigning them to. I don't understand where the problem is; the x value is equal. If someone could lead me in the right direction, that would be really great.
Here's my code
public class GovernmentJepordyGame {
public static void main(String[] args)
{
JFrame frame = new JFrame("Jepordy");
frame.setVisible(true);
frame.setSize(1300,900);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
frame.getContentPane().add(panel, BorderLayout.PAGE_START);
/*JLabel label = new JLabel("Welcome to government jepordy.");*/
JLabel catagoryOne = new JLabel("Catagory 1");
c.gridx = 0;
c.gridy = 0;
panel.add(catagoryOne, c);
JButton button500A = new JButton("500");
c.gridx = 0;
c.gridy = 1;
c.insets = new Insets(65, 65, 65, 65);
panel.add(button500A, c);
JButton button400A = new JButton("400");
c.gridx = 0;
c.gridy = 2;
panel.add(button400A, c);
JButton button300A = new JButton("300");
c.gridx = 0;
c.gridy = 3;
panel.add(button300A, c);
JButton button200A = new JButton("200");
c.gridx = 0;
c.gridy = 4;
panel.add(button200A, c);
JButton button100A = new JButton("100");
c.gridx = 0;
c.gridy = 5;
panel.add(button100A, c);
JLabel catagoryTwo = new JLabel("Catagory 2");
c.gridx = -1;
c.gridy = 0;
panel.add(catagoryTwo, c);
JButton button500B = new JButton("500");
c.gridx = -1;
c.gridy = 1;
c.insets = new Insets(65, 65, 65, 65);
panel.add(button500B, c);
JButton button400B = new JButton("400");
c.gridx = -1;
c.gridy = 2;
panel.add(button400B, c);
JButton button300B = new JButton("300");
c.gridx = -1;
c.gridy = 3;
panel.add(button300B, c);
JButton button200B = new JButton("200");
c.gridx = -1;
c.gridy = 4;
panel.add(button200B, c);
JButton button100B = new JButton("100");
c.gridx = -1;
c.gridy = 5;
panel.add(button100B, c);
JLabel catagoryThree = new JLabel("Catagory 3");
c.gridx = -2;
c.gridy = 0;
panel.add(catagoryThree);
JButton button500C = new JButton("500");
c.gridx = -2;
c.gridy = 1;
c.insets = new Insets(65, 65, 65, 65);
panel.add(button500C, c);
JButton button400C = new JButton("400");
c.gridx = -2;
c.gridy = 2;
panel.add(button400C, c);
JButton button300C = new JButton("300");
c.gridx = -2;
c.gridy = 3;
panel.add(button300C, c);
JButton button200C = new JButton("200");
c.gridx = -2;
c.gridy = 4;
panel.add(button200C, c);
JButton button100C = new JButton("100");
c.gridx = -2;
c.gridy = 5;
panel.add(button100C, c);
JLabel catagoryFour = new JLabel("Catagory 4");
c.gridx = -3;
c.gridy = 0;
panel.add(catagoryFour);
JButton button500D = new JButton("500");
c.gridx = -3;
c.gridy = 1;
c.insets = new Insets(65, 65, 65, 65);
panel.add(button500D, c);
JButton button400D = new JButton("400");
c.gridx = -3;
c.gridy = 2;
panel.add(button400D, c);
JButton button300D = new JButton("300");
c.gridx = -3;
c.gridy = 3;
panel.add(button300D, c);
JButton button200D = new JButton("200");
c.gridx = -3;
c.gridy = 4;
panel.add(button200D, c);
JButton button100D = new JButton("100");
c.gridx = -3;
c.gridy = 5;
panel.add(button100D, c);
JLabel catagoryFive = new JLabel("Catagory 5");
c.gridx = -4;
c.gridy = 0;
panel.add(catagoryFive);
JButton button500E = new JButton("500");
c.gridx = -4;
c.gridy = 1;
c.insets = new Insets(65, 65, 65, 65);
panel.add(button500E, c);
JButton button400E = new JButton("400");
c.gridx = -4;
c.gridy = 2;
panel.add(button400E, c);
JButton button300E = new JButton("300");
c.gridx = -4;
c.gridy = 3;
panel.add(button300E, c);
JButton button200E = new JButton("200");
c.gridx = -4;
c.gridy = 4;
panel.add(button200E, c);
JButton button100E = new JButton("100");
c.gridx = -4;
c.gridy = 5;
panel.add(button100E, c);
}
}

The gridx should be a positive value, something like...
JFrame frame = new JFrame("Jepordy");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
frame.getContentPane().add(panel, BorderLayout.PAGE_START);
c.gridx = 0;
for (int index = 0; index < 5; index++) {
c.gridy = 0;
JLabel label = new JLabel("Catagory " + index);
c.insets = new Insets(0, 0, 0, 0);
panel.add(label, c);
c.insets = new Insets(65, 65, 65, 65);
for (int cat = 1; cat < 6; cat++) {
c.gridy++;
panel.add(new JButton(Integer.toString(cat * 100)), c);
}
c.gridx++;
}
frame.setVisible(true);
frame.setSize(1300, 900);
for instance...

You are getting x and y mixed up. The top left corner of your screen is 0,0. As you move to the right, x increases, as you move down, y increases. So assigning a (-) value to your x coordinate system is not necessary, it will move likely start moving your buttons off screen to the left. Every button, try increasing the x grid value, instead of the y, and you should get your desired results.
gridx = 10....gridx = 20.. and so on.
Even better, implement some sort of loop to iterate through each button and increase the x values upon each iterate.
Or, look into flowlayout managers, they handle this automatically.

Related

Question about Grid Bag layout and alignment

I am using the gridbag layout to create my first application in Java. My code is completed and is located in another class. The problem I am having is being able to control buttons. I have the Jtextfield that outputs text on very first row to the bottom right. I want that Jtextfield to take 3 of the 4 spaces but its only taking the space in the end only. I want to be able to show entry spanning from from 3 of the cells in the top. I reserved one cell to reserve an image of the operator. Also, I set up some text constraints so that it doesn't resize the whole right hand size as output is being shown. But I wanted to keep the right hand side even. Can anyone tell me what I am doing wrong?
public class MyFrame extends JFrame{
static JLabel display;
static JLabel displayOperator;
public static String entry;
public static String repeatEntry;
public static String repeatEntry2;
public static String num1 = "";
public static String num2 = "";
public static String total = "";
public static String operator = "";
// public values for buttons
public JButton zero;
public JButton one;
public JButton two;
public JButton three;
public JButton four;
public JButton five;
public JButton six;
public JButton seven;
public JButton eight;
public JButton nine;
public JButton equal;
public JButton divide;
public JButton multiply;
public JButton minus;
public JButton plus;
public JButton clear;
public JButton negative;
public JButton decimal;
//attributes for bag layout
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
public MyFrame() {
super();
init();
//added strings to constructor frame
}
private void init() {
// creating a grid bag layout
/* FYI
* weight x and weight y refer to the space taking in the frame or space it takes
* grid x and y refers to location of button
* constraints sets limits to avoid things stretching out too much
*/
this.setLayout(new GridBagLayout());
this.setTitle("Calculator");
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
//all output will shown in this JLabel
display = new JLabel("",SwingConstants.RIGHT); //aligns right
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 3;
c.weighty = 1;
c.gridx = 3;
c.gridy = 0;
this.add(display, c);
// this will display an indicator that show the operator currently in use
displayOperator = new JLabel("",SwingConstants.LEFT); //aligns LEFT
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.weighty = 0;
c.gridx = 0;
c.gridy = 0;
this.add(displayOperator, c);
display.setFont(new Font("Times Roman", Font.PLAIN, 20));
FontMetrics fm = display.getFontMetrics(display.getFont());
int w = fm.stringWidth("00000000"); //set display of numbers by these amount of digits to the text
int h = fm.getHeight();
Dimension size = new Dimension(w, h);
//locks perspective so that JBagged buttons are not autoresized
display.setMinimumSize(size);
display.setPreferredSize(size);
// all input buttons with Button Listeners assigned to them
JButton seven = new JButton("7");
this.add(seven);
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 4;
c.weightx = 1;
c.weighty = 1;
this.add(seven, c);
seven.addActionListener(new MyButtonListener(this));
JButton eight = new JButton("8");
this.add(eight);
c.fill = GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 4;
c.weightx = 1;
c.weighty = 1;
this.add(eight, c);
eight.addActionListener(new MyButtonListener(this));
JButton nine = new JButton("9");
this.add(nine);
c.fill = GridBagConstraints.BOTH;
c.gridx = 2;
c.gridy = 4;
c.weightx = 1;
c.weighty = 1;
this.add(nine, c);
nine.addActionListener(new MyButtonListener(this));
JButton divide = new JButton("/");
this.add(divide);
c.fill = GridBagConstraints.BOTH;
c.gridx = 3;
c.gridy = 4;
c.weightx = .75;
c.weighty = .75;
this.add(divide, c);
divide.addActionListener(new MyButtonListener(this));
JButton four = new JButton("4");
this.add(four);
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 5;
c.weightx = 1;
c.weighty = 1;
this.add(four, c);
four.addActionListener(new MyButtonListener(this));
JButton five = new JButton("5");
this.add(five);
c.fill = GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 5;
c.weightx = 1;
c.weighty = 1;
this.add(five, c);
five.addActionListener(new MyButtonListener(this));
JButton six = new JButton("6");
this.add(six);
c.fill = GridBagConstraints.BOTH;
c.gridx = 2;
c.gridy = 5;
c.weightx = 1;
c.weighty = 1;
this.add(six, c);
six.addActionListener(new MyButtonListener(this));
JButton multiply = new JButton("*");
this.add(multiply);
c.fill = GridBagConstraints.BOTH;
c.gridx = 3;
c.gridy = 5;
c.weightx = 1;
c.weighty = 1;
this.add(multiply, c);
multiply.addActionListener(new MyButtonListener(this));
JButton one = new JButton("1");
this.add(one);
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 6;
c.weightx = 1;
c.weighty = 1;
this.add(one, c);
one.addActionListener(new MyButtonListener(this));
JButton two = new JButton("2");
this.add(two);
c.fill = GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 6;
c.weightx = 1;
c.weighty = 1;
this.add(two, c);
two.addActionListener(new MyButtonListener(this));
JButton three = new JButton("3");
this.add(three);
c.fill = GridBagConstraints.BOTH;
c.gridx = 2;
c.gridy = 6;
c.weightx = 1;
c.weighty = 1;
this.add(three, c);
three.addActionListener(new MyButtonListener(this));
JButton minus = new JButton("-");
this.add(minus);
c.fill = GridBagConstraints.BOTH;
c.gridx = 3;
c.gridy = 6;
c.weightx = 1;
c.weighty = 1;
this.add(minus, c);
minus.addActionListener(new MyButtonListener(this));
JButton zero = new JButton("0");
this.add(zero);
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 7;
c.weightx = 1;
c.weighty = 1;
this.add(zero, c);
zero.addActionListener(new MyButtonListener(this));
JButton decimal = new JButton(".");
this.add(decimal);
c.fill = GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 7;
c.weightx = 1;
c.weighty = 1;
this.add(decimal, c);
decimal.addActionListener(new MyButtonListener(this));
JButton equal = new JButton("=");
this.add(equal);
c.fill = GridBagConstraints.BOTH;
c.gridx = 2;
c.gridy = 7;
c.weightx = 1;
c.weighty = 1;
this.add(equal, c);
equal.addActionListener(new MyButtonListener(this));
JButton plus = new JButton("+");
this.add(plus);
c.fill = GridBagConstraints.BOTH;
c.gridx = 3;
c.gridy = 7;
c.weightx = 1;
c.weighty = 1;
this.add(plus, c);
plus.addActionListener(new MyButtonListener(this));
JButton clear = new JButton("C");
this.add(clear);
c.fill = GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 8;
c.weightx = 1;
c.weighty = 1;
this.add(clear, c);
clear.addActionListener(new MyButtonListener(this));
JButton negative = new JButton("+/-");
this.add(negative);
c.fill = GridBagConstraints.BOTH;
c.gridx = 2;
c.gridy = 8;
c.weightx = 1;
c.weighty = 1;
this.add(negative, c);
negative.addActionListener(new MyButtonListener(this));
JButton percentage = new JButton("%");
this.add(percentage);
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 8;
c.weightx = 1;
c.weighty = 1;
this.add(percentage, c);
percentage.addActionListener(new MyButtonListener(this));
// sets dimensions for window and locks resizing
this.setPreferredSize(new Dimension(400,500));
this.setMaximumSize(new Dimension(400,500));
this.setMinimumSize(new Dimension(400,500));
this.pack();
}
}

fill gridbagconstraints not working properly?

the cells of x other than the first will not take the extra space. I focused first on trying to fix the name parts, which now work but the 3-row part in the birthday section
is supposed to look like this
I can't identify the logical error in here since I have followed the standard: set the fill to horizontal, set weights to 1, set gridwidth and update x and y.
panelMain = new JPanel();
getContentPane().add(panelMain);
panelMain.setBackground(Color.WHITE);
panelForm = new JPanel(new GridBagLayout());
panelForm.setBackground(formColor);
panelMain.add(panelForm);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0,20,0,20);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
lblName = new JLabel("Name");
panelForm.add(lblName, c);
c.gridy = 1;
c.gridwidth = 1;
c.weightx = 0.5;
txfFirst = new JTextField("First");
panelForm.add(txfFirst, c);
c.gridx = 1;
c.gridwidth = 1;
c.weightx = 0.5;
txfLast = new JTextField("Last");
panelForm.add(txfLast, c);
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 2;
c.weightx = 1;
lblEmail = new JLabel("Your email address");
panelForm.add(lblEmail, c);
c.gridy = 3;
txfEmail = new JTextField();
panelForm.add(txfEmail, c);
c.gridy = 4;
lblPw = new JLabel("Create a password");
panelForm.add(lblPw, c);
c.gridy = 5;
pwfPw = new JPasswordField();
panelForm.add(pwfPw, c);
c.gridy = 6;
lblConf = new JLabel("Confirm your password");
panelForm.add(lblConf, c);
c.gridy = 7;
pwfConf = new JPasswordField();
panelForm.add(pwfConf, c);
c.gridy = 8;
lblBday = new JLabel("Birthday");
panelForm.add(lblBday, c);
c.gridy = 9;
c.gridwidth = 1;
c.gridx = 0;
cmbMonth = new JComboBox(months);
panelForm.add(cmbMonth, c);
c.gridx = 1;
c.gridwidth = 1;
c.weightx = 1;
txfDay = new JTextField("Day");
panelForm.add(txfDay, c);
c.gridx = 2;
c.gridwidth = 1;
c.weightx = 1;
txfYear = new JTextField("Year");
panelForm.add(txfYear, c);
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 10;
lblGend = new JLabel("Gender");
panelForm.add(lblGend, c);
c.gridy = 11;
cmbGend = new JComboBox();
panelForm.add(cmbGend,c );
c.gridy = 12;
lblPhone = new JLabel("Mobile phone");
panelForm.add(lblPhone, c);
c.gridy = 13;
txfPhone = new JTextField();
panelForm.add(txfPhone, c);
c.gridy = 14;
lblLoc = new JLabel("Location");
panelForm.add(lblLoc, c);
c.gridy = 15;
cmbLoc = new JComboBox();
panelForm.add(cmbLoc, c);
c.gridy = 16;
c.gridx = 2;
btnNext = new JButton("Next Step");
panelForm.add(btnNext, c);

GridBagLayout Grid Width

When I use the code below to make a JFrame using a GridBagLayout, this is the output:
I want the first three rows' buttons to all have equal widths, but they aren't.
I also want the fourth row buttons to all have equal widths.
How can I do this?
Here is the code:
public Test() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menu = new JMenuBar();
setJMenuBar(menu);
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
field = new JTextField(20);
field.setFont(new Font("Cambria Math", Font.PLAIN, 32));
field.setEditable(false);
updateDisplay();
c.fill = GridBagConstraints.HORIZONTAL;
//c.insets = new Insets(9,9,9,9);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 24;
c.gridheight = 2;
add(field, c);
// sin, cos, tan
c.weightx = 0;
c.gridwidth = 4;
c.gridheight = 1;
c.gridx = 0;
c.weightx = 0;
c.gridy = 2;
add(createButton("sin"), c);
c.gridx = 4;
add(createButton("cos"), c);
c.gridx = 8;
add(createButton("tan"), c);
// cot, sec, csc
c.gridx = 0;
c.gridy = 3;
add(createButton("cot"), c);
c.gridx = 4;
add(createButton("sec"), c);
c.gridx = 8;
add(createButton("csc"), c);
// asin, acos, atan
c.gridx = 0;
c.gridy = 4;
add(createButton("asin"), c);
c.gridx = 4;
add(createButton("acos"), c);
c.gridx = 8;
add(createButton("atan"), c);
// atan2, acot, asec, acsc
c.gridy = 5;
c.gridwidth = 3;
c.weightx = 0;
c.gridx = 0;
add(createButton("atan2"), c);
c.gridx = 3;
add(createButton("acot"), c);
c.gridx = 6;
add(createButton("asec"), c);
c.gridx = 9;
add(createButton("acsc"), c);
pack();
setVisible(true);
}
public JButton createButton(String name) {
JButton button = new JButton(name);
button.setActionCommand(name);
button.addActionListener(this);
button.setFont(new Font("Dialog", Font.PLAIN, 25));
return button;
}
I don't think you can use GridBagLayout for this.
Instead create a main panel with a GridLayout(0, 1);
Then create second panel for the first 3 buttons with a GridLayout(1, 0) and add this panel to the first panel.
Repeat for the other three rows.

How do I put spaces between components in a GridBagLayout?

In a JFrame with a BorderLayout, I have a "control panel" (with buttons and stuff) on the bottom of the window. In the JPanel of this "control panel" I'd like to use a GridBagLayout.
This is the result I have right now.
I was thinking to divide the layout in a 3 rows x 8 columns table.
In this configuration, the "+" symbol should take just one square and all the buttons should fill the panel.
The code is this:
buttonsPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
removeCost = new JButton("-");
c.gridx = 5;
c.gridy = 0;
buttonsPanel.add(removeCost, c);
addCost = new JButton("+");
c.gridx = 7;
c.gridy = 0;
buttonsPanel.add(addCost, c);
text = new JLabel("Incasso");
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 3;
buttonsPanel.add(text, c);
cost = new JTextArea();
cost.setBorder(BorderFactory.createLineBorder(Color.black, 1, true));
cost.setPreferredSize(new Dimension(80, 18));
c.gridx = 5;
c.gridy = 1;
c.gridwidth = 3;
buttonsPanel.add(cost, c);
cancel = new JButton("Cancella");
c.anchor = GridBagConstraints.LINE_START;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 3;
c.insets = new Insets(0, 2, 2, 30);
buttonsPanel.add(cancel, c);
enter = new JButton("Accetta");
c.anchor = GridBagConstraints.LINE_END;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 5;
c.gridy = 2;
c.gridwidth = 3;
c.insets = new Insets(0, 30, 2, 2);
buttonsPanel.add(enter, c);
What am I doing wrong?
You can not create a new instance of the GridBagConstrains. I did so.
I execute your code. Please see screenshot.
import javax.swing.*;
import java.awt.*;
public class app {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel buttonsPanel = new JPanel(new GridBagLayout());
frame.add(buttonsPanel);
// =====================
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
JButton removeCost = new JButton("-");
c.gridx = 5;
c.gridy = 0;
buttonsPanel.add(removeCost, c);
JButton addCost = new JButton("+");
c.gridx = 7;
c.gridy = 0;
buttonsPanel.add(addCost, c);
JLabel text = new JLabel("Incasso");
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 3;
buttonsPanel.add(text, c);
JTextArea cost = new JTextArea();
cost.setBorder(BorderFactory.createLineBorder(Color.black, 1, true));
cost.setPreferredSize(new Dimension(80, 18));
c.gridx = 5;
c.gridy = 1;
c.gridwidth = 3;
buttonsPanel.add(cost, c);
JButton cancel = new JButton("Cancella");
c.anchor = GridBagConstraints.LINE_START;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 3;
c.insets = new Insets(0, 2, 2, 30);
buttonsPanel.add(cancel, c);
JButton enter = new JButton("Accetta");
c.anchor = GridBagConstraints.LINE_END;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 5;
c.gridy = 2;
c.gridwidth = 3;
c.insets = new Insets(0, 30, 2, 2);
buttonsPanel.add(enter, c);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setSize(new Dimension(400, 400));
}
}
You need to have this behavior?
You need to create a new instance of the GridBagConstraints (c = new GridBagConstraints) every time you use it as a parameter to the add method.

Java, Change label Text on 1 of 4 panels created from the same panel declaration

I have Four Panels being dynamically created from a single panel "Mold" each panel has different information on it being set from an array.
Problem is that when the information updates only the last panel in the set changes.
Example: there is a MovesLeft label that shows obviously the number of moves left. As the number goes down the number listed for that player should change, each player having their own panel. Currently only the last panel's Moveleft Label changes and changes for any player who moves not just the last Player. So if Player 1 uses up all his moves it will show 0 on Player 4's panel rather than on player 1's Panel.
Question: is there a way for me to specify which of the four instances of the label I want to change so that the correct player's info gets updated?
Panel Set up
protected JComponent makeTextPanel(String text) {
JPanel panel = new JPanel(false);
//panel.add(filler);
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
name = new JLabel(minirpg.MiniRPG.players.get(setupPlayerIndex).getName());
c.insets = new Insets(5, 5, 5, 5);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 2;
panel.add(name, c);
icon = new JLabel(MiniRPG.players.get(setupPlayerIndex).getIcon());
c.gridx = 1;
c.gridy = 2;
panel.add(icon, c);
lblClass = new JLabel("Class:");
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 1;
panel.add(lblClass, c);
pClass = new JLabel(MiniRPG.players.get(setupPlayerIndex).getclass());
c.gridx = 2;
c.gridy = 3;
panel.add(pClass, c);
lblRole = new JLabel("Role:");
c.gridx = 1;
c.gridy = 4;
panel.add(lblRole, c);
role = new JLabel(MiniRPG.players.get(setupPlayerIndex).getRole());
c.gridx = 2;
c.gridy = 4;
panel.add(role, c);
dash = new JLabel("--------------------STATS--------------------");
c.gridwidth = 4;
c.gridx = 1;
c.gridy = 5;
panel.add(dash, c);
lblStr = new JLabel("Strengh:");
c.gridwidth = 2;
c.gridx = 1;
c.gridy = 6;
panel.add(lblStr, c);
str = new JLabel(Integer.toString(
minirpg.MiniRPG.players.get(setupPlayerIndex).getStr()));
//c.insets = new Insets(5, 5, 5, 5);
c.gridwidth = 1;
c.gridx = 3;
c.gridy = 6;
panel.add(str, c);
lblDex = new JLabel("Dexterity:");
c.gridwidth = 2;
c.gridx = 1;
c.gridy = 7;
panel.add(lblDex, c);
dex = new JLabel(Integer.toString(
minirpg.MiniRPG.players.get(setupPlayerIndex).getDex()));
c.gridwidth = 1;
c.gridx = 3;
c.gridy = 7;
panel.add(dex, c);
lblEnd = new JLabel("Endurance:");
c.gridwidth = 2;
c.gridx = 1;
c.gridy = 8;
panel.add(lblEnd, c);
end = new JLabel(Integer.toString(
minirpg.MiniRPG.players.get(setupPlayerIndex).getEnd()));
c.gridwidth = 1;
c.gridx = 3;
c.gridy = 8;
panel.add(end, c);
lblDex = new JLabel("Wisdom:");
c.gridwidth = 2;
c.gridx = 1;
c.gridy = 9;
panel.add(lblDex, c);
wiz = new JLabel(Integer.toString(
minirpg.MiniRPG.players.get(setupPlayerIndex).getWis()));
c.gridwidth = 1;
c.gridx = 3;
c.gridy = 9;
panel.add(wiz, c);
lblHp = new JLabel("HP: ");
c.gridx = 1;
c.gridy = 10;
panel.add(lblHp, c);
health = new JLabel(Integer.toString(
minirpg.MiniRPG.players.get(setupPlayerIndex).getHp()));
c.gridx = 2;
c.gridy = 10;
panel.add(health, c);
lblMoves = new JLabel("Moves Left: ");
c.gridx = 1;
c.gridy = 11;
panel.add(lblMoves, c);
movesLeft = new JLabel(Integer.toString(
MiniRPG.players.get(setupPlayerIndex).getMoves()));
c.gridx = 2;
c.gridy = 11;
panel.add(movesLeft, c);
dash = new JLabel("--------------------SKILLS--------------------");
c.gridwidth = 4;
c.gridx = 1;
c.gridy = 12;
panel.add(dash, c);
lblSkill1 = new JLabel("Skill 1:");
c.gridwidth = 1;
c.gridx = 1;
c.gridy = 13;
panel.add(lblSkill1, c);
skill1 = new JButton(MiniRPG.players.get(setupPlayerIndex).getSkill1());
c.gridx = 2;
c.gridy = 13;
panel.add(skill1, c);
lblSkill2 = new JLabel("Skill 2:");
c.gridx = 1;
c.gridy = 14;
panel.add(lblSkill2, c);
skill2 = new JButton(MiniRPG.players.get(setupPlayerIndex).getSkill2());
c.gridx = 2;
c.gridy = 14;
panel.add(skill2, c);
skill2.setEnabled(false);
lblSkill3 = new JLabel("Skill 3:");
c.gridx = 1;
c.gridy = 15;
panel.add(lblSkill3, c);
skill3 = new JButton(MiniRPG.players.get(setupPlayerIndex).getSkill3());
c.gridx = 2;
c.gridy = 15;
panel.add(skill3, c);
skill3.setEnabled(false);
lblSkill4 = new JLabel("Skill 4:");
c.gridx = 1;
c.gridy = 16;
panel.add(lblSkill4, c);
skill4 = new JButton(MiniRPG.players.get(setupPlayerIndex).getSkill4());
c.gridx = 2;
c.gridy = 16;
panel.add(skill4, c);
skill4.setEnabled(false);
skill1.addActionListener(e);
skill2.addActionListener(e);
skill3.addActionListener(e);
skill4.addActionListener(e);
return panel;
}
How the Panel is added to the GUI
final JTabbedPane characterInfoPane = new JTabbedPane();
while (setupPlayerIndex < 4) {
JComponent panel = makeTextPanel("");
characterInfoPane.addTab(minirpg.MiniRPG.players.get(setupPlayerIndex).getName(), null, panel,
"");
setupPlayerIndex++;
}
how I am currently trying to change the relavent Label where x is the selected Player
movesLeft.setText(Integer.toString(MiniRPG.players.get(x).getMoves()));
please let me know if addition code is needed to Answer this Question of if it is possible to do what im looking for.
Every time you create a new text pane, you are reassign the variable references. That is, movesLeft will ALWAYS refer to the last panel you created.
Rather then creating the panels in the makeTextPane, I would create a custom panel that contained all the information that makeTextPane produces.
I would then make setters & getters for all the relevent information you need to get and update.
For example
protected class TextPane extends JPanel {
private JLabel name;
private JLabel icon;
private JLabel lblClass;
private JLabel pClass;
private JLabel lblRole;
private JLabel role;
private JLabel lblStr;
private JLabel str;
private JLabel lblDex;
private JLabel dex;
private JLabel lblEnd;
private JLabel end;
private JLabel wiz;
private JLabel lblHp;
private JLabel health;
private JLabel lblMoves;
private JLabel movesLeft;
private JLabel lblSkill1;
private JButton skill1;
private final JLabel lblSkill2;
private final JButton skill2;
private final JLabel lblSkill3;
private final JButton skill3;
private final JLabel lblSkill4;
private final JButton skill4;
public TextPane() {
//add(filler);
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel dash;
name = new JLabel(MiniRPG.MiniRPG.players.get(setupPlayerIndex).getName());
c.insets = new Insets(5, 5, 5, 5);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 2;
add(name, c);
icon = new JLabel(MiniRPG.players.get(setupPlayerIndex).getIcon());
c.gridx = 1;
c.gridy = 2;
add(icon, c);
lblClass = new JLabel("Class:");
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 1;
add(lblClass, c);
pClass = new JLabel(MiniRPG.players.get(setupPlayerIndex).getclass());
c.gridx = 2;
c.gridy = 3;
add(pClass, c);
lblRole = new JLabel("Role:");
c.gridx = 1;
c.gridy = 4;
add(lblRole, c);
role = new JLabel(MiniRPG.players.get(setupPlayerIndex).getRole());
c.gridx = 2;
c.gridy = 4;
add(role, c);
dash = new JLabel("--------------------STATS--------------------");
c.gridwidth = 4;
c.gridx = 1;
c.gridy = 5;
add(dash, c);
lblStr = new JLabel("Strengh:");
c.gridwidth = 2;
c.gridx = 1;
c.gridy = 6;
add(lblStr, c);
str = new JLabel(Integer.toString(
MiniRPG.MiniRPG.players.get(setupPlayerIndex).getStr()));
//c.insets = new Insets(5, 5, 5, 5);
c.gridwidth = 1;
c.gridx = 3;
c.gridy = 6;
add(str, c);
lblDex = new JLabel("Dexterity:");
c.gridwidth = 2;
c.gridx = 1;
c.gridy = 7;
add(lblDex, c);
dex = new JLabel(Integer.toString(
MiniRPG.MiniRPG.players.get(setupPlayerIndex).getDex()));
c.gridwidth = 1;
c.gridx = 3;
c.gridy = 7;
add(dex, c);
lblEnd = new JLabel("Endurance:");
c.gridwidth = 2;
c.gridx = 1;
c.gridy = 8;
add(lblEnd, c);
end = new JLabel(Integer.toString(
MiniRPG.MiniRPG.players.get(setupPlayerIndex).getEnd()));
c.gridwidth = 1;
c.gridx = 3;
c.gridy = 8;
add(end, c);
lblDex = new JLabel("Wisdom:");
c.gridwidth = 2;
c.gridx = 1;
c.gridy = 9;
add(lblDex, c);
wiz = new JLabel(Integer.toString(
MiniRPG.MiniRPG.players.get(setupPlayerIndex).getWis()));
c.gridwidth = 1;
c.gridx = 3;
c.gridy = 9;
add(wiz, c);
lblHp = new JLabel("HP: ");
c.gridx = 1;
c.gridy = 10;
add(lblHp, c);
health = new JLabel(Integer.toString(
MiniRPG.MiniRPG.players.get(setupPlayerIndex).getHp()));
c.gridx = 2;
c.gridy = 10;
add(health, c);
lblMoves = new JLabel("Moves Left: ");
c.gridx = 1;
c.gridy = 11;
add(lblMoves, c);
movesLeft = new JLabel(Integer.toString(
MiniRPG.players.get(setupPlayerIndex).getMoves()));
c.gridx = 2;
c.gridy = 11;
add(movesLeft, c);
dash = new JLabel("--------------------SKILLS--------------------");
c.gridwidth = 4;
c.gridx = 1;
c.gridy = 12;
add(dash, c);
lblSkill1 = new JLabel("Skill 1:");
c.gridwidth = 1;
c.gridx = 1;
c.gridy = 13;
add(lblSkill1, c);
skill1 = new JButton(MiniRPG.players.get(setupPlayerIndex).getSkill1());
c.gridx = 2;
c.gridy = 13;
add(skill1, c);
lblSkill2 = new JLabel("Skill 2:");
c.gridx = 1;
c.gridy = 14;
add(lblSkill2, c);
skill2 = new JButton(MiniRPG.players.get(setupPlayerIndex).getSkill2());
c.gridx = 2;
c.gridy = 14;
add(skill2, c);
skill2.setEnabled(false);
lblSkill3 = new JLabel("Skill 3:");
c.gridx = 1;
c.gridy = 15;
add(lblSkill3, c);
skill3 = new JButton(MiniRPG.players.get(setupPlayerIndex).getSkill3());
c.gridx = 2;
c.gridy = 15;
add(skill3, c);
skill3.setEnabled(false);
lblSkill4 = new JLabel("Skill 4:");
c.gridx = 1;
c.gridy = 16;
add(lblSkill4, c);
skill4 = new JButton(MiniRPG.players.get(setupPlayerIndex).getSkill4());
c.gridx = 2;
c.gridy = 16;
add(skill4, c);
skill4.setEnabled(false);
skill1.addActionListener(e);
skill2.addActionListener(e);
skill3.addActionListener(e);
skill4.addActionListener(e);
}
public void setMovesLeft(int value) {
movesLeft.setText(Integer.toString(value));
}
}
I would then keep a separate reference of each pane when I create it
final JTabbedPane characterInfoPane = new JTabbedPane();
TextPane[] textPanes = new TextPane[4];
while (setupPlayerIndex < 4) {
textPanes[setupPlayerIndex] = new TextPane();
characterInfoPane.addTab(minirpg.MiniRPG.players.get(setupPlayerIndex).getName(), null, textPanes[setupPlayerIndex],
"");
setupPlayerIndex++;
}
This would then allow me to change a particular players moves simply by access setter of the TextPane
textPanes[x].setMovesLeft(MiniRPG.players.get(x).getMoves());

Categories