简体   繁体   English

如何将项目添加到netbeans中另一个类的列表框中

[英]how to add items to a list box from another class in netbeans

I would like to add elements to a list box on a jframe, from a different class however it doesn't seem to work no matter what i try... i do not get an error or any feedback on whats wrong with this... but heres the layout i have, the listbox is using DefaultListModel lm2 我想在一个jframe的列表框中添加元素,来自不同的类但是无论我尝试什么它似乎都不起作用...我没有得到错误或任何关于这个错误的反馈..但是继承我的布局,列表框正在使用DefaultListModel lm2

I know how to add elements to a jList in the jframe class, but for some reason i am unable to add elments from another class even when adding this inside the jframe class: 我知道如何将元素添加到jframe类的jList中,但是由于某些原因,即使将其添加到jframe类中,我也无法添加其他类的元素:

  public void log(String str) {
      lm2.addElement(str);
    }

and on my "other class" 在我的“其他班级”上

  frmMain doit = new frmMain();

  doit.log("add to list box");
#

More details add- --- > 更多详细信息添加-->

#

I have 3 classes and here they all are: 我有3节课,所有这些都是:

frmMain.class frmMain.class

     // My main class which sets the jFrame to visible
  public class RS232Example {
      public static void main(String[] args) throws Exception {


        frmMain form = new frmMain();
        form.setVisible(true);

    }

  }

RequestInfo.class RequestInfo.class

 // RequestInfo.class, which is trying to add an item to the // jlist but it doesn't add anything or error public class RequestInfo { public void ProcessReturnedInfo(String sData, boolean bWithLabel) { frmMain fm = new frmMain(); fm.log("test test"); } 

RS232Example.class RS232Example.class

  // My main class which sets the jFrame to visible public class RS232Example { public static void main(String[] args) throws Exception { frmMain form = new frmMain(); form.setVisible(true); } } 

I understand maybe i need to set form to visible on the RequestInfo.class, however i can't do this, because it will continuously open the form multiple times, because this class method is called multiple times from an event... 我理解也许我需要在RequestInfo.class上将表单设置为可见,但是我不能这样做,因为它将连续多次打开表单,因为这个类方法被多次从事件中调用...

if your listBox-model is set right (like listBox.setModel(this.lm2); ) I guess the following should work: 如果您的listBox-model设置正确(例如listBox.setModel(this.lm2); ),我想以下应该可以工作:

public static void main(String[] args) throws Exception {
  frmMain form = new frmMain();
  form.setVisible(true);
  form.log("Hallo");
}

If you are wondering why this works and the code within RequestInfo does not, keep in mind, that you create a new frmMain with it's own listModel for every call of ProcessReturnedInfo 如果您想知道为什么这行得通,而RequestInfo中的代码却不行,请记住,您为每个ProcessReturnedInfo调用创建了一个新的frmMain及其自身的listModel。

If you want to have only one Frame updated try to use the frame as singleton: 如果只想更新一个框架,请尝试将该框架用作单例:

Change the constructor of frmMain from public to private and add this to the class: frmMain的构造函数从public更改为private并将其添加到类中:

private static frmMain instance = null;

public static frmMain getInstance() {
  if (instance == null) {
    instance = new frmMain();
  }
  return instance;
}

Instead of calling new frmMain() you must now use frmMain.getInstance() in RequestInfo and RS232Example 现在必须在RequestInfoRS232Example使用frmMain.getInstance() ,而不是调用new frmMain()

That's how you will always work on the same frame. 这样,您将始终在同一帧上工作。

Good Luck. 祝好运。

What is your other class? 你的其他课程是什么? I am assuming that frmMain() creates a JFrame with a JList in it but unless that JFrame is also made visible you won't see it. 我假设frmMain()创建了一个带有JListJFrame ,但是除非该JFrameJList ,否则您将看不到它。 I suspect - and I may be wrong - that you have more than one instance of a JFrame and the string is being added to one that is not being made visible. 我怀疑-我可能错了-您有多个JFrame实例,并且该字符串正在添加到一个不可见的字符串中。

Sorry been sick a while but I believe Andreas L has the right answer to your problem. 对不起,我病了一段时间,但我相信Andreas L对您的问题有正确的答案。 You do not necessarily need a static frame object but it is the simplest way. 您不一定需要静态框架对象,但这是最简单的方法。 You may also still have problems if you are processing multiple threads in which case you will need ways to control the processing of requests, queuing data you have not finished processing etc. 如果您正在处理多个线程,您可能仍然会遇到问题,在这种情况下,您将需要控制请求处理的方法,排队尚未完成处理的数据等。

Good luck. 祝好运。

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

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