Dungeoun Dungeoun - 1 month ago 20
Java Question

How to add or remove a jtable if the GUI is defined in a constructor?

The Problem is that the GUI of the app is defined in the constructor and I'm unable to access the panel or the frame to, either add or delete the jtable. Here's the constructor code:

public FirstSwingApp(){

super();
jbtOK = new JButton("OK");
jbtOK.addActionListener(new BtnPress());

jbtAdd = new JButton("Add");
jbtAdd.addActionListener(new BtnAdd());

jbtView = new JButton("View Table");
jbtView.addActionListener(new BtnView());

lblName = new JLabel("Friend Name");
nameText = new JTextField(20);

lblNo = new JLabel("Friend Number");
noText = new JTextField(4);

lblBdate = new JLabel("Birth date");
bdateText = new JTextField(10);

lblSex = new JLabel("Sex");
sexText = new JTextField(1);

lblState = new JLabel("State");
stateText = new JTextField(20);

lblCity = new JLabel("City");
cityText = new JTextField(20);

panelBtn = new JButton("pbutton");

table = new JTable();


JPanel panel1 = new JPanel();
panel1.setLayout(new FlowLayout(FlowLayout.LEADING));
panel1.setSize(300,300);
panel1.add(lblName);
panel1.add(nameText);
panel1.add(lblNo);
panel1.add(noText);


JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout(FlowLayout.LEADING));
panel2.setSize(300,300);
panel2.add(lblBdate);
panel2.add(bdateText);
panel2.add(lblSex);
panel2.add(sexText);

JPanel panel3 = new JPanel();
panel3.setLayout(new FlowLayout(FlowLayout.LEADING));
panel3.setSize(300,300);
panel3.add(lblState);
panel3.add(stateText);
panel3.add(lblCity);
panel3.add(cityText);


JPanel panel4 = new JPanel();
panel4.setLayout(new FlowLayout(FlowLayout.LEADING));
panel4.setSize(300,300);
panel4.add(jbtAdd);

JPanel panel5 = new JPanel();
panel5.setLayout(new FlowLayout(FlowLayout.LEADING));
panel5.setSize(300,300);

panel5.add(jbtView);



this.add(panel1);
this.add(panel2);
this.add(panel3);
this.add(panel4);
this.add(panel5);




this.setLayout(new FlowLayout(FlowLayout.LEADING));

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setBounds(100,100,400,250);

this.setTitle("gotta get that swing");

this.setVisible(true);

this.setSize(700,400);


}


Here's the ActionListener Code:

private class BtnView implements ActionListener{

public void actionPerformed(ActionEvent e) {

CallableStatement dstmt = null;
ResultSet rs;

try {

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Contact_Manager?user=root");
String disQuery = "select * from FRIEND";

dstmt = conn.prepareCall(disQuery);

rs = dstmt.executeQuery();

ResultSetMetaData metaData = rs.getMetaData();

// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}

// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}

// It creates and displays the table

table = new JTable(data, columnNames);

add(new JScrollPane(table));

repaint();
revalidate();


} catch (SQLException ex) {

System.out.println("Error in connection: " + ex.getMessage());
}

}
}


The actionListener class of the button cannot access the panels in the constructor. I've used add() to add the table but I'm not sure if it's the right way. And Even if I've added a table, I cannot delete it when the button is pressed again.

Answer

It looks like there are a few things that are currently missing in your code, which prevent the table from updating. As camickr already pointed out, it is not necessary to replace the table component (or even the table model). If you update the model, the table should automatically detect the changes and refresh. Also, the table is currently created, but not added to the frame.

Creating a table with an explicit table model and adding it to the frame could be done like this:

DefaultTableModel tableModel = new DefaultTableModel();

JTable table = new JTable(tableModel);

JPanel panel6 = new JPanel();
panel6.setLayout(new FlowLayout(FlowLayout.LEADING));
panel6.setSize(300,300);
panel6.add(new JScrollPane(table));

this.add(panel6);

You could pass the tableModel to the BtnView class (for example as a parameter to its constructor) and update the model like this:

tableModel.setDataVector(data, columnNames);