Results 1 to 6 of 6
- 09-24-2011, 11:54 AM #1
Member
- Join Date
- Jun 2009
- Posts
- 43
- Rep Power
- 0
how to add automatic refresh for Table
kindly help me how to display the current insert data to the Table, or how to automatic
refresh the Table so that the data currently add will display in the Table.
here is the code below
Java Code:import java.awt.*; import java.awt.event.*; import java.awt.Toolkit.*; import javax.swing.*; import javax.swing.JFrame; import javax.swing.border.*; import java.sql.*; import javax.swing.table.DefaultTableModel; import java.util.Vector; class StartForm extends JFrame implements ActionListener{ Connection con; PreparedStatement stat; private Container cpane; private JPanel jp; JTabbedPane jtp; private JLabel lblserialno; private JLabel lblyear; private JLabel lblmonth; private JLabel lblamount; private JTextField txtserialno; private JTextField txtyear; private JTextField txtmonth; private JTextField txtamount; private JButton addbtn; private JButton refreshbtn; private JTable table; private JScrollPane scroll; private DefaultTableModel tmodel; private ResultSet result; StartForm() { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection ("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=masterdata.mdb;DriverID=22}","",""); } catch(Exception e) { System.out.println("there was some error in establishing connection : "+e); } Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); setTitle("Automatation System"); setSize(900,700); setLocationByPlatform(true); setIconImage(new ImageIcon("logo/logo.jpg").getImage()); setLocation((screen.width - 900)/2,((screen.height-700)/2)); //setResizable(false); setVisible(true); cpane = getContentPane(); jtp = new JTabbedPane(); jtp.setFont(new Font("Lucida Grande", Font.BOLD, 11)); jp = new JPanel(); jp.setBorder(BorderFactory.createTitledBorder("")); jp.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED)); jp.setLayout(null); cpane.add(jtp); jtp.addTab("Test1", jp); lblserialno = new JLabel("Serial No:"); lblserialno.setBounds(30, 10, 100, 50); lblserialno.setFont(new Font("Monotype Corsiva", Font.BOLD, 16)); jp.add(lblserialno); lblyear = new JLabel("Year:"); lblyear.setBounds(30, 42, 110, 50); lblyear.setFont(new Font("Monotype Corsiva", Font.BOLD, 16)); jp.add(lblyear); lblmonth = new JLabel("Month :"); lblmonth.setBounds(30, 70, 110, 50); lblmonth.setFont(new Font("Monotype Corsiva", Font.BOLD, 16)); jp.add(lblmonth); lblamount = new JLabel("Amount :"); lblamount.setBounds(30, 100, 110, 50); lblamount.setFont(new Font("Monotype Corsiva", Font.BOLD, 16)); jp.add(lblamount); txtserialno = new JTextField(15); txtserialno.setBounds(160, 22, 180, 25); txtserialno.setFont(new Font("Times Roman",Font.PLAIN,12)); jp.add(txtserialno); txtserialno.addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent e) { char c = e.getKeyChar(); if (!((c >= '0') && (c <= '9') || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE))) { getToolkit().beep(); e.consume(); } } }); txtyear= new JTextField(15); txtyear.setBounds(160, 51, 180, 25); txtyear.setFont(new Font("Times Roman",Font.PLAIN,12)); jp.add(txtyear); txtmonth= new JTextField(15); txtmonth.setBounds(160, 80, 180, 25); txtmonth.setFont(new Font("Times Roman",Font.PLAIN,12)); jp.add(txtmonth); txtamount= new JTextField(15); txtamount.setBounds(160, 110, 180, 25); txtamount.setFont(new Font("Times Roman",Font.PLAIN,12)); jp.add(txtamount); addbtn = new JButton("ADD"); addbtn.setBounds(100, 400, 100, 25); addbtn.setFont(new Font("Monotype Corsiva", Font.PLAIN, 16)); jp.add(addbtn); addbtn.addActionListener(this); refreshbtn = new JButton("Refresh"); refreshbtn.setBounds(200,400,100,25); refreshbtn.setFont(new Font("Monotype Corsiva", Font.PLAIN, 16)); jp.add(refreshbtn); //Table display String s1=" ",s2="",s3=" ",s4=""; String data[][]= new String[100][4]; String col[] = {"sl.no","year","month","amount"}; ResultSet result=null; try { stat = con.prepareStatement("Select * from collection"); result = stat.executeQuery(); int r=0; int c=0; while(result.next()) { s1=result.getString(1); s2=result.getString(2); s3=result.getString(3); s4=result.getString(4); data[r][c++]=s1; data[r][c++]=s2; data[r][c++]=s3; data[r][c++]=s4; r++; c=0; } result.close(); stat.close(); } catch(Exception e) { System.out.println("Could not execute the query"+e); } DefaultTableModel tablemodel = new DefaultTableModel(); for(int i =0;i<col.length;i++) { tablemodel.addColumn(col[i]); } JTable table = new JTable(tablemodel); table.setRowHeight(15); table.setShowGrid(true); table.setEnabled(false); table.setSelectionForeground(Color.RED); table.setSelectionBackground(Color.BLUE); scroll = new JScrollPane(table); scroll.createHorizontalScrollBar(); scroll.setBounds(400,10,450,450); jp.add(scroll); int i=0; while(i<data.length) { Vector data1 = new Vector(); int j=0; while(j<col.length) { data1.addElement(data[i][j]); j++; } tablemodel.addRow(data1); i++; } } public void actionPerformed(ActionEvent e) { if(e.getSource()==addbtn) { AccessUser(); } } public void AccessUser() { try { stat = con.prepareStatement("INSERT INTO collection VALUES(?,?,?,?)"); String slno = txtserialno.getText(); String yearcollect = txtyear.getText(); String monthcollect = txtmonth.getText(); String amountcollect = txtamount.getText(); stat.setString(1, slno); stat.setString(2, yearcollect ); stat.setString(3, monthcollect); stat.setString(4, amountcollect); stat.executeUpdate(); txtserialno.setText(""); txtyear.setText(""); txtmonth.setText(""); txtamount.setText(""); } catch(Exception exp) { System.out.println("Could not execute the query"+ exp); } } /*public void RefreshData(); { String s1=" ",s2="",s3=" ",s4="",s5=" ",s6=""; String data[][]= new String[6][7]; String col[] = {"sl.no","year","date","week","month","amount"}; ResultSet result=null; try { //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Connection con=DriverManager.getConnection("jdbc:odbc:pharmacy","sa","dicky"); //Connection con = DriverManager.getConnection(url); stat = con.prepareStatement("Select * from collection"); result = stat.executeQuery(); int r=0; int c=0; while(result.next()) { s1=result.getString(1); s2=result.getString(2); s3=result.getString(3); s4=result.getString(4); s5=result.getString(5); s6=result.getString(6); data[r][c++]=s1; data[r][c++]=s2; data[r][c++]=s3; data[r][c++]=s4; data[r][c++]=s5; data[r][c++]=s6; r++; c=0; } result.close(); stat.close(); } catch(Exception e) { System.out.println("Could not execute the query"+e); } DefaultTableModel tablemodel = new DefaultTableModel(); for(int i =0;i<col.length;i++) { tablemodel.addColumn(col[i]); } JTable table = new JTable(tablemodel); table.setRowHeight(15); table.setShowGrid(true); table.setSelectionForeground(Color.RED); table.setSelectionBackground(Color.BLUE); JScrollPane scroll = new JScrollPane(table); scroll.createHorizontalScrollBar(); //JPanel panel = new JPanel(); //jp1.setLayout(new BorderLayout()); //jp1.add(scroll,BorderLayout.CENTER); scroll.setBounds(400,10,450,450); jp1.add(scroll); int i=0; while(i<data.length) { Vector data1 = new Vector(); int j=0; while(j<col.length) { data1.addElement(data[i][j]); j++; } tablemodel.addRow(data1); i++; } }*/ public static void main(String arg[]) { StartForm st = new StartForm(); } }Last edited by kiki2009; 09-24-2011 at 12:01 PM.
- 09-24-2011, 12:13 PM #2
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
Re: how to add automatic refresh for Table
One way to make your table refresh would be with implementing a Thread in your program which would call a method after some some time.
Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
-
Re: how to add automatic refresh for Table
I nominate this thread for the most creative code indentation and unreadable code of the day award.
- 09-24-2011, 04:56 PM #4
Member
- Join Date
- Jun 2009
- Posts
- 43
- Rep Power
- 0
Re: how to add automatic refresh for Table
Thank You FlyNn for replying to my qoute, i use new thread to the table and its working.
-
Re: how to add automatic refresh for Table
kiki, I'm glad you've got it working, but you'll probably get better and quicker help if you take the time to format your code well before posting it here. Can you honestly say that your code is easier to read then well-formatted code such as this?
Java Code:import java.awt.*; import java.awt.event.*; import java.awt.Toolkit.*; import javax.swing.*; import javax.swing.JFrame; import javax.swing.border.*; import java.sql.*; import javax.swing.table.DefaultTableModel; import java.util.Vector; class StartForm extends JFrame implements ActionListener { Connection con; PreparedStatement stat; private Container cpane; private JPanel jp; JTabbedPane jtp; private JLabel lblserialno; private JLabel lblyear; private JLabel lblmonth; private JLabel lblamount; private JTextField txtserialno; private JTextField txtyear; private JTextField txtmonth; private JTextField txtamount; private JButton addbtn; private JButton refreshbtn; private JTable table; private JScrollPane scroll; private DefaultTableModel tmodel; private ResultSet result; StartForm() { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager .getConnection( "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=masterdata.mdb;DriverID=22}", "", ""); } catch (Exception e) { System.out .println("there was some error in establishing connection : " + e); } Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); setTitle("Automatation System"); setSize(900, 700); setLocationByPlatform(true); setIconImage(new ImageIcon("logo/logo.jpg").getImage()); setLocation((screen.width - 900) / 2, ((screen.height - 700) / 2)); // setResizable(false); setVisible(true); cpane = getContentPane(); jtp = new JTabbedPane(); jtp.setFont(new Font("Lucida Grande", Font.BOLD, 11)); jp = new JPanel(); jp.setBorder(BorderFactory.createTitledBorder("")); jp.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED)); jp.setLayout(null); cpane.add(jtp); jtp.addTab("Test1", jp); lblserialno = new JLabel("Serial No:"); lblserialno.setBounds(30, 10, 100, 50); lblserialno.setFont(new Font("Monotype Corsiva", Font.BOLD, 16)); jp.add(lblserialno); lblyear = new JLabel("Year:"); lblyear.setBounds(30, 42, 110, 50); lblyear.setFont(new Font("Monotype Corsiva", Font.BOLD, 16)); jp.add(lblyear); lblmonth = new JLabel("Month :"); lblmonth.setBounds(30, 70, 110, 50); lblmonth.setFont(new Font("Monotype Corsiva", Font.BOLD, 16)); jp.add(lblmonth); lblamount = new JLabel("Amount :"); lblamount.setBounds(30, 100, 110, 50); lblamount.setFont(new Font("Monotype Corsiva", Font.BOLD, 16)); jp.add(lblamount); txtserialno = new JTextField(15); txtserialno.setBounds(160, 22, 180, 25); txtserialno.setFont(new Font("Times Roman", Font.PLAIN, 12)); jp.add(txtserialno); txtserialno.addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent e) { char c = e.getKeyChar(); if (!((c >= '0') && (c <= '9') || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE))) { getToolkit().beep(); e.consume(); } } }); txtyear = new JTextField(15); txtyear.setBounds(160, 51, 180, 25); txtyear.setFont(new Font("Times Roman", Font.PLAIN, 12)); jp.add(txtyear); txtmonth = new JTextField(15); txtmonth.setBounds(160, 80, 180, 25); txtmonth.setFont(new Font("Times Roman", Font.PLAIN, 12)); jp.add(txtmonth); txtamount = new JTextField(15); txtamount.setBounds(160, 110, 180, 25); txtamount.setFont(new Font("Times Roman", Font.PLAIN, 12)); jp.add(txtamount); addbtn = new JButton("ADD"); addbtn.setBounds(100, 400, 100, 25); addbtn.setFont(new Font("Monotype Corsiva", Font.PLAIN, 16)); jp.add(addbtn); addbtn.addActionListener(this); refreshbtn = new JButton("Refresh"); refreshbtn.setBounds(200, 400, 100, 25); refreshbtn.setFont(new Font("Monotype Corsiva", Font.PLAIN, 16)); jp.add(refreshbtn); // Table display String s1 = " ", s2 = "", s3 = " ", s4 = ""; String data[][] = new String[100][4]; String col[] = { "sl.no", "year", "month", "amount" }; ResultSet result = null; try { stat = con.prepareStatement("Select * from collection"); result = stat.executeQuery(); int r = 0; int c = 0; while (result.next()) { s1 = result.getString(1); s2 = result.getString(2); s3 = result.getString(3); s4 = result.getString(4); data[r][c++] = s1; data[r][c++] = s2; data[r][c++] = s3; data[r][c++] = s4; r++; c = 0; } result.close(); stat.close(); } catch (Exception e) { System.out.println("Could not execute the query" + e); } DefaultTableModel tablemodel = new DefaultTableModel(); for (int i = 0; i < col.length; i++) { tablemodel.addColumn(col[i]); } JTable table = new JTable(tablemodel); table.setRowHeight(15); table.setShowGrid(true); table.setEnabled(false); table.setSelectionForeground(Color.RED); table.setSelectionBackground(Color.BLUE); scroll = new JScrollPane(table); scroll.createHorizontalScrollBar(); scroll.setBounds(400, 10, 450, 450); jp.add(scroll); int i = 0; while (i < data.length) { Vector data1 = new Vector(); int j = 0; while (j < col.length) { data1.addElement(data[i][j]); j++; } tablemodel.addRow(data1); i++; } } public void actionPerformed(ActionEvent e) { if (e.getSource() == addbtn) { AccessUser(); } } public void AccessUser() { try { stat = con.prepareStatement("INSERT INTO collection VALUES(?,?,?,?)"); String slno = txtserialno.getText(); String yearcollect = txtyear.getText(); String monthcollect = txtmonth.getText(); String amountcollect = txtamount.getText(); stat.setString(1, slno); stat.setString(2, yearcollect); stat.setString(3, monthcollect); stat.setString(4, amountcollect); stat.executeUpdate(); txtserialno.setText(""); txtyear.setText(""); txtmonth.setText(""); txtamount.setText(""); } catch (Exception exp) { System.out.println("Could not execute the query" + exp); } } /* * public void RefreshData() { * * * } */ public static void main(String arg[]) { StartForm st = new StartForm(); } }
- 09-24-2011, 05:16 PM #6
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
Re: how to add automatic refresh for Table
If you do use a thread, I highly recommend understanding the Swing single thread rule, which you can read about at the following link:
Threads and Swing
Similar Threads
-
automatic ENTER?
By glina126 in forum New To JavaReplies: 3Last Post: 02-25-2011, 07:08 AM -
Why the web browser needs to refresh first so that creating table may work? Pls help.
By MarkSquall in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 03-29-2010, 11:14 AM -
Automatic sign in?
By dunafrothint in forum NetworkingReplies: 1Last Post: 03-26-2010, 03:00 PM -
How to repaint.refresh the table (table model) with combo box selection envent
By man4ish in forum AWT / SwingReplies: 1Last Post: 01-08-2010, 06:19 AM -
how to refresh the table when a new account is added and display instantly
By sravanthi narra in forum SWT / JFaceReplies: 4Last Post: 01-05-2008, 07:39 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks