Results 1 to 8 of 8
- 11-16-2011, 09:51 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 65
- Rep Power
- 0
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?
Java 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... } }
- 11-17-2011, 02:24 AM #2
Member
- Join Date
- Nov 2011
- Posts
- 65
- Rep Power
- 0
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 :/
- 11-17-2011, 05:43 AM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Re: Refresh Jtable after data has been changed
- 11-17-2011, 04:21 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: Refresh Jtable after data has been changed
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.
- 11-17-2011, 04:28 PM #5
Re: Refresh Jtable after data has been changed
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?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 11-17-2011, 04:36 PM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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!
- 11-17-2011, 05:09 PM #7
Member
- Join Date
- Nov 2011
- Posts
- 65
- Rep Power
- 0
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 reasonLast edited by kevinn205; 11-17-2011 at 05:13 PM.
- 11-17-2011, 05:25 PM #8
Member
- Join Date
- Nov 2011
- Posts
- 65
- Rep Power
- 0
Similar Threads
-
Refresh JTable data
By pink123 in forum AWT / SwingReplies: 1Last Post: 03-31-2011, 06:18 PM -
Could not able to refresh JTable.
By pratim in forum AWT / SwingReplies: 3Last Post: 03-28-2011, 01:52 PM -
Ye Olde jTable Refresh Question...
By Cynot in forum New To JavaReplies: 4Last Post: 05-26-2010, 11:09 PM -
Dynamic Refresh in JTable every 5 Minutes
By britto_bicsjohn in forum AWT / SwingReplies: 3Last Post: 08-07-2009, 05:16 AM -
how to refresh data of the JTable
By paty in forum JDBCReplies: 3Last Post: 08-17-2008, 12:01 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks