NetBeans GuiBuilder - adding table of objects - java

Is there any way to add "n" Jlabels to the frame, where n is specified somewhere in code? Can I make an Array of Jlabels?

Yes you can
JLabel [] labels = new JLabel[n];
for(int i = 0 ; i <labels.length;i++){
labels[i] = new JLabel("sometext");
//.... and other things
}
It's better to use java.util.List instead of array:
List<JLabel> labels = new ArrayList<>();
for(int i = 0 ; i <labels.length;i++){
labels.add(new JLabel("SomeText"));
labels.get(i).setForeground(Color.red);
//.... and other things
}

Related

How to populate a ButtonGroup[] array using a for loop

So I have two arrays of JRadioButton[] that I am trying to put into an array of ButtonGroup[]. When I add a single JRadioButton from my array one at a time to a single ButtonGroup, I have no problems. When I use a for-loop to add to an array of ButtonGroup[], there are no syntax errors but I do get a compiler error:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "javax.swing.ButtonGroup.add(javax.swing.AbstractButton)" because "allButtons[x]" is null
I don't mind creating buttonGroups one at a time, but I was just wondering why the first one works and the other doesn't.
The code that works:
//creates radio buttons
JRadioButton[] profButtons = new JRadioButton[profs.length];
JRadioButton[] expButtons = new JRadioButton[profs.length];
for(int i=0; i<profs.length; i++)
{
profButtons[i] = new JRadioButton(profs[i]);
expButtons[i] = new JRadioButton(profs[i]);
}
//makes the radio button groups.
ButtonGroup acrGroup = new ButtonGroup();
acrGroup.add(profButtons[0]);
acrGroup.add(expButtons[0]);
The code that doesn't work:
//creates radio buttons
JRadioButton[] profButtons = new JRadioButton[profs.length];
JRadioButton[] expButtons = new JRadioButton[profs.length];
for(int i=0; i<profs.length; i++)
{
profButtons[i] = new JRadioButton(profs[i]);
expButtons[i] = new JRadioButton(profs[i]);
}
//makes the radio button groups.
ButtonGroup[] allButtons = new ButtonGroup[profs.length];
for(int x=0; x<profs.length; x++)
{
allButtons[x].add(profButtons[x]);
allButtons[x].add(expButtons[x]);
}
profs is a String[] array that I use to label the radio buttons.
In this part of your code, you're creating the array of ButtonGroups, but you never initialize each element, so each of them is null
//makes the radio button groups.
ButtonGroup[] allButtons = new ButtonGroup[profs.length];
for(int x=0; x<profs.length; x++)
{
allButtons[x].add(profButtons[x]);
allButtons[x].add(expButtons[x]);
}
So, inside your for-loop, add this line as the first line
allButtons[x] = new ButtonGroup();
And then, the rest of your code

How to make some of jtextfield & jlabel not appear in JPanel

I am currently making a GUI that prints a textarea. In this textarea I am required to receive the "weights" for the ID of the variable.
I have created multiple labels showing from ID1: - ID8: & their textfields, and use if statements instead but the with the amount of if and else if.
if (id.size = 1){
id1.setvisible(true);
weight1TField.setvisible(true);
}else if (id.size = 2){
id1.setvisible(true);
weight1TField.setvisible(true);
id2.setvisible(true);
weight2TField.setvisible(true);
}
else if (if.size = 3){
id1.setvisible(true);
weight1TField.setvisible(true);
id2.setvisible(true);
weight2TField.setvisible(true);
id3.setvisible(true);
weight3TField.setvisible(true);
}
So on... untill ID8.
The values are added to the array from a jtable in another Jframe when user has selected the rows(maximum 8 rows).
List<String> ID = new ArrayList<>();
I want to create text fields to allow the user to input their weights and jlabels showing the ID beside the textfield e.g ID: TextField. Image is shown below
ID[i] is replaced with the value in the array if there is one while the rest is hidden if there is no value. How can i create the Jlabels and JTexFields without doing the following below.
ID1.setText(ID[0]);
ID2.setText(ID[1]);
ID3.setText(ID[2]);
ID4.setText(ID[3]);
ID5.setText(ID[4]);
ID6.setText(ID[5]);
ID7.setText(ID[6]);
ID8.setText(ID[7]);
What about instead of using fields (I am not sure you are using fields, but the only thing i can do without more code is guess) to store the components into arrays? I have done an example with JLabels, you can do the same with textfields.
private static int idsCount = 6; //The number of ids. Let's say 6
private JLabel[] labels = new JLabel[8]; // Keep the array as a field. (8 = max capacity)
private void initIDLabels {
for (int i = 0; i < labels.length; i++) {
labels[i] = new JLabel();
// add this font to all labels.
labels[i].setFont(new Font("Tahoma", Font.BOLD, 12));
}
changeLabelsVisibility();
}
private void changeLabelsVisibility() {
// Hide all labels.
for (JLabel label : labels) {
label.setVisible(false);
}
// Show all labels that supposed to be visible
for (int i = 0; i < idsCount; i++) {
labels[i].setVisible(true);
}
}

Declare multiplie swing elements at once

I am working on a little game, a Java project for IT-classes.
To add/declare(???) Java swing elements I've used this type of writing:
JLabel A = new JLabel();
JLabel B = new JLabel();
//More JLabels...
JButton A = new JButton();
JButton B = new JButton();
//More JButtons...
That the code does not become longer and (more) confusing, I continued with this type of writing:
JLabel A = new JLabel(), B = new JLabel()/*More JLabels...*/;
JButton A = new JButton(), B = new JButton()/*More JButton...*/;
/*Generaly more comments everywhere over the code for my teacher (and me)
*and more for a better overview.
*/
My question is:
Is there a shorter way to add/declare(???) multiple Java swing elements at once?
//like this
JLabel A, B, C, D, E, F = new JLabel();
//or
new JLabel[A, B, C, D, E, F];//PLS don't ask what I'm doing in this line xD
or may is there already a semilar question in Stackoverflow that I've not found?
Edit
This question may already have an answer here: Initializing multiple
variables to the same value in Java 6 answers
Here the Link to the question
Your question has been identified as a possible duplicate of another
question. If the answers there do not address your problem, please
edit to explain in detail the parts of your question that are unique.
Not worked with Jbuttons and JLabels.
If you are using Java 8 you can use :
List<String> labels = ....;
Stream<Button> stream = labels.stream().map(Button::new);
List<Button> buttons = stream.collect(Collectors.toList());
From the book Java se 8 for the really impatient
Then you can use :
JPanel p = new JPanel();
buttons.forEach((t) -> p.add(t));//add your buttons to your panel
It depends, if all your objects are JLabel or the same object type, you could try:
An array of JLabel like:
JLabel[] labels = new JLabel[size of your array];
Then access it after inside a for loop:
for (int i = 0; i < labels.length; i++) {
labels[i] = new JLabel("I'm label: " + i);
}
A List of labels:
ArrayList <JLabel> labelsList = new ArrayList <JLabel>();
Then you could:
for (int i = 0; i < 10; i++) { //I take 10 as an arbitrary number just to do it in a loop, it could be inside a loop or not
labelsList.add(new JLabel("I'm label-list: " + i));
}
And later you could add them like:
pane.add(labels[i]); //Array
pane.add(labelsList.get(i)); //List
The above code should be inside a loop or change i for the explicit index of the element to be added.
If you want to do the same thing (or a similar thing) many times in a program, the answer is to use a loop of some kind. Here, you could declare an array (or List) of JButton elements, and loop over it to initialize its elements:
final int NUM_BUTTONS = 6;
JButton[] buttons = new JButton[NUM_BUTTONS];
for (int i = 0; i < NUM_BUTTONS; i++) {
buttons[i] = new JButton();
}
// refer to A as buttons[0], C as buttons[2], etc
You can make variables equal to each other after declaring them.
String one, two, three;
one = two = three = "";
So, I think you could do
JLabel A,B,C,D,E,F;
A = B = C = D = E = F = new JLabel();

Initializing two-dimensional JPanel arrays using a for loop

I'm relatively new to java and i'm trying to do an assignment for school. In my assignment i'm supposed to make a GUI program that makes a 8 by 8 red and black colored checkerboard. The only problem (so far) that i'm having is initializing a two-dimensional array of JPanels. i'm getting this error when using eclipse:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
This doesn't give an error until i try to run the code. It says the error is occurring in the body of this for loop:
JPanel[][] panel = new JPanel[7][7];
for (int i = 0; i <= panel.length; i++){
panel[i][0] = new JPanel();
panel[i][1] = new JPanel();
panel[i][2] = new JPanel();
panel[i][3] = new JPanel();
panel[i][4] = new JPanel();
panel[i][5] = new JPanel();
panel[i][6] = new JPanel();
panel[i][7] = new JPanel();
}
This does work if i don't use a for loop but i really don't want to put in 64 different statements do do this. I double checked and the panel.length does give the value 7 (which is what i wanted) and did not work even when i physically put in 7. I don't have any syntax error is my code, but i still get the error. Is there some other way i should go about doing this? Thanks in advance. Remember i'm new to this.
"make a GUI program that makes a 8 by 8 red and black colored checkerboard. The only problem (so far) that i'm having is initializing a two-dimensional array of JPanels.".
If all you need to do is make the board, with no other conditions, why not just use a GridLayout
JPanel mainPanel = new JPanel(new GridLayout(8, 8));
for (int i = 0; i < 64; i++){
JPanel panel = new JPanel();
// alternate background colors with a predefined boolean and an if
mainPanel.add(panel);
}
Three things:
A checkboard is 8×8, so you really want panel.length to be 8, no?
A standard for loop uses < for the test, not <=.
If you use two nested loops, you don't need to repeat new JPanel() 8 times.
Result:
JPanel[][] panel = new JPanel[8][8];
for (int i = 0; i < panel.length; i++) {
for (int j = 0; j < panel[i].length; j++) {
panel[i][j] = new JPanel();
}
}

Problem regarding loops. I need to access 10 labels through a loop in java?

I have a problem regarding loops. I need to access 10 labels which have names like label1, label2, label3 .... etc. I need to know whether I can access those labels by going through a loop in java?
How about using List or an array
List<JLabel> labels = new ArrayList<JLabel>();
labels.get(index);
Change those labels to be an array, and access it using an index.
For example:
JLabel[] labels = new JLabel[10];
for (int i = 0; i < labels.length; ++i) {
labels[i] = new JLabel("Label " + i);
}
for (int i = 0; i < labels.length; ++i) {
// access each label.
}
Put your labels in to LinkList or array
Then you can access those array or linkList on a loop
If you cannot change the labels names / put them into an array you can make an array of references to the labels and fill it at the beginning of your program with the list of your labels.
'Access to labels' is kinda vague. Are you referring to different instances of java.awt.label? If so you can simply loop over them when they're in a list with a for-each statement.
If you are talking about Java labels, you can use a switch statement instead. If you are talking about objects such as a JLabel, use an array or ArrayList.

Categories