Refresh Jtable after data has been changed
Java Questoin
Hi, thank you for taking the time to read my post I have a mysql database which uses a program to add data into the database; I want to have a seperate Jtable which then retrieves the data able to either auto update, update upon the press of a button, or update in reponse to the other program adding a new record to the database. I've read as much as possible about repaint and firetablemodelchanged but i'm unsure how these work. Also I have read into abstract table models. Do anyone know how I could get my table to refresh? if not does anyone know the answer to the questions below
A few questions I would greatly appreciate the answer to are is the default table method capable of accomplishing my goals or will I need to create a abstract method?
Will I need to use repaint or will i need to use firetablemodelchanged?
and how do i call either methods if I need to use them?
Thank you for your help!!!
My current problem that i'm trying to solve is getting my working jtable to auto update.
what are you recommendations on how to accomplish my goals?
Code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Comparator;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.table.*;
public class jtable implements ActionListener{
JTable table;
JFrame frame;
JPanel panel;
DefaultTableModel model;
public static void main(String[] args) {
new jtable();
StoreData.StoreData();
}
public jtable(){
JButton homeButton = new JButton("Home");
JButton exitButton = new JButton("Exit");
JButton refreshButton = new JButton("Refresh");
JFrame frame = new JFrame("NEI CRM INFO");
JPanel panel = new JPanel();
panel.add( homeButton, getConstraints(0,0,1,1, GridBagConstraints.SOUTH));
homeButton.addActionListener(this);
panel.add(exitButton,getConstraints(0,0,1,1, GridBagConstraints.SOUTH));
exitButton.addActionListener(this);
panel.add(refreshButton,getConstraints(0,0,1,1, GridBagConstraints.SOUTH));
refreshButton.addActionListener(this);
String data[][] = {};
String col[] = {"InfoID","Company","Date/Time","Reason","Note"};
//Creates default table model
DefaultTableModel model = new DefaultTableModel(data,col);
JTable table = new JTable(model);
table.setAutoCreateRowSorter(true);
//SetsTable Size Within the Pane
table.setPreferredScrollableViewportSize(new Dimension(910, 420));
table.setFillsViewportHeight(true);
JTableHeader header = table.getTableHeader();
header.setBackground(Color.YELLOW);
JScrollPane pane = new JScrollPane(table);
pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
pane.setHorizontalScrollBarPolicy(JScrollPane .HORIZONTAL_SCROLLBAR_ALWAYS);
//Set AutoResize
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
//Set Column Widths
table.getColumnModel().getColumn(0).setMinWidth(60);
table.getColumnModel().getColumn(1).setMinWidth(190);
table.getColumnModel().getColumn(2).setMinWidth(160);
table.getColumnModel().getColumn(3).setMinWidth(100);
table.getColumnModel().getColumn(4).setMinWidth(400);
table.setAutoCreateRowSorter(true);
panel.add(pane);
frame.add(panel);
frame.setSize(1000,550);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
table.repaint();
//Insert first position
math.connection = math.connect();
//code to retrieve data goes here
model.insertRow(0,new Object[]{InfoID,Company,DateNTime,Reason,Note});
}
}
private GridBagConstraints getConstraints(int gridx, int gridy,
int gridwidth, int gridheight, int anchor)
{
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5, 5, 5, 5);
c.ipadx = 0;
c.ipady = 0;
c.gridx = gridx;
c.gridy = gridy;
c.gridwidth = gridwidth;
c.gridheight = gridheight;
c.anchor = anchor;
return c;
}
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
if ("Exit".equals(command))
System.exit(0);
else if("Home".equals(command));
Home.Home();
if("Refresh".equals(command));
//model.fireTableDataChanged();
}
public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int column = e.getColumn();
TableModel model = (TableModel)e.getSource();
String columnName = model.getColumnName(column);
Object data = model.getValueAt(row, column);
// Do something with the data...
}
}
Re: Refresh Jtable after data has been changed
Out of curousity do people not want to help or do most people not know the answer :/
Re: Refresh Jtable after data has been changed
Re: Refresh Jtable after data has been changed
Quote:
Originally Posted by
kevinn205
Out of curousity do people not want to help or do most people not know the answer :/
Just asking a question doesn't guarantee an answer (especially on a forum where there are unpaid volunteers with hundreds of other posts to deal with)...getting answers is intimately tied to how you ask the question. Make the question easy to understand and answer (succinct, posting an SSCCE, and one single question), you get an answer faster. Make someone work that much harder and your chances drop.
Re: Refresh Jtable after data has been changed
Quote:
Originally Posted by
kevinn205
Out of curousity do people not want to help or do most people not know the answer :/
Most people probably looked at your wall of text and code and stopped reading. Call us lazy, but there are hundreds of posts here waiting on an answer, and we don't have time to get to all of them. So we can either answer 10 well-asked questions (see the advice doWhile already gave you, or see the link in my signature on asking questions the smart way), or we could take the same amount time to answer just 1 code dump. Which do you think is more effective?
Re: Refresh Jtable after data has been changed
Out of curiosity are you going to reply and thank people when they give you help? Its been 11 hours since you got a reply.
You only waited 5 hours before you expected people to answer your question!
Re: Refresh Jtable after data has been changed
Sorry I was finishing up a job for my company;
I was just looking for any help, I didnt post a SSCE because my code is so interactive that it would not even work; therefore i'd have to create from scratch a brand new code that shows the problem; yes perhaps thats what I should have done
with the answer to the few questions-- I would easily be able to solve this
i'm not looking for a handout unless someone convinently had something similiar to post, rather im just looking for a ohh, well it looks like you used the add function to add to your list and then try to update it , your problem is that you need to use a arraylist and then reupdate the arraylist using a comand ; in order to fill the array list try using
string [] = new array list (data)
I was asking for merely statistical reason
Re: Refresh Jtable after data has been changed
Thank you for the post Camerick, It helped a quite a bit.