简体   繁体   中英

Get the value when JButton increments value in JLabel and put into database

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

在监听器外部声明newHelp,并通过删除int声明在监听器内部使用它。

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.

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