简体   繁体   English

更新Textarea(Java)中的实例变量

[英]Update instance variable in Textarea (Java)

Im trying to create an code that will update and add an instance variable with 1 every three seconds. 我试图创建一个代码,每三秒钟更新并添加一个实例变量1。 This is the current piece of code that I have 这是我目前拥有的代码

Timer: 计时器:

 public rityta() {
        initComponents();
            //Timer
        time = new Timer(1000, null);
        time.addActionListener(this);
        time.start();
    }


public void actionPerformed(ActionEvent ae) {
    Object o = ae.getSource();
     if (o == time) {
        tid++;
        if (tid % 3 == 0) { //Every three seconds
            for (int i = 0; i < djur.size(); i++) {              //Wont start until something's been added to tha ArrayList named "djur"
                djur.get(i).setValue(djur.get(i).getValue()+1);  //Get the instance variable and adds one
                System.out.println(djur.get(i).getValue());      //Just a proof of concept that it works, the output window updated every three seconds and adds 1
                jTextArea2.update(jTextArea2.getGraphics());     // Is this needed?
            }

        }
    }
}

The Problem Now, since the output updates every three seconds with the new instance variable from my class, my Textarea that displays the instance variable should update as well, right? 现在的问题 ,由于输出每三秒钟更新一次我类中的新实例变量,因此显示实例变量的Textarea也应该更新,对吗? But it wont. 但这不会。 So how do I make the new instance variable update in my Textarea as well? 那么,如何在Textarea中更新新的实例变量?

Since my TextArea also contains the ToString of my ArrayList, the TextArea looks something like this: 由于我的TextArea也包含我的ArrayList的ToString,因此TextArea看起来像这样:

Name: aaa    Value: 10
Name: bbb    Value: 10

And I just want to update the instance variable of 10 to add 1 every three seconds. 我只想将实例变量10更新为每三秒钟添加1。 :) :)

You shouldn't expect the JTextArea update automatically as you never call setText(...) or append(...) on it, and just because a variable has been changed, its String representation within the JTextArea will not automatically change when this occurs. 您不应该期望JTextArea会自动更新,因为您永远不会在其上调用setText(...)append(...) ,并且仅因为变量已更改,JTextArea中的String表示在此情况下不会自动更改。发生。 For the JTextArea to update, either these methods must be called (setText or append) on it, or you must modify the JTextArea's Document (but do so with care). 为了更新JTextArea,必须在这些方法上调用这些方法(setText或append),或者必须修改JTextArea的Document(但要小心)。

As for this: jTextArea2.update(jTextArea2.getGraphics()); 为此: jTextArea2.update(jTextArea2.getGraphics()); You never have a need to do this, nor should you ever want to do this. 您永远不需要这样做,也永远不需要这样做。

As an aside #1, I wonder if you really want to be using a JList or JTable and not a JTextArea, but I can't say for sure without more knowledge of the details of your problem-space and your code. 除了第一点,我想知道您是否真的要使用JList或JTable而不是JTextArea,但是如果没有更多关于问题空间和代码的详细信息,我不能肯定地说。

Aside #2: Please consider changing your user name. 除了#2:请考虑更改您的用户名。 You, like all of us, were born ignorant but hopefully not stupid as the former can be improved upon through diligent effort while the latter cannot. 您和我们所有人一样,天生无知,但希望自己不会傻,因为前者可以通过勤奋的努力得到改善,而后者则不能。


Regarding: 关于:

Since my TextArea also contains the ToString of my ArrayList, the TextArea looks something like this: 由于我的TextArea也包含我的ArrayList的ToString,因此TextArea看起来像这样:

 Name: aaa Value: 10 Name: bbb Value: 10 

And I just want to update the instance variable of 10 to add 1 every three seconds. 我只想将实例变量10更新为每三秒钟添加1。 :) :)

You appear to be displaying mutable tabular data, data that changes frequently, and so in this situation I think that you're far better off using a tool that is built for just this sort of situation, a JTable. 您似乎正在显示可变的表格数据,这些数据经常更改,因此在这种情况下,我认为使用针对这种情况而构建的工具JTable更好。 If you did this, then you would only have to change the pertinent cell's data within the JTable's model when that reference data has been updated, and since you'll wire the model to notify its view (the JTable itself) whenever it changes, your data display will automatically update. 如果执行了此操作,则只需在该参考数据已更新时更改JTable模型中相关单元的数据,并且由于您将在模型更改时将其连接以通知其视图(JTable本身),因此您的数据显示将自动更新。

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

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