Reputation: 73
I have this code right here, which is a label with a button, and when the button is clicked the label incrememnts by one:
int helpfulNumber = content.getHelpful();
JButton helpfulBT = new JButton("Helpful");
reviewBoxPanel.add(helpfulBT);
JLabel helpfulLB = new JLabel("Helpful: " + helpfulNumber);
reviewBoxPanel.add(helpfulLB);
helpfulBT.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
int helpfulNumber = content.getHelpful();
int newHelp = helpfulNumber + 1;
helpfulLB.setText("Helpful:" + newHelp);
}
});
helpfulLB.setText("Helpful: " + newHelp); // this doesn't work
In the code below, when submit is clicked, I need to get the value of the label but with the new value of helpfulNumber
. As it is now, it only seems to get the old value of the label
final JButton submitBT = new JButton("Submit");
southPanel.add(submitBT);
submitBT.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == submitBT)
{
MovieReview some = new MovieReview();
some.setUser(userTF.getText());
some.setMovie(titleTF.getText());
some.setFeatured(featuredCB.isSelected());
some.setRating(Integer.parseInt(ratingTF.getText()));
some.setHelpful(helpfulNumber);
some.setUnhelpful(notHelpfulNumber);
some.setComments(reviewTA.getText());
some.setId(content.getId());
if(owner.updateReview(isUpdate, some))
{
// success
try {
MovieReviewDSC.edit(some);
//tableModel.fireTableRowsInserted(firstRow, lastRow);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JOptionPane.showMessageDialog(ReviewEditor.this, "Database succesfully updated!");
}
else
{
// fail
}
}
}
});
So the idea is that when this frame is open, there are already values in there from content
that may not be 0. Currently if I click the button to increment and click submit, the number remains at its original value.
I've tried using the helpfulLB.setText("Helpful: " + newHelp);
again outside the helpfulBT
actionListener but the newHelp
variable isn't recognized. Any help would be appreciated, thank you for your time.
Solution thanks to @tiago7 and @Boosha
helpfulNumber = content.getHelpful();
JButton helpfulBT = new JButton("Helpful");
reviewBoxPanel.add(helpfulBT);
JLabel helpfulLB = new JLabel("Helpful: " + helpfulNumber);
reviewBoxPanel.add(helpfulLB);
helpfulBT.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
helpfulNumber += 1;
helpfulLB.setText("Helpful:" + helpfulNumber);
}
});
while declaring int helpfulNumber
in the frame. Thanks guys, I appreciate the help
Upvotes: 0
Views: 498
Reputation: 11609
Seems that you trying to submit something similar to 'Like' action in social network.
Just declare the variable in your frame, that will hold your 'helpful' value.
Initialize in, when you load the data, increment it in button ActionListener and read it in submit action.
You just need to provide a global scope for this variable, so it can be accessed from all your listeners.
Upvotes: 1
Reputation: 3061
Declare newHelp outside the listener and use it inside the listener by removing the int declaration.
Upvotes: 1