Door D Uthpala
Door D Uthpala

Reputation: 59

How to load data into set of jbuttons from the database

I want to load my food items int to a set of jbuttons. I have come this far

 static JButton j;
 jPanel3.setLayout(new GridLayout(3, 5, 3, 3));
 jPanel3.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
       for (int i = 0; i < 10; i++) {
            j = new JButton();
            j.setText(i + "");
            jPanel3.add(j);
                 }

but i want to get individual source of the button. i only get the last button source. anybody know how do to this?

Upvotes: 0

Views: 46

Answers (1)

Larex
Larex

Reputation: 11

You have to define j as an array of JButtons. Afterwards you can use j as an Array. Here is a link where you can see how Arrays work : JavaDocsTutorial

 static JButton[] j = new JButton[10];
 jPanel3.setLayout(new GridLayout(3, 5, 3, 3)); 
 Panel3.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
 for (int i = 0; i < 10; i++) {
      j[i] = new JButton();
      j[i].setText(i + "");
      jPanel3.add(j[i]);
 }

The code is untested, but it shows where to make changes.

Upvotes: 1

Related Questions