简体   繁体   中英

Java how to assign id to button and retrieve them?

I'm getting stuck while building a forum like application which has a vote button.

I have vote up and vote down button for each content which are automatically generated. I want this button to only display the up and down arrow but not any text or label.. how can i find out which button is pressed?

Automated content..

ImageIcon upvote = new ImageIcon(getClass().getResource("vote_up.png"));
ImageIcon downvote = new ImageIcon(getClass().getResource("vote_down.png"));
JButton vote_up = new JButton(upvote);
JButton vote_down = new JButton(downvote);
vote_up.addActionListener(voting);
vote_down.addActionListener(voting);

Action voting = new AbstractAction(){
    @Override
    public void actionPerformed(ActionEvent e){
        //What to do here to find out which button is pressed?
    }
};

any help is appreciated.

public void a(){
    int crt_cnt = 0;
    for(ClassA temp : listofClassA)
    {                    
        b(crt_cnt);
        crt_cnt++;
    }

}
public void b(crt_cnt){
     //draw button
}

As from above, I have multiple vote_up and vote_down button created by the b function, how can i differentiate which crt_cnt is the button from?

There are multiple ways you might achieve this

You could...

Simply use the source of the ActionEvent

Action voting = new AbstractAction(){
    @Override
    public void actionPerformed(ActionEvent e){
        if (e.getSource() == vote_up) {
            //...
        } else if (...) {
            //...
        }
    }
};

This might be okay if you have a reference to the original buttons

You could...

Assign a actionCommand to each button

JButton vote_up = new JButton(upvote);
vote_up.setActionCommand("vote.up");
JButton vote_down = new JButton(downvote);
vote_down .setActionCommand("vote.down");
//...
Action voting = new AbstractAction(){
    @Override
    public void actionPerformed(ActionEvent e){
        if ("vote.up".equals(e.getActionCommand())) {
            //...
        } else if (...) {
            //...
        }
    }
};

You could...

Take full advantage of the Action API and make indiviual, self contained actions for each button...

public class VoteUpAction extends AbstractAction {

    public VoteUpAction() {
        putValue(SMALL_ICON, new ImageIcon(getClass().getResource("vote_up.png")));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // Specific action for up vote
    }

}

Then you could simply use

JButton vote_up = new JButton(new VoteUpAction());
//...

Which will configure the button according to the properties of the Action and will trigger it's actionPerformed method when the button is triggered. This way, you know 100% what you should/need to do when the actionPerformed method is called, without any doubts.

Have a closer look at How to Use Actions for more details

You can detect by using the method getSource() of your EventAction

Action voting = new AbstractAction(){
    @Override
    public void actionPerformed(ActionEvent e){
        if (e.getSource() == vote_up ) {
           // vote up clicked
       } else if (e.getSource() == vote_down){
          // vote down clicked
       }
    }
};

I would wrap the JButton similar to this:

    JButton createMyButton(final JPanel panel, final String text,
        final boolean upOrDown, final int gridx, final int gridy) {
        final JButton button = new JButton();
        button.setPreferredSize(new Dimension(80, 50));
        final GridBagConstraints gbc = Factories.createGridBagConstraints(gridx,
            gridy);
        panel.add(button, gbc);
        button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            myActionPerformed(text, upOrDown);
        }
    });
    return button;
}

You could use an int instead of the text, if more convenient.

hey thanks for all the help and assistance! I've finally got it! I solved it by assigning a text on the button, +/- for vote up or down, followed by the content id which i required, then change the font size to 0

vote.setText("+"+thistopic.content.get(crt_cnt).get_id());
vote.setFont(heading.getFont().deriveFont(0.0f));

after that i could easily trace which button is pressed by comparing to the

actionEvent.getActionCommand()

which return the text on the button!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM