0

refresh a jTable from another window! i tried this many times with different types like calling a fire method or calling your model again a simple code will be like this using it with an event like a bush button:

private void btn_add_updtActionPerformed(java.awt.event.ActionEvent evt) {
 ItemsForm form = new ItemsForm();
   //this:
    form.mymodel();
    //or this:
    form.model.fireTableDataChanged();
    //or this:
    form.model.fireTableStructureChanged();
         }

all of them refresh the the table.. but the problem it open another window so i got tow windows opend the new one is refreshed the old is not.. and why i got another window without calling it?

Anti Atlas Dev
  • 416
  • 1
  • 4
  • 15
  • 1. those fire methods, such as `fireTableDataChanged()` and `fireTableStructureChanged()` should be internal to the model itself -- only the model itself should make those calls, and an ActionListener should not be doing this. 2. You've got a question about code that's not working, but you've not posted valid code that we can run or test. If you need serious help you're first going to have to do some serious work, including creating and posting a valid [mcve] here with your question. – Hovercraft Full Of Eels Jan 15 '17 at 02:41
  • My guess is that your problem is here: `ItemsForm form = new ItemsForm();`, that you're creating a new `ItemsForm` object when you shouldn't be doing this. Instead you should be making calls on an already in existence and displayed `ItemsForm` object. If so, the key will be in getting a reference to that object, something a [mcve] would help with. – Hovercraft Full Of Eels Jan 15 '17 at 02:45
  • ok brother i will try to update and add an exampl but later because it is too late here thank you – Anti Atlas Dev Jan 15 '17 at 02:45
  • that button is in another frame so how can i call it without creating a new object of another class?? – Anti Atlas Dev Jan 15 '17 at 02:47
  • How do you pass references of one object to another in any program? You pass them in method or constructor parameters. Just because it's a GUI does not mean it requires different rules. Use constructor or getter/setter methods as you would for any Java program. Required reading for you though: [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636) – Hovercraft Full Of Eels Jan 15 '17 at 02:50
  • you just gave me a good idea i will try that thank you ;-) – Anti Atlas Dev Jan 15 '17 at 02:54

1 Answers1

1

A guess based on incomplete data. Your problem is here:

private void btn_add_updtActionPerformed(java.awt.event.ActionEvent evt) {
    ItemsForm form = new ItemsForm();

    // update form's state here
}

You likely already have an ItemsForm instance in existence that is already displayed and showing its data. Creating a new one will have no effect on the one already displayed.

A wrong solution is to give ItemsForm static fields so that changes made to one are shown in another. Doing this will break OOPs rules and make your code very difficult to test, upgrade and enhance.

A much better solution is to be sure that the code that needs a reference to the active and displayed ItemsForm instance has one, either directly through constructor or setter/getter method calls, or better, indirectly through a M-V-C program structure.

Side recommendations:

  • Those fire methods, such as fireTableDataChanged() and fireTableStructureChanged() should be internal to the model itself -- only the model itself should make those calls, and an ActionListener should not be doing this.
  • Be sure to read: The Use of Multiple JFrames, Good/Bad Practice?
Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373