Java swing jTable not being updated - java

I built a jTable using NetBeans GUI, and I want to update it inside the constructor of the class. I'm planning to add a search option on the frame so the whole update idea is quite critical for me.
My code:
public availableTrumps(TrumpistClient TC){
initComponents();
availableTrumpsTrumpistClient=TC;
String result=null;
String query="SELECT * FROM APP.TRUMPS";
result=this.availableTrumpsTrumpistClient.WritingReading("sql_select", query);
if (result.contains("empty")){
JOptionPane.showMessageDialog(this, "There are now trumps to show.");
}
else if (result.contains("error")){
JOptionPane.showMessageDialog(this, "Error in the connection. Please try again.");
}
else{
int i;
String []data = result.split("\r\n");
String [][] data2 = new String [data.length][];
for (i = 0; i < data.length; i++)
{
data2[i] = data[i].split("&");
}
String[] columnNames = {"From", "To", "Departure Time", "Remaining Places", "Proposer", "ClosingTime", "Cost Per Seat" };
this.jTable1 = new JTable(data2,columnNames);
this.jTable1.setPreferredScrollableViewportSize(new Dimension(500,100));
this.jTable1.setFillsViewportHeight(true);
JScrollPane jps = new JScrollPane(jTable1);
add(jps);
jTable1.revalidate();
}
}
The input two-dimentional array data2 is fine and validated.
I added the last 5 rows of the code to see if they help with something. I don't know if they are mandatory and in any case I do not want to change the graphical properties of the jTable I built with the GUI (just the data in it).
When I run the program, I see that the jTable remains empty.
Why?

I suggest you use a table model, whenever the data changes you change the model. Build the JTable instance only once, not whenever you need to change the data.

As others have said, you don't want to create multiple JTable instances. Create one like this:
DefaultTableModel model = new DefaultTableModel(new Object[0][0],
new String[]{"From", "To", "etc."});
JTable table = new JTable(model);
Then, when you need to add rows, use
model.addRow(dataForThisRow); // Object
If you want to change a cell:
model.setValueAt(newValue, row, col); // Object, int, int
Or, to remove row i:
model.removeRow(i); // int
For more information, see the DefaultTableModel documentation.
If, for some reason, it is imperative that you recreate the table each time, I believe the problem is that you are calling revalidate without calling repaint.

Related

Rows inserted into a DefaultTableModel don't show

In my Java 8 app I use a SwingWorker to get some data from a database and call publish once I know the data for a full row that I then want to add to my table:
DefaultTableModel dtm123 = (DefaultTableModel) myTable.getModel();
#Override
protected void process(List<Integer> chunks) {
if(chunks!=null) {
ListIterator<Integer> li = chunks.listIterator();
while(li.hasNext()) {
dtm123.addRow(myDataList.get(li.next()).getRowArray());
}
}
}
dtm123.getRowCount() is increased every time (I checked) but the table isn't updated to actually show the new rows. I tried to update it manually, which didn't work either:
dtm123.fireTableRowsInserted(0, dtm123.getColumnCount()-1);
Here's the weird thing: If I use the full version, it's filling the table just fine:
((DefaultTableModel) myTable.getModel()).addRow(myDataList.get(li.next()).getRowArray());
Can you not use the shorter version for filling a table? dtm123 isn't used anywhere else, so what's the difference between the two versions?
Edit:
Filling it directly like this (without the SwingWorker) works:
myTable.setModel(new DefaultTableModel(
new Object[][] {},
new Object[] {"blaA","blaB","blaC"}
));
DefaultTableModel dtm123 = (DefaultTableModel) myTable.getModel();
dtm123.addRow(new String[] {"bla1A","bla1B","bla1C"});
dtm123.addRow(new String[] {"bla2A","bla2B","bla2C"});
dtm123.addRow(new String[] {"bla3A","bla3B","bla3C"});
dtm123.addRow(new String[] {"bla4A","bla4B","bla4C"});
dtm123.addRow(new String[] {"bla5A","bla5B","bla5C"});
dtm123.addRow(new String[] {"bla6A","bla6B","bla6C"});
dtm123.addRow(new String[] {"bla7A","bla7B","bla7C"});
Looks like the SwingWoker is the problem but why? process is running on the main thread, which has to be used for UI updates, and there are no exceptions or error messages.
Fixed it - there were two reasons why it didn't work:
Setting the model of a table (myTable.setModel(new DefaultTableModel(...add info here...));) and creating the columns has to be done on the main/UI thread too.
process is called at a seemingly random point in time after calling publish() and there's no way to tell beforehand how many chunks there will be (could be just one or all of them), so you have to make sure that you get the DefaultTableModel after you set up the columns. To guarantee this, either set up the columns before you call publish() the first time (but still outside doInBackground - see 1.) or call publish for a "zeroth" time to set them up (see code below). In hindsight this seems pretty obvious...
If you're using a SwingWorker like me, you can do something along the lines of:
DefaultTableModel dtm123;
#Override
protected void process(List<Integer> chunks) {
if(chunks!=null) {
ListIterator<Integer> li = chunks.listIterator();
int next;
while(li.hasNext()) {
next = li.next();
if(next==-1) {
//Set up columns/column titles here
myTable.setModel(new DefaultTableModel(
new Object[][] {},
columnHeaderArray
));
dtm123 = (DefaultTableModel) myTable.getModel();
} else {
//Add new rows here
dtm123.addRow(myDataList.get(next).getRowArray());
}
}
}
}
Use publish as usual but once you know the columns' titles, call publish(-1) to set them up (of course, before you add the rows).

Updating a JTable after setModel

I have a JTable and I can't manage to make it update its content, I am in IntelliJ IDEA.
I'll share some code first
I have all the objects declared in the main class, which extends a JFrame
private Object[][] transactionsData;
private String[] transactionsColumnNames = new String[] {"uid","data","ora","product","ini","amount","final"};
private DefaultTableModel transactionsModel;
private JTable transactionsTable;
Then in the createUIComponents() method, that I managed to understand, is called at the very beginning, when the UI needs to be created, I initialize the table like this:
transactionsData = Main.db.getTransactions();
transactionsModel = new DefaultTableModel(transactionsData,transactionsColumnNames);
transactionsTable = new JTable(transactionsData,transactionsColumnNames){
#Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component component = super.prepareRenderer(renderer, row, column);
int rendererWidth = component.getPreferredSize().width;
TableColumn tableColumn = getColumnModel().getColumn(column);
tableColumn.setPreferredWidth(Math.max(rendererWidth + getIntercellSpacing().width, tableColumn.getPreferredWidth()));
return component;
}
};
transactionsTable.setRowSelectionAllowed(true);
transactionsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
transactionsTable.setDefaultEditor(Object.class, transactionsEditor);
transactionsTable.getTableHeader().setFont(new Font("Segoe",Font.PLAIN,16));
sorter = new TableRowSorter<>(transactionsTable.getModel());
transactionsTable.setRowSorter(sorter);
transactionsTable.getSelectionModel().addListSelectionListener(e -> {
if ( transactionsTable.getSelectedRows().length > 0 ) {
logUIDField.setText(transactionsTable.getValueAt(transactionsTable.getSelectedRow(), 0).toString());
transactionsTable.clearSelection();
uidFilter();
}
});
Now, each time I click a button i switch the content of the frame and the table is displayed, I need it to update its content before being displayed, because its content is taken from an online database, and of course, its content may have changed, so I do this:
storicoButton.addActionListener(e -> {
if( Main.db.hasTag() )
logUIDField.setText(Main.db.tag.getUid());
else
logUIDField.setText("");
transactionsData = Main.db.getTransactions();
transactionsModel = new DefaultTableModel(transactionsData, transactionsColumnNames);
transactionsTable.setModel(transactionsModel);
uidFilter();
CardLayout cardLayout = (CardLayout) rootPanel.getLayout();
cardLayout.show(rootPanel, "Card4");
});
But I can't manage to make it update its content, I tried everything I could find on the internet, and on this site, but everyone says to just use setModel, but it doesn't work, and I can't find what I'm doing wrong.
I tried to read the sizes of transactionsData, transactionsModel and transactionsTable before and after I click the button, transactionsData and transactionsModel actually update, since their size changes accordingly ( I checked looking at the database while I ran the program ) but transactionsTable doesn't change at all.
I tried repaint(), various methods that looks like "updateSomething", I tried with local variables, global variables, initializing new model, updating the model, initializing the table with the model or with data and columns, nothing. I'm desperate.
I hope someone here can help me.
At the moment here is late, and tomorrow I need to wake up early, after work I'll try starting clean, rewriting everything from scratch, maybe I'll find the solution myself, in the mean time I hope someone here could push me in the right direction at least.
Il post the code of that uidFilter() which I found online, it's the first thing I'm going to remove tomorrow, because I suspect it can change something, but I don't have the time to do that now
private void uidFilter() {
RowFilter<TableModel, Object> rf = null;
//If current expression doesn't parse, don't update.
try {
rf = RowFilter.regexFilter(logUIDField.getText(), 0);
} catch (java.util.regex.PatternSyntaxException e) {
return;
}
sorter.setRowFilter(rf);
}

adding check box in the jTable [duplicate]

This question already has an answer here:
How to add checkboxes to JTABLE swing [closed]
(1 answer)
Closed 8 years ago.
i am using swings for creating the desktop application . i have created the functionality which provided the data from the database and insert that into the Jtable .now i want to use provide the additional facility that include the additional column with the check box and the a button to delete that perticuler column (which is checked ) when the button is clicked .i have used the netbeans and it provide the maximum drag and drop option. i am not able to figure it out that the how and where to insert the instance of the checkbox in the current code for inserting the checkbox for the each and every row .
For providing the checkbox with the each row do have to generate the multiple instance of the check box
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
java.sql.Connection con = new DBConnection().getConnection();
PreparedStatement pst;
String Q;
Q = "select * from userregister ";
try {
pst = con.prepareStatement(Q);
ResultSet rs = null;
rs = pst.executeQuery();
String a, b, c, d;
int x = 0;
// DefaultTableModel dt = new DefaultTableModel(data, columnNames);
JCheckBox c1 = new JCheckBox();
for (int i = 0; rs.next(); i++) {
a = rs.getString(1);
b = rs.getString(2);
c = rs.getString(3);
d = rs.getString(4);
jTable2.setValueAt(a, i, 0);
jTable2.setValueAt(b, i, 1);
jTable2.setValueAt(c, i, 2);
jTable2.setValueAt(d, i, 3);
jTable2.setValueAt(, i,4);
}
//jTable1.setAutoscrolls(rootPaneCheckingEnabled);
// TODO add your handling code here:
} catch (SQLException ex) {
Logger.getLogger(NewJFrame1.class.getName()).log(Level.SEVERE, null, ex);
}
}
this is the methods that insert the data into the table . also i want to know that how do i be able to find out that which check box is checked and how to use the variable to respond the request of the multiple deletes . plz help
You have to take a look to Concepts: Editors and Renderers section of How to Use Tables tutorial.
This JCheckBox you're looking for is the default renderer/editor for Boolean class. Having said this JTable makes use of TableModel.getColumnClass() to decide the proper renderer/editor. If you use DefaultTableModel the implementation of the aforementioned method always return Object.class so you would have to override it to return Boolean.class. For instance let's say the first column will contain booleans:
DefaultTableModel model = new DefaultTableModel() {
#Override
Class<?> getColumnClass(int columnIndex) {
return columnIndex == 0 ? Boolean.class : super.getColumnClass(columnIndex);
}
};
It is all well explained in the linked tutorial.
Addendum
Another approach is shown in this Q&A: Checkbox in only one JTable Cell. This is useful when a given column may contain different types of values (booleans, numbers, strings...) so overriding getColumnClass() is not feasible. Don't think it's your case but it might be helpful.
also i want to know that how do i be able to find out that which check
box is checked and how to use the variable to respond the request of
the multiple deletes
Just iterate over the rows asking for the column value (true/false). If it's "selected" (true) then delete it:
TableModel model = table.getModel();
for(int i = 0; i < model.getRowCount(); i++) {
if((Boolean)model.getValueAt(i, 0)) {
// delete the row
}
}
Off-topic
Database calls are time consuming tasks and may block the Event Dispatch Thread (a.k.a. EDT) causing the GUI become unresponsive. The EDT is a single and special thread where Swing components creation and update take place. To avoid block this thread consider use a SwingWorker to perform database calls in a background thread and update Swing components in the EDT. See more in Concurrency in Swing trail.

Clearing data from one JTable is also deleting the another JTable

I am doing an application in Java using Swing. I have two tables and I have to copy contents from one table to another (Replication.) The problem is if I clear the destination Table rows then my source table rows are also getting deleted.
If I press CopyAll then I will copy all the contents from Table-A to Table-B. If I press clear then I have to clear Table-B. But the problem is Table-A is also getting cleared.
For copying
public void copyAll() {
TableModel tableAModel = tableA.getModel();
tableB.setModel(tableAModel);
repaint();
}
For clearing rows (I am doing for table-B)
public void clearTableB() {
DefaultTableModel clearTableData = (DefaultTableModel) tableB.getModel();
clearTableData.setNumRows(0);
}
I think I am getting problem while copying in copyAll() method. I am getting tableA's Model and then clearing it at clearTable() method.
If the above copyAll() method is wrong please tell me how can I implement copyAll(), removeTableB().
You have copied the TableModel between the two tables. This means the two tables share the same data. If you delete the contents of the TableModel, both tables will loose their data.
You should create two separate TableModel instances, and keep them in sync (for example by using a listener as the TableModel fires events each time the model is updated)
In your copy version, you set the model of the first table to the second table. So the two tables share the same model. You should make a copy of the model :
public void copyAll() {
final TableModel tableAModel = tableA.getModel();
final DefaultTableModel copy = new DefaultTableModel(tableAModel.getRowCount(), 0);
for (int column = 0; column < tableAModel.getColumnCount(); column++) {
copy.addColumn(tableAModel.getColumnName(column));
for (int row = 0; row < tableAModel.getRowCount(); row++)
copy.setValueAt(tableAModel.getValueAt(row, column), row, column);
}
tableB.setModel(copy);
}
Both tables are using the same model. You have to give Table B it's own Model, copy the values manually. Your current copyAll method copies the reference to the Table Model, it doesn't copy the contents.
That is because you shared the TableModel for the two tables. In the copy method, you should create a clone of the Model and use the clone for the second table.
If you are using DefaultTableModel You can get Vector of data from the model using getDataVector() and clone() it.
public void copyAll() {
TableModel tableAModel = tableA.getModel(), tableModelB;
Vector tableModelBDataVector = ((DefaultTableModel)tableAModel).getDataVector();
int tableModelAColumnCount = tableAModel.getColumnCount();
Vector<String> tableModelAColumnVector = new Vector<String>(tableModelAColumnCount);
for (int i = 0; i < tableModelAColumnCount; i++)
tableModelAColumnVector.add(tableAModel.getColumnName(i));
tableModelB = new DefaultTableModel((Vector)tableModelBDataVector.clone(), (Vector)tableModelAColumnVector.clone());
tableB.setModel(tableModelB);
}

Removing Column from TableModel in Java

In Java I'm using the DefaultTableModel to dynamically add a column to a JTable.
//create DefaultTableModel with columns and no rows
DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
JTable table = new JTable(tableModel);
The columnNames variable is a string array with the column names. So after the program is up and running the user has the option to add additional columns. I do so as follows
tableModel.addColumn("New column name");
Which dynamically adds the column to the table as desired. The user can also remove columns added. For this I use the following code:
TableColumn tcol = table.getColumnModel().getColumn(0);
table.getColumnModel().removeColumn(tcol);
which should remove the column at a specified index, I've also tried:
table.removeColumn(sheet.getColumn(assessmentName));
Both of them work (visually), but here's the problem. After deleting an added column, if another column is added and the table refreshes, the previously deleted column is there again. So while it is removing the column visually, neither of the last two code snippets actually removes it from the model. I'm assuming here that since the column was added to the model that is where it needs to be removed from? Is there a specific method that I need to call or some logic that I need to implement to remove the column?
For your table, try calling table.setAutoCreateColumnsFromModel(false);
This post has a good example as to how to delete column and the underlying data.
I'm assuming here that since the column was added to the model that is where it needs to be removed from?
Yes.
Is there a specific method that I need to call or some logic that I need to implement to remove the column?
No, but you can make up your own method:
moveColumn(...); // to move the column to the end
setColumnCount(...); // to remove the last column
As a side note if you want to give the users the ability to hide/show columns check out the Table Column Manager.
Acting at the TableColumn level, as you show, has only a visual impact but no impact on the TableModel whatsoever.
If you want to really remove a column from DefaultTableModel then you'll need to subclass it and then, in your subclass:
public class MyTableModel extends DefaultTableModel {
public void removeColumn(int column) {
columnIdentifiers.remove(column);
for (Object row: dataVector) {
((Vector) row).remove(column);
}
fireTableStructureChanged();
}
}
I haven't checked it, but it should work in your case.
Of course, removeColumn() should be called only from the EDT.
Note that I wouldn't encourage anyone to produce this kind of code; in particular, using, or deriving from, DefaultTableModel is not the best solution to define a TableModel.
The DefaultDataModel doesn't have a really removeColumn() function, so I wrote a function myself, which can actually solve the problem.
private void removeColumn(int index, JTable myTable){
int nRow= myTable.getRowCount();
int nCol= myTable.getColumnCount()-1;
Object[][] cells= new Object[nRow][nCol];
String[] names= new String[nCol];
for(int j=0; j<nCol; j++){
if(j<index){
names[j]= myTable.getColumnName(j);
for(int i=0; i<nRow; i++){
cells[i][j]= myTable.getValueAt(i, j);
}
}else{
names[j]= myTable.getColumnName(j+1);
for(int i=0; i<nRow; i++){
cells[i][j]= myTable.getValueAt(i, j+1);
}
}
}
DefaultTableModel newModel= new DefaultTableModel(cells, names);
myTable.setModel(newModel);
}

Categories