How can I create a multiple jLabel using the result set from database column?
I spent almost 2 days trying to solve but unfortunately i still didn't get it correct. please help me.
Here's my codes:
String[] arr = null;
try{
String sql = "SELECT * FROM Position";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
while(rs.next()){
String s1 = rs.getString("PositionName");
arr = s1.split("\n");
int v = 50;
for (int i = 0; i < arr.length; i++){
JLabel[] labels = new JLabel[i];
labels[i] = new JLabel();
labels[i].setBounds(50,v,80,20);
labels[i].setText(arr[i]);
jPanel3.add(labels[i]);
v+=40;
System.out.println(arr[i]);
}
jPanel3.repaint();
}
I want a result something like this, (in a jLabel)
President
Vice-President
Secretary
if i make int i = 0 the output is "java.lang.ArrayIndexOutOfBoundsException:0"
if i make it int i = 1, there's no error but not created jLabel at all.
When i == 0, then
JLabel[] labels = new JLabel[i];
creates an array which can't hold anything since it's size is 0. So then
labels[i] = new JLabel();
attempts to set the element at index 0 which causes the exception.
The code is attempting to do too much which can make it difficult to find bugs. Try separating the code into single responsibility methods such as:
List<String> getAvailablePositions() {
// get the positions from the database
}
and
void addPositionLabels(List<String> availablePositions) {
// add the positions to the JPanel
}
initialize JLabel array outside the for-loop
JLabel[] labels = new JLabel[arr.length];
for (int i = 0; i < arr.length; i++){
labels[i] = new JLabel();
labels[i].setBounds(50,v,80,20);
labels[i].setText(arr[i]);
jPanel3.add(labels[i]);
v+=40;
}
**here is the sample code **
JFrame frame = new JFrame();
frame.setSize(300,300);
JPanel panel = new JPanel();
panel.setLayout(null);
int v = 50;
JLabel[] labels = new JLabel[3];
for (int i = 0; i < 3; i++){
labels[i] = new JLabel();
labels[i].setBounds(50,v,80,20);
labels[i].setText("hi");
panel.add(labels[i]);
v+=40;
}
frame.add(panel);
frame.setVisible(true);
**you will get the following result
hi
hi
hi**
I am trying to create a grid of buttons.
This is my code to create the grid (which works), but if I want to access a single button later on how would I go about doing that?
for(int i = 1; i<= row; i++){
for( int p=1; p<= col; p++){
boardPanel.add(new JButton());
}
}
Many Thanks
A Clements;
From your question title I'm assuming that you want to be able to access these buttons from a 2d array, but in your code the button is not in an array. If you did something like the following:
JButton[][] buttons = new JButton[row][col];
for(int i = 1; i<= row; i++){
for( int p=1; p<= col; p++){
buttons[i][p] = new JButton();
boardPanel.add(buttons[i][p]);
}
}
Then you are maintaining a 2d array that contains references to the buttons in your JPanel. So now you can access the buttons from the array like this:
buttons[i][j];
I want to add 70 labels and 70 checkboxes in an applet I takes lot of code when we write normally . how to add these labels using for loop
You could use a for loop to add Labels to an array. Something like
List<Label> li = new List<Label>();
for(int i = 0; i < 70; i++)
{
li.add(new Label("label " + i));
}
If you don't want to use an array just you could just add the labels directly to your layout.
FlowLayout fl = new FlowLayout(FlowLayout.CENTER, 10, 10);
for(int i = 0; i < 70; i++)
{
fl.add(new Label("label " + i));
}
In my JPanel I am using tablelayout.jar Oracle library (have a look here) and so, generally, I have to do the following:
private double[][] size = {
{30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30},
{30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30}
};
JPanel p = new JPanel();
p.setLayout(new TableLayout(size));
where "30" is the dimension respectively for cos and rows. In this case we wanted square cells. So I can do, for example,:
p.add(new JButton(), "1,4" /*"col,row"*/);
We thought that declaring that "size" matrix like that was not good to do and so we changed the initialization like the following:
size = new double[Constants.GUI_ROWS][Constants.GUI_COLS];
for (int i=0; i<Constants.GUI_COLS-1; i++)
for (int j=0; j<Constants.GUI_ROWS-1; j++)
size[i][j] = 30;
where
Constants.GUI_COLS = 19 ({30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30})
and
Constants.GUI_ROWS = 17 ({30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30})
but this does not work. When we try to add something to the JPanel then nothing is shown. It works only if we write the first initialization by hand. Why this?
To achieve the same as you did by hand you can use
double size[][];
size = new double[2][];
size[0]=new double[19];
size[1]=new double[17];
for (int i=0; i<19; i++)
size[0][i] = 30;
for (int i=0; i<17; i++)
size[1][i] = 30;
You got the loop conditions off by one. Should be :
for (int i=0; i<Constants.GUI_COLS; i++)
for (int j=0; j<Constants.GUI_ROWS; j++)
size[i][j] = 30;
Note that if this call
p.setLayout(new TableLayout(size));
comes before this call :
size = new double[Constants.GUI_ROWS][Constants.GUI_COLS];
The old array referred by size will be used by the TableLayout.
How to initialize/declare a 2D array of reference type in Java? In particular I want to initialize a 2d array of JButton type (3x3) and then add them to a frame inside the constructor. How do i go about it?
MadProgrammer is correct, but in order to use them, you will need to initialize every JButton individually thereafter.
JButton[][] buttons = new JButton[3][3];
for(int i = 0; i <= 2; i++){
for(int x = 0; x <= 2; x++){
buttons[i][x] = new JButton();
}
}
JButton[][] myButtons = JButton[3][3];
creates the array you need. It declares and initializes the array. If you want to declare and initialize it separately, then you can do it this way:
JButton[][] myButtons;
//...
myButtons = JButton[3][3];