简体   繁体   中英

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

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:

  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:

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, 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

  // 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...

if your listBox-model is set right (like listBox.setModel(this.lm2); ) I guess the following should work:

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

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:

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

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. 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.

Sorry been sick a while but I believe Andreas L has the right answer to your problem. 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.

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