简体   繁体   中英

Saving Data on a new JTable (new JFrame) using NetBeans GUI builder

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*/});
}

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);
            }
        });
    }

}

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