fireTableRowsUpdated() Not work after update done in JTable [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
In my JTable, after update done, it need to refresh to show the changes:
public class RecordTableGUI extends JFrame implements ActionListener {
private String newName;
private JTable table;
private RecordTableModel myModel;
private JButton editButton;
public RecordTableGUI() {
myModel = new RecordTableModel();
table = new JTable(myModel);
add(new JScrollPane(table), BorderLayout.CENTER);
add(buttonsPanel(), BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(700, 550);
setLocation(300, 80);
setVisible(true);
}
public JPanel buttonsPanel() {
JPanel bPanel = new JPanel();
editButton = new JButton("Edit");
editButton.addActionListener(this);
bPanel.add(editButton);
return bPanel;
}
#Override
public void actionPerformed(ActionEvent e) {
if (table.getSelectedRow() > -1) {
Object oldName = table.getValueAt(table.getSelectedRow(), 1);
UpdateGUIDialog updDialog = new UpdateGUIDialog(this,String.valueOf(oldName), this);
int rowToEdit = table.getSelectedRow();
int rowToModel = table.convertRowIndexToView(rowToEdit);
Object nameID = table.getValueAt(table.getSelectedRow(), 0);
myModel.updateRow(rowToModel, nameID, getNewName());
} else {
JOptionPane.showMessageDialog(null, "Select a row");
}
}
}
Model Class:
public class RecordTableModel extends AbstractTableModel {
Connection con;
Statement statement;
ResultSet result;
String dbUrl = "jdbc:mysql://localhost/mydb";
String query = "Select * from mytable";
ArrayList<String> cols = new ArrayList<String>();
ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
public RecordTableModel() {
try {
con = DriverManager.getConnection(dbUrl, "root", "2323");
statement = con.createStatement();
result = statement.executeQuery(query);
int c = result.getMetaData().getColumnCount();
for (int i = 1; i <= c; i++) {
cols.add(result.getMetaData().getColumnName(i));
}
while (result.next()) {
ArrayList<String> eachRow = new ArrayList<String>();
for (int i = 1; i <= c; i++) {
eachRow.add(result.getString(i));
}
data.add(eachRow);
}
} catch (SQLException sqle) {
sqle.printStackTrace();
} finally {
try {
if (con != null) {
con.close();
}
if (statement != null) {
statement.close();
}
} catch (SQLException sqlee) {
sqlee.printStackTrace();
}
}
}
#Override
public int getRowCount() {
return data.size();
}
#Override
public int getColumnCount() {
return cols.size();
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
ArrayList<String> selectedRow = data.get(rowIndex);
return selectedRow.get(columnIndex);
}
#Override
public String getColumnName(int col) {
return cols.get(col);
}
public void updateRow(int modelRow, Object nameID, Object newName) {
String query = "update mytable set name = '" + newName + "' where id = " + nameID;
Connection conn;
PreparedStatement pstate;
try {
conn = DriverManager.getConnection(dbUrl, "root", "2323");
pstate = conn.prepareStatement(query);
pstate.executeUpdate();
fireTableRowsUpdated(tableRow, tableRow);
fireTableDataChanged();
fireTableCellUpdated(modelRow, 1);
} catch (SQLException sql) {
sql.printStackTrace();
}
}

Here's your original code:
public class RecordTableModel extends AbstractTableModel {
...
public void updateRow(int modelRow,...) {
String query = ...;
Connection conn;
PreparedStatement pstate;
try {
conn = DriverManager.getConnection(...);
pstate = conn.prepareStatement(query);
pstate.executeUpdate();
fireTableRowsUpdated(modelRow, modelRow); // Not Work!
fireTableDataChanged(); // Not Work!
fireTableCellUpdated(modelRow, 1); // Not Work!
} catch (SQLException sql) {
sql.printStackTrace();
}
}
This code does nothing to the data held by the table model itself, and so it should come as no surprise that calling fireTableXXX(...) does nothing. If the model is unchanged, you can fire anything you want, and the table won't change.
You should perhaps not be using executeUpdate but executeQuery so you can get a ResultSet from the database, and then use it to update the data held by your table model. Then call your appropriate fireTableXXX(...) method.

Related

how to retrieve data from database to table using model view controller(mvc) model in swing [duplicate]

I'm making a GUI Project for database there are two classes which are for GUI's. And connector class is used to connect from user credentials. If credentials are correct than it fetch all data in the from of AbstractTableModel. When program run first GUI has a button in which we click it and it fetch all data in underlying TableModel. But i'm facing two problems. First in GUI2 class, sometimes it open like this.
and sometimes it show like this
I don't know why it's happening. And second problem is when we select any row from table and click on DeleteSelectedRow button it delete the row. This button has a ActionListener in GUI2 class. But what i want is i automatic update the table when row has been deleted. How can i do that?
class for first GUI
public class Gui extends JFrame {
private static Connector conni;
private Connection conn = null;
private JButton bt;
private JPanel panel;
public Gui() {
super("Frame");
panel = new JPanel();
bt = new JButton("Connect to Database 'World'");
panel.add(bt);
bt.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
conn = conni.Connector();
if (conn != null) {
dispose();
new Gui2(conn);
} else {
System.out.println("Return false");
}
}
});
add(panel);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
}
Connector class
public class Connector {
private static Connection conn = null;
public static Connection Connector() {
String data = "jdbc:mysql://localhost/world";
String user = "root";
String pass = "toot";
try {
conn = DriverManager.getConnection(data, user, pass);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
if (conn != null) {
System.out.println("Connection Suceess");
return conn;
} else {
return conn;
}
}
}
class for second GUI2
public class Gui2 extends JFrame {
private Statement state = null;
private ResultSet rs = null;
private JButton bt, delete;
private JTextField text;
private JPanel panel;
private GridBagLayout layout;
private GridBagConstraints constraints;
public Gui2(Connection conn) {
layout = new GridBagLayout();
constraints = new GridBagConstraints();
panel = new JPanel();
panel.setLayout(layout);
text = new JTextField(15);
bt = new JButton("Submit Query");
delete = new JButton("Delete Selected Row");
constraints.insets = new Insets(5, 2, 5, 10);
constraints.gridy = 0;// row 0
constraints.gridx = 0;// column 0
// TextField add on JPanel with given constraints
panel.add(text, constraints);
constraints.gridx++;
panel.add(delete, constraints);
constraints.gridx++;
panel.add(bt, constraints);
// North BorderLayout
add(panel, BorderLayout.NORTH);
try {
state = conn.createStatement();
rs = state.executeQuery("select * from city");
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
JTable table = new JTable();
JScrollPane spane = new JScrollPane(table);
add(spane, BorderLayout.CENTER);
table.setModel(new TableModel(rs));
delete.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
int rowIndex = table.getSelectedRow();
Object columnIndexValue = table.getModel().getValueAt(rowIndex, 0);
String columnName = table.getModel().getColumnName(0);
String query = "delete from world.city" + " where " + columnName + "=" + columnIndexValue;
try {
PreparedStatement pre = conn.prepareStatement(query);
pre.executeUpdate();
JOptionPane.showMessageDialog(null, "Row Deleted Successfully");
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, e1.getMessage());
}
}
});
setSize(817, 538);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
}
Tablemodel Class
public class TableModel extends AbstractTableModel {
private List ColumnHeader;
private List tableData;
private List rowData;
private int totalcolumn;
public TableModel(ResultSet rs) {
try {
ResultSetMetaData meta = rs.getMetaData();
totalcolumn = meta.getColumnCount();
ColumnHeader = new ArrayList(totalcolumn);
tableData = new ArrayList();
for (int i = 1; i <= totalcolumn; i++) {
ColumnHeader.add(meta.getColumnName(i));
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
SwingWorker<Boolean, List<Object>> worker = new SwingWorker<Boolean, List<Object>>() {
#Override
protected Boolean doInBackground() throws Exception {
while (rs.next()) {
rowData = new ArrayList(totalcolumn);
for (int i = 1; i <= totalcolumn; i++) {
rowData.add(rs.getObject(i));
}
publish(rowData);
}
return true;
}
#Override
protected void process(List chunks) {
tableData.add(chunks);
}
#Override
protected void done() {
try {
Boolean status = get();
JOptionPane.showMessageDialog(null, "Task is DONE");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
};
worker.execute();
}// constructor end
#Override
public int getColumnCount() {
return ColumnHeader.size();
}
public String getColumnName(int columnIndex) {
return (String) ColumnHeader.get(columnIndex);
}
#Override
public int getRowCount() {
return tableData.size();
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
List rowData2 = (List) tableData.get(rowIndex);
return rowData2.get(columnIndex);
}
}
Because database access is inherently asynchronous, you'll surely want to retrieve rows in the background to avoid blocking the event dispatch thread; SwingWorker makes this relatively easy. Fetch rows in your implementation of doInBackground(), publish() interim results, and add them to the table model in your implementation of process(). A complete example that outlines the attendant benefits is shown here. The example loops through a file, but you can substitute your ResultSet operations.
while (rs.next()) {
//collect row data
publish(rowData);
}
Defer tableData.add() to your implementation of process().
Focusing on the interaction between the custom TableModel and its contained SwingWorker, the following complete example creates a test database having N rows and displays a JTable showing the results of a query of that table. In particular,
JDBCModel extends AbstractTableModel. For simplicity, the model's data is stored in a List<Row>, and ResultSetMetaData is used for the column names. As a more abstract alternative, see Apache Commons DbUtils, which uses Class Literals as Runtime-Type Tokens and ResultSetMetaData to safely create instances of row data.
JDBCModel delegates row retrieval to a private JDBCWorker; it invokes publish() on each row retrieved from the ResultSet; because process() runs on the EDT, the worker can optimize the number of table model events that it fires on behalf of the parent model using fireTableRowsInserted().
Similarly, your implementation of delete() should reside in JDBCModel, not the GUI; it should fireTableRowsDeleted() after the row is successfully deleted from the database and removed from data.
Add Thread.sleep() to the worker's background loop to see the effect of artificially increasing latency.
Use setProgress() and a PropertyChangeListener, shown here, to display progress; a JOptionPane when done() may be superfluous.
Override getPreferredScrollableViewportSize() to customize the size of the table's enclosing JScrollPane.
Avoid class names, e.g. TableModel, that collide with common API names.
A variation that implements live filtering in the view is examined here.
import java.awt.Dimension;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingWorker;
import javax.swing.table.AbstractTableModel;
/**
* #see https://stackoverflow.com/a/34742409/230513
* #see https://stackoverflow.com/a/24762078/230513
*/
public class WorkerTest {
private static final int N = 1_000;
private static final String URL = "jdbc:h2:mem:test";
private static final Random r = new Random();
private void display() {
JFrame f = new JFrame("WorkerTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createTestDatabase(N);
JDBCModel model = new JDBCModel(getConnection(), "select * from city");
f.add(new JScrollPane(new JTable(model) {
#Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(320, 240);
}
}));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private static class Row {
int ID;
String name;
}
private static class JDBCModel extends AbstractTableModel {
private final List<Row> data = new ArrayList<>();
private ResultSet rs = null;
private ResultSetMetaData meta;
public JDBCModel(Connection conn, String query) {
try {
Statement s = conn.createStatement();
rs = s.executeQuery(query);
meta = rs.getMetaData();
JDBCWorker worker = new JDBCWorker();
worker.execute();
} catch (SQLException e) {
e.printStackTrace(System.err);
}
}
#Override
public int getRowCount() {
return data.size();
}
#Override
public int getColumnCount() {
try {
return meta.getColumnCount();
} catch (SQLException e) {
e.printStackTrace(System.err);
}
return 0;
}
#Override
public Object getValueAt(int rowIndex, int colIndex) {
Row row = data.get(rowIndex);
switch (colIndex) {
case 0:
return row.ID;
case 1:
return row.name;
}
return null;
}
#Override
public String getColumnName(int colIndex) {
try {
return meta.getColumnName(colIndex + 1);
} catch (SQLException e) {
e.printStackTrace(System.err);
}
return null;
}
private class JDBCWorker extends SwingWorker<List<Row>, Row> {
#Override
protected List<Row> doInBackground() {
try {
while (rs.next()) {
Row r = new Row();
r.ID = rs.getInt(1);
r.name = rs.getString(2);
publish(r);
}
} catch (SQLException e) {
e.printStackTrace(System.err);
}
return data;
}
#Override
protected void process(List<Row> chunks) {
int n = getRowCount();
for (Row row : chunks) {
data.add(row);
}
fireTableRowsInserted(n, n + chunks.size());
}
}
}
private static void createTestDatabase(int n) {
Connection conn = getConnection();
try {
Statement st = conn.createStatement();
st.execute("create table city(id integer, name varchar2)");
PreparedStatement ps = conn.prepareStatement(
"insert into city values (?, ?)");
for (int i = 0; i < n; i++) {
ps.setInt(1, i);
ps.setString(2, (char) ('A' + r.nextInt(26))
+ String.valueOf(r.nextInt(1_000_000)));
ps.execute();
}
} catch (SQLException ex) {
ex.printStackTrace(System.err);
}
}
private static Connection getConnection() {
try {
return DriverManager.getConnection(URL, "", "");
} catch (SQLException e) {
e.printStackTrace(System.err);
}
return null;
}
public static void main(String[] args) {
EventQueue.invokeLater(new WorkerTest()::display);
}
}

Display database table with swing (Columns names do not show up) [duplicate]

I'm making a GUI Project for database there are two classes which are for GUI's. And connector class is used to connect from user credentials. If credentials are correct than it fetch all data in the from of AbstractTableModel. When program run first GUI has a button in which we click it and it fetch all data in underlying TableModel. But i'm facing two problems. First in GUI2 class, sometimes it open like this.
and sometimes it show like this
I don't know why it's happening. And second problem is when we select any row from table and click on DeleteSelectedRow button it delete the row. This button has a ActionListener in GUI2 class. But what i want is i automatic update the table when row has been deleted. How can i do that?
class for first GUI
public class Gui extends JFrame {
private static Connector conni;
private Connection conn = null;
private JButton bt;
private JPanel panel;
public Gui() {
super("Frame");
panel = new JPanel();
bt = new JButton("Connect to Database 'World'");
panel.add(bt);
bt.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
conn = conni.Connector();
if (conn != null) {
dispose();
new Gui2(conn);
} else {
System.out.println("Return false");
}
}
});
add(panel);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
}
Connector class
public class Connector {
private static Connection conn = null;
public static Connection Connector() {
String data = "jdbc:mysql://localhost/world";
String user = "root";
String pass = "toot";
try {
conn = DriverManager.getConnection(data, user, pass);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
if (conn != null) {
System.out.println("Connection Suceess");
return conn;
} else {
return conn;
}
}
}
class for second GUI2
public class Gui2 extends JFrame {
private Statement state = null;
private ResultSet rs = null;
private JButton bt, delete;
private JTextField text;
private JPanel panel;
private GridBagLayout layout;
private GridBagConstraints constraints;
public Gui2(Connection conn) {
layout = new GridBagLayout();
constraints = new GridBagConstraints();
panel = new JPanel();
panel.setLayout(layout);
text = new JTextField(15);
bt = new JButton("Submit Query");
delete = new JButton("Delete Selected Row");
constraints.insets = new Insets(5, 2, 5, 10);
constraints.gridy = 0;// row 0
constraints.gridx = 0;// column 0
// TextField add on JPanel with given constraints
panel.add(text, constraints);
constraints.gridx++;
panel.add(delete, constraints);
constraints.gridx++;
panel.add(bt, constraints);
// North BorderLayout
add(panel, BorderLayout.NORTH);
try {
state = conn.createStatement();
rs = state.executeQuery("select * from city");
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
JTable table = new JTable();
JScrollPane spane = new JScrollPane(table);
add(spane, BorderLayout.CENTER);
table.setModel(new TableModel(rs));
delete.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
int rowIndex = table.getSelectedRow();
Object columnIndexValue = table.getModel().getValueAt(rowIndex, 0);
String columnName = table.getModel().getColumnName(0);
String query = "delete from world.city" + " where " + columnName + "=" + columnIndexValue;
try {
PreparedStatement pre = conn.prepareStatement(query);
pre.executeUpdate();
JOptionPane.showMessageDialog(null, "Row Deleted Successfully");
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, e1.getMessage());
}
}
});
setSize(817, 538);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
}
Tablemodel Class
public class TableModel extends AbstractTableModel {
private List ColumnHeader;
private List tableData;
private List rowData;
private int totalcolumn;
public TableModel(ResultSet rs) {
try {
ResultSetMetaData meta = rs.getMetaData();
totalcolumn = meta.getColumnCount();
ColumnHeader = new ArrayList(totalcolumn);
tableData = new ArrayList();
for (int i = 1; i <= totalcolumn; i++) {
ColumnHeader.add(meta.getColumnName(i));
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
SwingWorker<Boolean, List<Object>> worker = new SwingWorker<Boolean, List<Object>>() {
#Override
protected Boolean doInBackground() throws Exception {
while (rs.next()) {
rowData = new ArrayList(totalcolumn);
for (int i = 1; i <= totalcolumn; i++) {
rowData.add(rs.getObject(i));
}
publish(rowData);
}
return true;
}
#Override
protected void process(List chunks) {
tableData.add(chunks);
}
#Override
protected void done() {
try {
Boolean status = get();
JOptionPane.showMessageDialog(null, "Task is DONE");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
};
worker.execute();
}// constructor end
#Override
public int getColumnCount() {
return ColumnHeader.size();
}
public String getColumnName(int columnIndex) {
return (String) ColumnHeader.get(columnIndex);
}
#Override
public int getRowCount() {
return tableData.size();
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
List rowData2 = (List) tableData.get(rowIndex);
return rowData2.get(columnIndex);
}
}
Because database access is inherently asynchronous, you'll surely want to retrieve rows in the background to avoid blocking the event dispatch thread; SwingWorker makes this relatively easy. Fetch rows in your implementation of doInBackground(), publish() interim results, and add them to the table model in your implementation of process(). A complete example that outlines the attendant benefits is shown here. The example loops through a file, but you can substitute your ResultSet operations.
while (rs.next()) {
//collect row data
publish(rowData);
}
Defer tableData.add() to your implementation of process().
Focusing on the interaction between the custom TableModel and its contained SwingWorker, the following complete example creates a test database having N rows and displays a JTable showing the results of a query of that table. In particular,
JDBCModel extends AbstractTableModel. For simplicity, the model's data is stored in a List<Row>, and ResultSetMetaData is used for the column names. As a more abstract alternative, see Apache Commons DbUtils, which uses Class Literals as Runtime-Type Tokens and ResultSetMetaData to safely create instances of row data.
JDBCModel delegates row retrieval to a private JDBCWorker; it invokes publish() on each row retrieved from the ResultSet; because process() runs on the EDT, the worker can optimize the number of table model events that it fires on behalf of the parent model using fireTableRowsInserted().
Similarly, your implementation of delete() should reside in JDBCModel, not the GUI; it should fireTableRowsDeleted() after the row is successfully deleted from the database and removed from data.
Add Thread.sleep() to the worker's background loop to see the effect of artificially increasing latency.
Use setProgress() and a PropertyChangeListener, shown here, to display progress; a JOptionPane when done() may be superfluous.
Override getPreferredScrollableViewportSize() to customize the size of the table's enclosing JScrollPane.
Avoid class names, e.g. TableModel, that collide with common API names.
A variation that implements live filtering in the view is examined here.
import java.awt.Dimension;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingWorker;
import javax.swing.table.AbstractTableModel;
/**
* #see https://stackoverflow.com/a/34742409/230513
* #see https://stackoverflow.com/a/24762078/230513
*/
public class WorkerTest {
private static final int N = 1_000;
private static final String URL = "jdbc:h2:mem:test";
private static final Random r = new Random();
private void display() {
JFrame f = new JFrame("WorkerTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createTestDatabase(N);
JDBCModel model = new JDBCModel(getConnection(), "select * from city");
f.add(new JScrollPane(new JTable(model) {
#Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(320, 240);
}
}));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private static class Row {
int ID;
String name;
}
private static class JDBCModel extends AbstractTableModel {
private final List<Row> data = new ArrayList<>();
private ResultSet rs = null;
private ResultSetMetaData meta;
public JDBCModel(Connection conn, String query) {
try {
Statement s = conn.createStatement();
rs = s.executeQuery(query);
meta = rs.getMetaData();
JDBCWorker worker = new JDBCWorker();
worker.execute();
} catch (SQLException e) {
e.printStackTrace(System.err);
}
}
#Override
public int getRowCount() {
return data.size();
}
#Override
public int getColumnCount() {
try {
return meta.getColumnCount();
} catch (SQLException e) {
e.printStackTrace(System.err);
}
return 0;
}
#Override
public Object getValueAt(int rowIndex, int colIndex) {
Row row = data.get(rowIndex);
switch (colIndex) {
case 0:
return row.ID;
case 1:
return row.name;
}
return null;
}
#Override
public String getColumnName(int colIndex) {
try {
return meta.getColumnName(colIndex + 1);
} catch (SQLException e) {
e.printStackTrace(System.err);
}
return null;
}
private class JDBCWorker extends SwingWorker<List<Row>, Row> {
#Override
protected List<Row> doInBackground() {
try {
while (rs.next()) {
Row r = new Row();
r.ID = rs.getInt(1);
r.name = rs.getString(2);
publish(r);
}
} catch (SQLException e) {
e.printStackTrace(System.err);
}
return data;
}
#Override
protected void process(List<Row> chunks) {
int n = getRowCount();
for (Row row : chunks) {
data.add(row);
}
fireTableRowsInserted(n, n + chunks.size());
}
}
}
private static void createTestDatabase(int n) {
Connection conn = getConnection();
try {
Statement st = conn.createStatement();
st.execute("create table city(id integer, name varchar2)");
PreparedStatement ps = conn.prepareStatement(
"insert into city values (?, ?)");
for (int i = 0; i < n; i++) {
ps.setInt(1, i);
ps.setString(2, (char) ('A' + r.nextInt(26))
+ String.valueOf(r.nextInt(1_000_000)));
ps.execute();
}
} catch (SQLException ex) {
ex.printStackTrace(System.err);
}
}
private static Connection getConnection() {
try {
return DriverManager.getConnection(URL, "", "");
} catch (SQLException e) {
e.printStackTrace(System.err);
}
return null;
}
public static void main(String[] args) {
EventQueue.invokeLater(new WorkerTest()::display);
}
}

nullpointer exception on table.getSelectedRow()

I have a class A and a class B.
In class A there is a constructor:
public A() {
getSelectedRow();
}
This constructor calls:
public int getSelectedRow() {
System.out.println("The row is : " + table.getSelectedRow());
return table.getSelectedRow();
}
Up to here everything works fine!
The class B then calls the method getSelectedRow() like that:
A results = new A();
System.out.println("YEAH! IT'S: " + results.getSelectedRow());
I just want to find out the selected table row from class A. The problem is that I am getting a null pointer exception and i dont know why. if I dont call the method everything works fine.
CLASS A:
public class AllResultsFromDB extends JFrame {
#SuppressWarnings("compatibility:9056676689615464658")
private static final long serialVersionUID = 188850508334531506L;
GUI ins = new GUI();
JTable table;
public AllResultsFromDB(GUI x) {
final Vector columnNames = new Vector();
final Vector data = new Vector();
this.ins = x;
try {
/** Initializing GUI class
* in order to call
* getSelectedTable() method. **/
Login sgui = new Login();
String dburl = "jdbc:oracle:thin:#localhost:1521:ORCL";
Connection connection = DriverManager.getConnection(dburl, sgui.getUsername(), sgui.getPassword());
// Fetch data from table specified by user
String query = "SELECT * FROM " + ins.getSelectedTable() + " ORDER BY id";
System.out.println(query);
Statement stmt = connection.createStatement();
ResultSet rset = stmt.executeQuery(query);
ResultSetMetaData metad = rset.getMetaData();
int columns = metad.getColumnCount();
// This loop gets the names of the columns
for (int i = 1; i <= columns; i++) {
columnNames.addElement(metad.getColumnName(i));
}
// This loop gets the data inside the rows
while (rset.next()) {
final Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement(rset.getObject(i));
}
data.addElement(row);
}
rset.close();
stmt.close();
connection.close();
// Create table with results
table = new JTable(data, columnNames) {
public boolean isCellEditable(int row, int col) {
return false;
}
public Class getColumnClass(int column) {
for (int row = 0; row < getRowCount(); row++) {
Object obj = getValueAt(row, column);
if (obj != null) {
return obj.getClass();
}
}
return Object.class;
}
};
JScrollPane scroll = new JScrollPane(table);
getContentPane().add(scroll);
JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.SOUTH);
table.addMouseListener(new MouseListener() {
public void mousePressed(MouseEvent e) {
//System.out.println(table.getSelectedRow());
}
public void mouseReleased(MouseEvent e) {
//System.out.println(table.getSelectedRow());
}
public void mouseEntered(MouseEvent e) {
//System.out.println(table.getSelectedRow());
}
public void mouseExited(MouseEvent e) {
//System.out.println(table.getSelectedRow());
}
public void mouseClicked(MouseEvent e) {
getSelectedRow();
if (e.getClickCount() == 2) {
//System.out.println(table.getSelectedRow());
Profile profile = new Profile();
try {
profile.getData();
//wait(500000);
profile.getImage();
} catch (Exception f) {
}
profile.setVisible(true);
}
}
});
} catch (SQLException e) {
}
}
public AllResultsFromDB(int x) {
x = getSelectedRow();
System.out.println(table.getSelectedRow());
}
public int getSelectedRow() {
System.out.println("The row is : " + table.getSelectedRow());
return table.getSelectedRow();
}
}
CLASS B:
public class Profile extends JFrame {
AllResultsFromDB results = new AllResultsFromDB();
public Profile(AllResultsFromDB x) {
this.results=x;
try {
getData();
getImage();
} catch (Exception e) {
e.printStackTrace();
}
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}
public void getImage() throws Exception {
JLabel label;
Image img;
ImageIcon pic;
JPanel panel;
img = new ImageIcon("java.jpg").getImage();
pic = new ImageIcon(img);
label = new JLabel("", pic, JLabel.CENTER);
panel = new JPanel(new BorderLayout());
panel.setBounds(new Rectangle(0, 0, 340, 310));
panel.add(label, null);
panel.add(label, BorderLayout.CENTER);
this.getContentPane().setLayout(null);
this.setSize(new Dimension(1148, 336));
this.getContentPane().add(panel, null);
}
public void getData() throws Exception {
String url = "jdbc:oracle:thin:#localhost:1521:ORCL";
String username = "c##lambros";
String password = "16111111";
Connection conn = null;
try {
System.out.println("YEAH! IT'S: " + results.getSelectedRow());
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, username, password);
String sql = "SELECT foto FROM criminals WHERE id = 5";
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet resultSet = stmt.executeQuery();
while (resultSet.next()) {
//String name = resultSet.getString(1);
//System.out.println("Name = " + name);
File image = new File("java.jpg");
FileOutputStream fos = new FileOutputStream(image);
byte[] buffer = new byte[256];
//
// Get the binary stream of our BLOB data
//
InputStream is = resultSet.getBinaryStream(1);
while (is.read(buffer) > 0) {
fos.write(buffer);
}
fos.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null && !conn.isClosed()) {
conn.close();
}
}
}
private void jbInit() throws Exception {
this.setSize(new Dimension(816, 380));
JLabel label;
Image img;
ImageIcon pic;
JPanel panel;
img = new ImageIcon("java.jpg").getImage();
pic = new ImageIcon(img);
label = new JLabel("", pic, JLabel.CENTER);
panel = new JPanel(new BorderLayout());
panel.setBounds(new Rectangle(0, 0, 340, 310));
panel.add(label, null);
panel.add(label, BorderLayout.CENTER);
this.getContentPane().setLayout(null);
this.setSize(new Dimension(1148, 336));
this.getContentPane().add(panel, null);
}
}
In the classB since you are creating a new instance
A results = new A();
The value present in the table.getSelectedRow() also gets created newly and will point to null.
So make sure that you do somthing
A results = new A(selectedRow);
and in the constructor of the A,pass the argument to the function
getSelectedRow(selectedRow);
Please note : Make sure that the value of the "table.selectedRow" is maintained
If table is your instance variable in Class A then it might not be initialized when you are trying to access it in constructor of A.
And calling getSelectedRow from the constructor is not making any sense too.
Try to initialize the table variable in constructor instead of calling that method, it should work after it.
This is because the table object is not being initialized.
Try to initialize the table object in constructor....it is a good practice

Simple JTable with DefaultTableModel

I use DefaultTableModel for my JTable model, But not show my table!
public class RecordTableGUI2 extends JFrame {
JTable table;
RecordTableModel2 model2;
public RecordTableGUI2() {
model2 = new RecordTableModel2();
table = new JTable(model2);
add(new JScrollPane(table), BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 500);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new RecordTableGUI2();
}
});
}
}
Model Class:
public class RecordTableModel2 extends DefaultTableModel {
Connection con;
Statement statement;
ResultSet result;
String dbUrl = "jdbc:mysql://localhost/mydb";
String query = "Select * from mytable";
Vector data = new Vector();
Vector column = new Vector();
public RecordTableModel2() {
try {
con = DriverManager.getConnection(dbUrl, "root", "2323");
statement = con.createStatement();
result = statement.executeQuery(query);
int c = result.getMetaData().getColumnCount();
for (int i = 1; i <= c; i++) {
column.add(result.getMetaData().getColumnName(i));
System.out.println(result.getMetaData().getColumnName(i)); //prints correct
}
while (result.next()) {
Vector eachRow = new Vector(c);
for (int i = 1; i <= c; i++) {
eachRow.add(result.getString(i));
System.out.println(result.getString(i)); //prints correct
}
data.add(eachRow);
}
} catch (SQLException sqle) {
sqle.printStackTrace();
} finally {
try {
if (con != null) {
con.close();
}
if (statement != null) {
statement.close();
}
} catch (SQLException sqlee) {
sqlee.printStackTrace();
}
}
}
}
How to introduce vectors to DefaultTableModel?
Output just show a blank JFrame
Add this at first statement of constructor:
super(data,column);
And declare data and column as static

Using java swing table to update MySQL database values

This program is used to read data from database. In the database,there are three tables pki17, pki18, pkn18. For viewing different tables,JComboBox is used and it works by changing the TableModel of the table. Also by using tableChanged method I made table editable. When a cell value in the table is changed the corresponding value in the database has to change to.
When I use tableChanged and actionPerformed methods together, value in database doesn’t get changed when I’m editing a cell in the swing table. When I remove actionPerformed method, then I can update database by editing table cells.
I need to have both abilities, to choose a table from the database by using JComboBox and update database values by editing values in the swing table.
I think the problem exists because TableModel of the table is changed in both methods. But I don’t know how to solve it.
public class TableCombobox extends JPanel implements ActionListener, TableModelListener {
static JTable table;
static JComboBox box;
static MyTableModel model;
static Connection con = null;
static Statement stmt = null;
public TableCombobox() throws SQLException {
super(new BorderLayout());
table = new JTable(new MyTableModel("pki18"));
table.setPreferredScrollableViewportSize(new Dimension(500, 400));
table.setFillsViewportHeight(true);
table.getModel().addTableModelListener(this);
JScrollPane scrollPane = new JScrollPane(table);
JPanel menuPanel = new JPanel();
menuPanel.setLayout(new BoxLayout(menuPanel, BoxLayout.Y_AXIS));
menuPanel.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1,
Color.black));
String[] dalykas = { "Chose groop", "pki17", "pki18", "pkn18" };
box = new JComboBox(dalykas);
box.setMaximumSize(new Dimension(150, 25));
box.setAlignmentX(Component.LEFT_ALIGNMENT);
box.addActionListener(this);
box.setSelectedIndex(2);
menuPanel.add(box);
JPanel cards = new JPanel(new CardLayout());
cards.add(scrollPane, "view");
add(menuPanel, BorderLayout.LINE_START);
add(cards, BorderLayout.CENTER);
}
public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int col = e.getColumn();
model = (MyTableModel) e.getSource();
String stulpPav = model.getColumnName(col);
Object data = model.getValueAt(row, col);
Object studId = model.getValueAt(row, 0);
System.out.println("tableChanded works");
try {
new ImportData(stulpPav, data, studId);
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
public void actionPerformed(ActionEvent event) {
JComboBox cb = (JComboBox) event.getSource();
String pav = (String) cb.getSelectedItem();
if (pav != "Chose groop") {
try {
model = new MyTableModel(pav);
table.setModel(model);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private static void GUI() throws SQLException {
JFrame frame = new JFrame("E-gradebook");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new TableCombobox());
frame.pack();
frame.setSize(800, 400);
frame.setVisible(true);
}
public static void main(String[] args) throws SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/pki18",
"root", "");
GUI();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} finally {
if (stmt != null)
stmt.close();
}
}
static Connection getConnection() {
return con;
}
}
public class ImportData {
static Connection con = TableCombobox.getConnection();
public ImportData(String a, Object b, Object c)
throws ClassNotFoundException, SQLException {
Statement stmt = null;
try {
String stulpPav = a;
String duom = b.toString();
String studId = c.toString();
System.out.println(duom);
con.setAutoCommit(false);
stmt = con.createStatement();
stmt.addBatch("update pki18 set " + stulpPav + " = " + duom
+ " where studento_id = " + studId + ";");
stmt.executeBatch();
con.commit();
} catch (BatchUpdateException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (stmt != null)
stmt.close();
con.setAutoCommit(true);
System.out.println("Data was imported to database");
}
}
}
public class MyTableModel extends AbstractTableModel{
static int rowCount;
static Object data [][];
static String columnNames [];
public MyTableModel(String grupName) throws SQLException{
String query ="select Studento_id, vardas_pavarde, 1_semestras,"+
" 2_semestras, egzaminas, bendras_balas "+
"from pki18." + grupName;
ResultSet rs ;
Connection con = TableCombobox.getConnection();
Statement stmt = null;
stmt = con.createStatement();
rs = stmt.executeQuery(query);
rs.last();
rowCount = rs.getRow();
data = new Object[rowCount][6];
rs = stmt.executeQuery(query);
for (int iEil = 0; iEil < rowCount; iEil++){
rs.next();
data[iEil][0] = rs.getLong("Studento_id");
data[iEil][1] = rs.getString("Vardas_Pavarde");
data[iEil][2] = rs.getByte("1_semestras");
data[iEil][3] = rs.getByte("2_semestras");
data[iEil][4] = rs.getByte("Egzaminas");
data[iEil][5] = rs.getByte("bendras_balas");
}
String[] columnName = {"Asmens_kodas","Vardas_Pavarde","1_Semestras"
,"2_Semestras","Egzaminas","Bendras_Balas"};
columnNames = columnName;
}
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 col){
return getValueAt(0, col).getClass();
}
public boolean isCellEditable(int row, int col){
return true;
}
public void setValueAt(Object value, int row, int col){
data[row][col] = value;
fireTableCellUpdated(row, col);
}
}
In the constructor, you add the table model listener to the current model only:
table.getModel().addTableModelListener(this);
In the action event, however, you replace the table model:
model = new MyTableModel(pav);
table.setModel(model);
As a consequence, the new table model won't have the listener, and you won't receive notifications any more. Have the actionPerformed method add the listener as well, and your problem should be fixed.

Categories