1

I have used below code but did not work properly. I have list of JButton objects on the panel but could not click on each button individually.

for(int i=0; i<udataArr.length(); i++) {
    userBtn = new JButton();
    userLb = new JLabel();

    cur1 = userBtn.getCursor();
    userBtn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

    Image imgUO =  ImageIO.read(getClass().getResource("/resources/img-std.png"));
    userBtn.setIcon(new ImageIcon(imgUO));
    userBtn.setBorder(BorderFactory.createCompoundBorder(border,paddingBorder));
    userLb.setText((String) udataArr.getJSONObject(i).get("user_name"));
    //button[i].setText((String) udataArr.getJSONObject(i).get("user_name"));
    panelLeft.add(userBtn);
    panelLeft.add(userLb);
    panelLeft.add(Box.createVerticalStrut(15));
}

enter image description here

5
  • 2
    "did not work properly" doesn't really describe the issue. There's also no indication that you've got an array of JButtons anywhere... did you mean to declare a JButton[] buttons variable somewhere? Commented May 14, 2014 at 9:22
  • JButton[] myArray = new JButton[]{new JButton("first"), new JButton("second")}; Commented May 14, 2014 at 9:23
  • no... I am using single button multiple times the userBtn is JButton. I have used iteration so that i can use it more then one times.. I haveen user array og JButton... Commented May 14, 2014 at 9:24
  • Then your topic title is just completely irritating. Commented May 14, 2014 at 9:25
  • For better help sooner, post an MCVE (Minimal Complete and Verifiable Example). Note that one component can be added to a maximum of one container exactly one time. Commented May 14, 2014 at 9:26

1 Answer 1

6

This is the code to create JButton Array

JButton buttons[];
buttons = new JButton[10];
for(int i = 0; i < 10; i++) {
    buttons[i] = new JButton(String.valueOf(i));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.