1

I'm very new to java which is why I'm using NetBeans GUI builder, basically I've created a JFrame which has two components and I'm able to save the data of two text fields and use a submit button to put this into a JTable thats in the JFrame. But I've created a new JFrame specifically to hold the JTable. so one JFrame has two textfield and a submit button, and another JFrame as a JTable. below is the code I used when I had the JTable, button and two textfield in one JFrame. How would I go about trying to save data into a different JFrame containing only JTable?

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
   DefaultTableModel model = (DefaultTableModel) table.getModel();
   model.addRow(new Object[]{/*some stuff here ignore for this question*/});
}
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Sultan Ahmed
  • 73
  • 1
  • 8
  • either make those label static or table static..then simply get something like `frame1.textfield1.getText()` – Madhan Jul 03 '15 at 17:17
  • 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 3) `static` as suggested by @Madhan is **not the correct way to approach this.** Making things `static` so they are accessibly to other classes will come back to haunt you. – Andrew Thompson Jul 03 '15 at 21:28

1 Answers1

0

One way to update table from Frame2 with values from text fields from Frame1 is to use the observer pattern. Frame1 will have a list of observers which need to be updated once the observable (Frame1) inserts or has new values. I will add the code to be able to understand this better. Also, have a look at the Observer Pattern.

Let's define an Observable interface (these are all the methods that an Observable needs to implement)

public interface Observable {

    public void addObserver(Observer o);

    public void removeObserver(Observer o);

    public void notifyObserver(String[] row);
}

Let's define Frame1 which will be the Observervable

public class Frame1 extends javax.swing.JFrame implements Observable{

    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JButton submitButton;


    private List<Observer> observers = new ArrayList<>();

    public Frame1() {
        initComponents(); // 2 text fields and 1 button
    }

    private void initComponents() {
        // I will skip this part you can generate it with NetBeans
        // Basically initialise jTextField1, jTextField2, and submitButton
    }

    private void submitButtonActionPerformed(java.awt.event.ActionEvent evt)    {                                             
        String[] row = {jTextField1.getText(), jTextField2.getText()};        
        notifyObserver(row);
    }  


    @Override
    public void addObserver(Observer o) {
        observers.add(o);  // subscribe new observer
    }

    @Override
    public void removeObserver(Observer o) {
        observers.remove(o); // unsubscribe new observer
    }

    @Override
    public void notifyObserver(String[] row) {
        for (Observer observer: observers) { 
            observer.update(row);  // notify all observers that new row values are available
        }
    } 
}

Also, let's define an Observer interface (these are all the methods that an Observer needs to implement)

public interface Observer {

    public void update(String[] row);
}

Let's define Frame2 which will be the Observer

public class Frame2 extends javax.swing.JFrame implements Observer {

    private javax.swing.JTable jTable1;

    public Frame2() {
        initComponents();
    }

    private void initComponents() {
        // I will skip this part you can generate it with NetBeans
        // Basically initialise jTable1
    }

    public void addRow(String column1, String column2){
        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
        model.addRow(new Object[]{column1, column2});
    }

    @Override
    public void update(String[] row) {
        addRow(row[0], row[1]);
    }
}

Now, let's wrap everything and test:

public class Main {

    public static void main(String args[]) {

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Frame2 frame2 = new Frame2();
                Frame1 frame1 = new Frame1();

                // Register frame2 as an observer of frame1
                frame1.addObserver(frame2);

                frame1.setVisible(true);
                frame2.setVisible(true);
            }
        });
    }

}
Andrei
  • 7,509
  • 7
  • 32
  • 63