1

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?

Kent Ong
  • 97
  • 1
  • 7

4 Answers4

3

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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • You came out with a solution really fast! – user3437460 Sep 15 '15 at 06:51
  • @user3437460 I can copy and paste pretty well ;) – MadProgrammer Sep 15 '15 at 06:54
  • hey thanks for the reply, however, I have alot of vote_up and vote_down, each crt_cnt(current content) have one of each, how can i identify which crt_cnt these buttons belong to? thank you for your help! – Kent Ong Sep 15 '15 at 06:59
  • I have no idea as I only have the snippet of code you've provided to go on. You could, for example, use the idea of the `VoteUpAction` and pass it some information which allows it to identify what it's actually voting against as an idea – MadProgrammer Sep 15 '15 at 07:00
  • I "imgaine" that you have data in some kind of model. May you can construct the `Action` in such away that it not only had a reference to the model, but knew what it was updating. Of course you could equally just defined a `interface` or `class` of some kind which simply had a `voteUp` and `voteDown` method and pass an instance of this to each `Action`, which would call the appropriate method for their use case. You would then have a model of some kind which maintain a list of all these elements ... as some ideas – MadProgrammer Sep 15 '15 at 07:11
1

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
       }
    }
};
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

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!

Kent Ong
  • 97
  • 1
  • 7
0

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.

Jonathan Rosenne
  • 2,159
  • 17
  • 27