J have two classes, listed below, one is table model, and second show data from database in JTable, and it's OK, but after adding a row into database table does't update. How update JTable after adding a row into database?
package djile pak.java clss.for_table; /** Class which creates table model */ import javax.swing.JTable; import javax.swing.table.AbstractTableModel; public class ModelDataTabele extends AbstractTableModel { private String[] columnNames; private Object[][] data; private boolean edit table, change data; public ModelDataTabele(String[] naziviKolona, Object[][] podaci) { columnNames = naziviKolona; data = podaci; edit_table = tedit; change_data = dchange; } public void tableChanged() { fireTableDataChanged(); } public int getColumnCount() { return columnNames.length; } public int getRowCount() { return data.length; } public String getColumnName(int col) { return columnNames[col]; } public Object getValueAt(int row, int col) { return data[row][col]; } public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); } public boolean isCellEditable(int row, int col) { if (col < 2) { return false; } else { return true; } } public void setValueAt(Object value, int row, int col) { data[row][col] = value; fireTableCellUpdated(row, col); } public void insertRow(int row, Object[] npodaci) { for(int i = 0; i < npodaci.length; i++) { data[row][i] = npodaci[i]; } } } // Form which shows data in JTable from database import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.AbstractTableModel; import javax.swing.table.TableColumn; import javax.swing.event.*; import java.sql.*; import djile pak.java clss.for_screen.*; import djile pak.java clss.for_table.*; import djile pak.java clss.for_mysql.*; public class DodajDlg extends JDialog implements ActionListener, FocusListener, KeyListener { JPanel gornjiPanel = new JPanel(); JPanel centralniPanel = new JPanel(); JPanel donjiPanel = new JPanel(); JScrollPane zaTabelu; JLabel lblAutor = new JLabel("IME I PREZIME AUTORA: "); JTextField txtAutor = new JTextField(100); JButton btnPotvrdi = new JButton("Potrdi"); JButton btnOdustani = new JButton("Odustani"); JTable tabela; ModelDataTabele mdt; TableColumn kolona; ConnectMySQL vezaDB; Connection Konekcija; Statement sqlKomanda; String upit; ResultSet rs; String[] nazivi; Object[][] podaci; private static int dodajsta; boolean odustani = false; static String[] parametri_vb; int[] wih; int rb, idna; String naziva; public DodajDlg(String naslov, int stasedodaje, String[] parametri_veze) { dodajsta = stasedodaje; parametri_vb = parametri_veze; setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setTitle(naslov); int w = 0, h =0, koeficijent_w = 100, koeficijent_h = 100; PutFrameTo width_height = new PutFrameTo(w, h); wih = width_height.Scr_WH(); switch (dodajsta) { case 1: case 2: koeficijent_w = 95; koeficijent_h = 95; break; } w = (wih[0] / 100) * koeficijent_w; h = (wih[1] / 100) * koeficijent_h; setSize(w, h); PutFrameTo pozicija = new PutFrameTo(w, h); int[] xiy = pozicija.XY_Pos(); setResizable(false); setLocation(xiy[0], xiy[1]); setModal(true); gornjiPanel.setBackground(Color.lightGray); switch (dodajsta) { case 1: break; case 2: this.formaAutor(); break; } add(gornjiPanel, BorderLayout.NORTH); btnPotvrdi.setMnemonic(KeyEvent.VK_P); btnPotvrdi.setEnabled(false); btnOdustani.setMnemonic(KeyEvent.VK_O); btnOdustani.setEnabled(true); btnPotvrdi.addActionListener(this); btnPotvrdi.addFocusListener(this); btnPotvrdi.addKeyListener(this); btnOdustani.addActionListener(this); btnOdustani.addFocusListener(this); btnOdustani.addKeyListener(this); centralniPanel.setBackground(Color.lightGray); centralniPanel.add(btnPotvrdi); centralniPanel.add(btnOdustani); add(centralniPanel, BorderLayout.CENTER); napraviTabelu(); donjiPanel.setBackground(Color.darkGray); zaDonjiPanel(); add(donjiPanel, BorderLayout.SOUTH); setVisible(true); } public void napraviTabelu() { switch (dodajsta) { case 1: break; case 2: int sirinaID = (wih[0] / 100) * 10; int brslogova = brojSlogova(); nazivi = new String[3]; nazivi[0] = "RB"; nazivi[1] = "ID"; nazivi[2] = "Autor"; if (brslogova == 0) { podaci = new Object[1][3]; podaci[0][0] = 1; podaci[0][1] = 0; podaci[0][2] = ""; } else { podaci = new Object[brslogova][3]; procitajPodatke(); } mdt = new ModelDataTabele(nazivi, podaci); tabela = new JTable(mdt); for (int j = 0; j < tabela.getColumnCount(); j++) { kolona = tabela.getColumnModel().getColumn(j); if (j == 0) { kolona.setPreferredWidth(50); } else if (j == 1) { kolona.setPreferredWidth(sirinaID); } else { kolona.setPreferredWidth(wih[0] - (sirinaID + 50)); } } break; } } private void formaAutor() { txtAutor.setToolTipText("Upisati IME i PREZIME autora"); txtAutor.addKeyListener(this); gornjiPanel.add(lblAutor); gornjiPanel.add(txtAutor); } public void actionPerformed(ActionEvent event) { Object akter = event.getSource(); if (akter == btnOdustani) { dispose(); } if (akter == btnPotvrdi) { pozoviUpis(); napraviTabelu(); } } private void zaDonjiPanel() { int sirina = 0; int visina = 0; switch (dodajsta) { case 1: break; case 2: sirina = (wih[0] / 100) * 93; visina = (wih[1] / 100) * 75; break; } Font zaglavljeFont = new Font("DialogInput", Font.BOLD, 15); tabela.getTableHeader().setFont(zaglavljeFont); zaTabelu = new JScrollPane(tabela); zaTabelu.setPreferredSize(new Dimension(sirina, visina)); tabela.setFillsViewportHeight(true); tabela.setSelectionForeground(Color.white); tabela.setSelectionBackground(Color.blue); tabela.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); donjiPanel.add(zaTabelu); } private int brojSlogova() { int brojsl = 0; if (poveziSeSaBazom()) { switch (dodajsta) { case 1: break; case 2: try { sqlKomanda = Konekcija.createStatement(); upit = "select count(*) from autor"; rs = null; rs = sqlKomanda.executeQuery(upit); if (rs != null) { rs.next(); brojsl = rs.getInt(1); } rs.close(); sqlKomanda.close(); } catch (SQLException esql) { String poruka = "Gre\u0161ka: " + esql.toString() + "\n" + esql.getMessage(); JOptionPane.showMessageDialog(null, poruka, "Gre\u0161ka", JOptionPane.WARNING_MESSAGE); } vezaDB.prekiniVezuSaBazom(Konekcija); break; } } return brojsl; } private void pozoviUpis() { if (poveziSeSaBazom()) { upisiUBazu(); vezaDB.prekiniVezuSaBazom(Konekcija); }else { JOptionPane.showMessageDialog(null, "PODACI NISU UPISANI!", "Gre\u0161ka", JOptionPane.WARNING_MESSAGE); } } private boolean poveziSeSaBazom() { vezaDB = new ConnectMySQL(); Konekcija = vezaDB.konekcijaSaBazom(parametri_vb[0], parametri_vb[1], parametri_vb[2], parametri_vb[3]); if (Konekcija != null) { return true; } else { return false; } } private void upisiUBazu() { switch (dodajsta) { case 1: break; case 2: try { sqlKomanda = Konekcija.createStatement(); upit = "insert into autor(ida, autor) values(?,?)"; PreparedStatement upisi = Konekcija.prepareStatement(upit); upisi.setInt(1, 0); upisi.setString(2, txtAutor.getText()); upisi.executeUpdate(); upisi.close(); sqlKomanda.close(); } catch (SQLException esql) { String poruka = "Gre\u0161ka: " + esql.toString() + "\n" + esql.getMessage(); JOptionPane.showMessageDialog(null, poruka, "Gre\u0161ka", JOptionPane.WARNING_MESSAGE); } txtAutor.setText(""); break; } btnPotvrdi.setEnabled(false); } private void procitajPodatke() { if (poveziSeSaBazom()) { switch (dodajsta) { case 1: break; case 2: try { sqlKomanda = Konekcija.createStatement(); upit = "select ida, autor from autor order by autor"; rs = null; rs = sqlKomanda.executeQuery(upit); if (rs != null) { int i = 0; while (rs.next()) { podaci[i][0] = i + 1; podaci[i][1] = rs.getInt("ida"); podaci[i][2] = rs.getString("autor"); i++; } } rs.close(); sqlKomanda.close(); } catch (SQLException esql) { String poruka = "Gre\u0161ka: " + esql.toString() + "\n" + esql.getMessage(); JOptionPane.showMessageDialog(null, poruka, "Gre\u0161ka", JOptionPane.WARNING_MESSAGE); } vezaDB.prekiniVezuSaBazom(Konekcija); break; } } } }
Hello Friend, Try the following code:
import java.sql.*; import java.util.*; import javax.swing.*; import javax.swing.table.*; public class UpdateJTable extends TimerTask { private static JTable table = new JTable(); private static Vector columnNames = new Vector(); private static Vector data = new Vector(); private static JFrame f = new JFrame(); private static JScrollPane scrollPane = new JScrollPane(); public static void main(String[] args) { java.util.Timer timer = new java.util.Timer(); timer.schedule(new UpdateJTable(), 0, 10000); } public void run() { remove(); table = draw(); scrollPane.getViewport().add(table, 0); f.getContentPane().add(scrollPane); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); } public static JTable draw() { try { Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection con = DriverManager.getConnection( "jdbc:mysql://localhost/test", "root", "root"); String sql = "Select * from employee"; Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(sql); ResultSetMetaData md = rs.getMetaData(); int columns = md.getColumnCount(); for (int i = 1; i <= columns; i++) { columnNames.addElement(md.getColumnName(i)); } while (rs.next()) { Vector row = new Vector(columns); for (int i = 1; i <= columns; i++) { row.addElement(rs.getObject(i)); } data.addElement(row); } rs.close(); stmt.close(); } catch (Exception e) { System.out.println(e); e.printStackTrace(); } table.setModel(new DefaultTableModel(data, columnNames)); TableColumn col; table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); for (int i = 0; i < table.getColumnCount(); i++) { col = table.getColumnModel().getColumn(i); col.setMaxWidth(col.getMaxWidth()); } return table; } public static void remove() { columnNames.clear(); if (table.getRowCount() != 0) { DefaultTableModel dm = (DefaultTableModel) table.getModel(); dm.getDataVector().removeAllElements(); } } }
Thanks
Ads