简体   繁体   English

当JButton将JLabel中的值递增并放入数据库时​​,获取该值

[英]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 . 在下面的代码中,单击“提交”后,我需要获取标签的值,但要使用新的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. 这样的想法是,当这个框架是开放的,目前已经在那里值content可能不为0。目前,如果我按一下按钮来增加和点击提交,数保持在原来的数值。

I've tried using the helpfulLB.setText("Helpful: " + newHelp); 我试过使用helpfulLB.setText("Helpful: " + newHelp); again outside the helpfulBT actionListener but the newHelp variable isn't recognized. 再次在helpfulBT actionListener外部,但是无法识别newHelp变量。 Any help would be appreciated, thank you for your time. 任何帮助将不胜感激,谢谢您的时间。

Solution thanks to @tiago7 and @Boosha 感谢@ tiago7和@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. 同时在框架中声明int helpfulNumber 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. 初始化时,加载数据时,在按钮ActionListener中将其递增,并在提交操作中对其进行读取。

You just need to provide a global scope for this variable, so it can be accessed from all your listeners. 您只需要为此变量提供全局范围,即可从所有侦听器访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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