0

So I am trying to build a CRUD app, I am able to perform all the operations in a console app and now I want to use the same methods in my GUI application. I have setup all the necessary fields and a table, the add button works and I can implement the others too! my question is how to work with the JTable?

Here is the code for my window builder. You can see that I have set columns for the tables too.

this is what the GUI looks like

enter image description here

package clientGUI;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

import bus.Student;
import data.ConnectionDB;

import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
 import java.awt.Font;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData; 
import java.sql.SQLException;
import java.sql.Statement;
public class DbTester1 {

private JFrame frame;
private JTextField textID;
private JTextField textFN;
private JTextField txtLN;
private JTable table;
private JTable table_1;
ArrayList<Student> studentList= null;
String query = "" ;
Statement stmt = null;     
ResultSet rs = null;
Student aStudent = null;    

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
                DbTester1 window = new DbTester1();
                window.frame.setVisible(true);
        }
    });
}
public DbTester1() {
    initialize();   
}
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 732, 695);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
    
    JLabel lblID = new JLabel("ID");
    lblID.setBounds(10, 11, 46, 14);
    frame.getContentPane().add(lblID);
    
    JLabel lblFN = new JLabel("FN");
    lblFN.setBounds(11, 42, 57, 14);
    frame.getContentPane().add(lblFN);
    
    JLabel lblLN = new JLabel("LN");
    lblLN.setBounds(10, 79, 46, 14);
    frame.getContentPane().add(lblLN);
    
    textID = new JTextField();
    textID.setBounds(78, 8, 86, 20);
    frame.getContentPane().add(textID);
    textID.setColumns(10);
    
    textFN = new JTextField();
    textFN.setBounds(78, 39, 86, 20);
    frame.getContentPane().add(textFN);
    textFN.setColumns(10);
    
    txtLN = new JTextField();
    txtLN.setBounds(78, 76, 86, 20);
    frame.getContentPane().add(txtLN);
    txtLN.setColumns(10);
    
    ArrayList<Student> studList = new ArrayList<Student>();

    JButton btnAdd = new JButton("Add");
    btnAdd.setFont(new Font("Tahoma", Font.PLAIN, 8));
    btnAdd.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        Student aStudent = new Student(Integer.parseInt(textID.getText()) , 
textFN.getText(), txtLN.getText());
        
        studList.add(aStudent);
        }
    });
    btnAdd.setBounds(0, 103, 89, 23);
    frame.getContentPane().add(btnAdd);
    
    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(174, 11, 250, 292);
    frame.getContentPane().add(scrollPane);
    
    table = new JTable();
    table.setModel(new DefaultTableModel(
        new Object[][] {
        },
        new String[] {
            "ID", "FN", "LN"
        }
    ));
    scrollPane.setViewportView(table);
    
    table_1 = new JTable();
    scrollPane.setColumnHeaderView(table_1);
    
    JButton btnConnect = new JButton("Connect");
    btnConnect.setFont(new Font("Tahoma", Font.PLAIN, 8));
    btnConnect.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Connection con = null;
            try {
                con = ConnectionDB.getConnection();
                JOptionPane.showMessageDialog(null, "Connection 
Established!");
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
                JOptionPane.showMessageDialog(null, "Connection failed!");
            }   
        }
    });
    btnConnect.setBounds(0, 128, 89, 23);
    frame.getContentPane().add(btnConnect);
    
    JButton btnSearch = new JButton("Search");
    btnSearch.setFont(new Font("Tahoma", Font.PLAIN, 8));
    btnSearch.setBounds(0, 153, 89, 23);
    frame.getContentPane().add(btnSearch);
    
    JButton btnUpdate = new JButton("Update");
    btnUpdate.setFont(new Font("Tahoma", Font.PLAIN, 8));
    btnUpdate.setBounds(0, 178, 89, 23);
    frame.getContentPane().add(btnUpdate);
    
    JButton btnRefresh = new JButton("Refresh");
    btnRefresh.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Connection con = null;
            try {
                con = ConnectionDB.getConnection();
                String id, fn, ln;
                query = "select * from student";       
                stmt = con.createStatement();
                rs = stmt.executeQuery(query);

                studentList = new ArrayList<Student> ();
                
                 while(rs.next())
                   {
                       id = rs.getString(1);
                       fn = rs.getString(2);
                       ln = rs.getString(3);    
                       aStudent = new Student(Integer.parseInt(id) , fn, ln);
                       studentList.add(aStudent);   
                    }
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
                JOptionPane.showMessageDialog(null, "Connection read 
    data!!!!!");
            }
        }
    });
    btnRefresh.setFont(new Font("Tahoma", Font.PLAIN, 8));
    btnRefresh.setBounds(0, 205, 89, 23);
    frame.getContentPane().add(btnRefresh);
        }
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
WildThing
  • 969
  • 1
  • 12
  • 30
  • Store the Oracle table information in a java.util.List, then use the List to populate a DefaultTableModel for the JTable. – Gilbert Le Blanc Apr 17 '21 at 08:09
  • *"Here is the code.."* .. Where? For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) *"this is what the GUI looks like"* That provides no useful information that cannot be described in words. Use words. – Andrew Thompson Apr 17 '21 at 12:47
  • 1) Don't post an image of your entire desktop. The picture of your frame is too small to actually see the components. 2) See: https://stackoverflow.com/questions/55627828/how-to-get-a-defaulttablemodel-objects-data-into-a-subclass-of-defaulttablemode/55635012#55635012 for a general example of adding data to a JTable from a database. – camickr Apr 17 '21 at 14:51
  • Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Apr 18 '21 at 06:51

0 Answers0