1

I use SQLite. I have 2 JFrames. 1st is consists of table and "Load data", "Edit data". Click on Edit make the new, 2nd JFrame to appear. I want my table to be updated after I make some changes with a help of class Edit. How do I make it? One idea is to use ActionListener so that after I click save, delete or update buttons in JFrame2 method of Jframe 1, which stays for updating of the table(using query), is called. I have tried, but it does not work. I do not want the method of the 1 Jframe to be called in 2nd Jframe, I want this method to be called in 1st Jframe. What are possible solutions?

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import net.proteanit.sql.DbUtils;

import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.*;
import javax.swing.*;
public class Diary extends JFrame {

    private JPanel contentPane;
    JTable table;
    private JButton btnLoadData;



    public void refreshTabel(){
        try {
            String query = "select * from Diary";
            PreparedStatement pst = connection.prepareStatement(query);
            ResultSet rs = pst.executeQuery();
            table.setModel(DbUtils.resultSetToTableModel(rs));
            pst.close();
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Diary frame = new Diary();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
Connection connection = null;
    /**
     * Create the frame.
     */
    public Diary() {
        setResizable(false);
        connection = Main.dbConnector();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 1250, 650);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(34, 58, 1182, 530);
        contentPane.add(scrollPane);

        table = new JTable();
        scrollPane.setViewportView(table);

        btnLoadData = new JButton("Load data");
        btnLoadData.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    String query = "select * from Diary";
                    PreparedStatement pst = connection.prepareStatement(query);
                    ResultSet rs = pst.executeQuery();
                    table.setModel(DbUtils.resultSetToTableModel(rs));
                    pst.close();
                    rs.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        btnLoadData.setBounds(33, 17, 117, 29);
        contentPane.add(btnLoadData);

        JButton btnAddData = new JButton("Edit");
        btnAddData.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                AddData nw = new AddData();
                nw.addData();
                refreshTabel();
            }
        });
        btnAddData.setBounds(153, 17, 117, 29);
        contentPane.add(btnAddData);

    }
}

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.sun.glass.events.WindowEvent;

import net.proteanit.sql.DbUtils;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.awt.event.ActionEvent;

public class AddData extends JFrame {

    private JPanel contentPane;
    private JTextField Date;
    private JTextField ExerciseName;
    private JTextField Weight;
    private JTextField Sets;
    private JTextField Reps;
    /**
     * Launch the application.
     * @return 
     */


    public static void addData() {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AddData frame = new AddData();
                    frame.setVisible(true);
                    frame.setAlwaysOnTop(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    Connection connection = null;
    private JTextField Exercise;
    private JTextField N;
    /**
     * Create the frame.
     */
    public AddData() {
        setTitle("Edit");
        setResizable(false);
        connection = Main.dbConnector();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 465, 368);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblDate = new JLabel("Date");
        lblDate.setBounds(29, 49, 61, 16);
        contentPane.add(lblDate);

        JLabel ExerciseLabel = new JLabel("Exercise");
        ExerciseLabel.setBounds(29, 88, 90, 16);
        contentPane.add(ExerciseLabel);

        JLabel lblNewLabel_1 = new JLabel("Sets");
        lblNewLabel_1.setBounds(29, 169, 61, 16);
        contentPane.add(lblNewLabel_1);

        JLabel lblReps = new JLabel("Reps");
        lblReps.setBounds(29, 213, 61, 16);
        contentPane.add(lblReps);

        JLabel lblWeight = new JLabel("Weight");
        lblWeight.setBounds(29, 126, 61, 16);
        contentPane.add(lblWeight);

        Date = new JTextField();
        Date.setBounds(169, 56, 150, 26);
        contentPane.add(Date);
        Date.setColumns(10);

        ExerciseName = new JTextField();
        ExerciseName.setBounds(169, 60, 150, 26);
        contentPane.add(ExerciseLabel);
        ExerciseName.setColumns(10);

        Weight = new JTextField();
        Weight.setBounds(169, 132, 150, 26);
        contentPane.add(Weight);
        Weight.setColumns(10);

        Sets = new JTextField();
        Sets.setBounds(169, 170, 150, 26);
        contentPane.add(Sets);
        Sets.setColumns(10);

        Reps = new JTextField();
        Reps.setBounds(169, 208, 150, 26);
        contentPane.add(Reps);
        Reps.setColumns(10);

        JButton btnSave = new JButton("Save");
        btnSave.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    String query = "insert into Diary (Date, Exercise, Weight, Sets, Reps) values (?,?,?,?,?)";
                    PreparedStatement pst = connection.prepareStatement(query);

                    pst.setString(1, Date.getText());
                    pst.setString(2, Exercise.getText());
                    pst.setString(3, Weight.getText());
                    pst.setString(4, Sets.getText());
                    pst.setString(5, Reps.getText());


                    pst.execute();
                    JOptionPane.showMessageDialog(null, "Saved");
                    pst.close();

                } catch (Exception ea) {
                    ea.printStackTrace();
                }

            }
        });
        btnSave.setBounds(29, 261, 117, 29);
        contentPane.add(btnSave);

        JButton btnUpdate = new JButton("Update");
        btnUpdate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    String query = "update Diary set N ='"+N.getText()+"', Exercise ='"+Exercise.getText()+"', Weight ='"+Weight.getText()+"', Sets ='"+Sets.getText()+"', Reps ='"+Reps.getText()+"'  where N ='"+N.getText()+"' ";
                    PreparedStatement pst = connection.prepareStatement(query);

                    pst.execute();
                    JOptionPane.showMessageDialog(null, "Updated");
                    pst.close();

                } catch (Exception ea) {
                    ea.printStackTrace();
                }

            }
        });
        btnUpdate.setBounds(176, 261, 117, 29);
        contentPane.add(btnUpdate);

        Exercise = new JTextField();
        Exercise.setBounds(169, 94, 150, 26);
        contentPane.add(Exercise);
        Exercise.setColumns(10);

        N = new JTextField();
        N.setBounds(169, 18, 150, 26);
        contentPane.add(N);
        N.setColumns(10);

        JLabel lblN = new JLabel("N");
        lblN.setBounds(29, 23, 61, 16);
        contentPane.add(lblN);

        JButton Delete = new JButton("Delete");
        Delete.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    String query = "delete from Diary where N = '"+N.getText()+"'";
                    PreparedStatement pst = connection.prepareStatement(query);

                    pst.execute();
                    JOptionPane.showMessageDialog(null, "Deleted");
                    pst.close();

                } catch (Exception ea) {
                    ea.printStackTrace();
                }
            }
        });
        Delete.setBounds(305, 261, 117, 29);
        contentPane.add(Delete);


    }
}
Artyom
  • 11
  • 2
  • Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammer Mar 09 '16 at 22:59
  • [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice) - You might consder using a `JDiaog` instead to gather the information from the user, when it's closed, adding the row and triggering a new update to the table. See [How to Make Dialogs](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) for more details – MadProgrammer Mar 09 '16 at 23:01

0 Answers0