I have a JFrame which holds a JXTable (from SwingX dependency) and a JButton.
Once I click the JButton the table should get updated each time. In my case it gets updated only at the first time. Other events also get triggered on the button click (which happens each time I click the button). Only the table is not refreshed with the new rows added.
I am using DeafultTableModel and have tried (explicit trigger) all suggested methods like repaint, fireTableDataChanged etc.
Can someone please help?
EDIT-1 (code snippet added): -
// the actions will take place when VALIDATE button is clicked
validateButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent ae) {
if (evCheckbox1.isSelected() || !list.isSelectionEmpty()) {
try {
// store the validation errors for future use
List<List<String>> validationErrors = validateSheet(Driver.this.fileLocation, list
.getSelectedValuesList(), regulatorTypeCB.getSelectedItem().toString(), sheetTypeCB
.getSelectedItem().toString());
// creates the validation error overview to be added to roTable
Map<String, Integer> tmpMap = getValidationErrorsOverview(validationErrors);
System.out.println(tmpMap);
// create the report overview table
String[] columnNames = {"SHEET_NAME", "VALIDATION_NAME", "#"};
DefaultTableModel tmodel = new DefaultTableModel(0, 0);
tmodel.setColumnIdentifiers(columnNames);
JXTable roTable = new JXTable();
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
roTable.addHighlighter(HighlighterFactory.createSimpleStriping());
List<String> tlist = new ArrayList<String>();
JScrollPane scrPane = new JScrollPane(roTable);
scrPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
overviewPanel.add(scrPane);
// create a list from the validation error overview map to insert as a row in table
for (Map.Entry<String, Integer> entry : tmpMap.entrySet()) {
tlist.add(entry.getKey().split(":")[0]);
tlist.add(entry.getKey().split(":")[1]);
tlist.add(String.valueOf(entry.getValue()));
}
// add rows in table
for (int i = 0; i < tmpMap.size(); i++) {
tmodel.addRow(new Object[] {tlist.get((i * 3) + 0), tlist.get((i * 3) + 1),
tlist.get((i * 3) + 2)});
}
FileUtils.writeStringToFile(logFile, "\n" + new Date().toString() + "\n", true);
roTable.setModel(tmodel);
roTable.repaint();
// frame refresh
Driver.this.frame.revalidate();
Driver.this.frame.repaint();
// open the log file in notepad.exe
ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "verifier.log");
pb.start();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
There are some conceptual mistakes in the lines below:
String[] columnNames = {"SHEET_NAME", "VALIDATION_NAME", "#"};
DefaultTableModel tmodel = new DefaultTableModel(0, 0);
tmodel.setColumnIdentifiers(columnNames);
JXTable roTable = new JXTable();
...
JScrollPane scrPane = new JScrollPane(roTable);
...
overviewPanel.add(scrPane);
1) Don't create a new JXTable when you press the button but work with the table model instead either by clearing the current table model and adding rows to it or directly by setting a new one. For example:
String[] columnNames = {"SHEET_NAME", "VALIDATION_NAME", "#"};
DefaultTableModel tmodel = new DefaultTableModel(0, 0);
tmodel.setColumnIdentifiers(columnNames);
yourTable.setModel(tmodel);
2) These lines suggests that overviewPanel has already been displayed by the time you are trying to add the new table by clicking the button, thus invalidating the components hierarchy and in consequence you have to revalidate and repaint the panel like this:
overviewPanel.add(scrPane);
overviewPanel.revalidate();
overviewPanel.repaint();
However while we can add components dynamically in Swing we tipically place all our components before the top-level container (window) is made visible. Thus the approach described in point 1 is highly preferable over this one and I'm adding this point just for completeness.
3) Be aware that time consuming tasks such as database calls or IO operations may block the Event Dispatch Thread (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 heavy tasks in a background thread and update Swing components in the EDT. See more in Concurrency in Swing lesson.
Update
Please consider the following example illustrating point 1:
The table is created and placed once before making the top-level container (window) visible.
Both actions work with the table model: one of them sets a new table model and the other one clear and re-fill the current table model.
Here is the code. Hope it helps!
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.swingx.JXTable;
public class Demo {
private void createAndShowGUI() {
final JXTable table = new JXTable(5, 6);
table.setPreferredScrollableViewportSize(new Dimension(500, 200));
Action resetModelAction = new AbstractAction("Set a new model") {
#Override
public void actionPerformed(ActionEvent e) {
Random random = new Random(System.currentTimeMillis());
DefaultTableModel model = new DefaultTableModel(0, 6);
for (int i = 0; i < model.getColumnCount(); i++) {
model.addRow(new Object[]{
random.nextInt(),
random.nextInt(),
random.nextInt(),
random.nextInt(),
random.nextInt(),
random.nextInt()
});
}
table.setModel(model);
}
};
Action clearAndFillModelAction = new AbstractAction("Clear and fill model") {
#Override
public void actionPerformed(ActionEvent e) {
Random random = new Random(System.currentTimeMillis());
DefaultTableModel model = (DefaultTableModel)table.getModel();
model.setRowCount(0); // clear the model
for (int i = 0; i < model.getColumnCount(); i++) {
model.addRow(new Object[]{
random.nextInt(),
random.nextInt(),
random.nextInt(),
random.nextInt(),
random.nextInt(),
random.nextInt()
});
}
}
};
JPanel buttonsPanel = new JPanel();
buttonsPanel.add(new JButton(resetModelAction));
buttonsPanel.add(new JButton(clearAndFillModelAction));
JPanel content = new JPanel(new BorderLayout(8,8));
content.setBorder(BorderFactory.createEmptyBorder(8,8,8,8));
content.add(new JScrollPane(table));
content.add(buttonsPanel, BorderLayout.PAGE_END);
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(content);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Demo().createAndShowGUI();
}
});
}
}
Related
2015.5.5 22:11 updated. I found that, when I create a sorter and call setRowSorter() method in the MyTable's construction, afterwards, it
will keep the original line number(in which the data still refresh
correctly but not easy to discover)even though the dataModel inside is
already changed many times which can be proven as
printf(getModel().getRowCount()). What's more,
MyTable.getAutoCreateRowSorter() always return true. I must explicitly
call setAutoCreateRowSorter(true) to fix the issue it if I called
setRowSorter(). I am happy but this is still wierd.
[2015.5.5 6:19 updated] I have found a way to access: make a "setRow" with the combination of insertRow() and removeRow() and everytime I
update, I setrRow all the rows in the table. It reflect immediately in
the UI. BUT there will be a series of error begin with "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Invalid index" and I guess it's about some kiind of swing thread problem because I can see "RepaintManager" or "paint" in the error. It occurs especially when I move the scrollbar when it's running.(but it still occur if I don't move it)
I have a JTable in a JScrollPane in a JFrame. I initial a MyTableModel with a data and use it to Create a JTable.Like this
MyTableModel extends DefaultTableModel{
Data data;
//object
MyTableModel (Data a){
data = a;
// do something to initial the table model,like build object[][] and add rows.
}
}
class main{
MyTableModel tm = new MyTableModel(data);
Jtable table = new JTable(tm);
JScrollpane jsp = new JScrollpane(table);
JFrame window = new JFrame();
window.getxxxpane().add(jsp);
}
So, as my data is always changing/updating and the changed row is plural and impossible to caculate.
while(true){
data.change();
refreshing the table to display the data immediately;
}
my idea is to simply build a new MyTableModel object and set it as the table's model like:
table.setModel(new MyTableModel(data)); //data had been changed.
which doesn't work.
and I tried this:
tm = MyTableModel(data);
tm.fireTableDataChanged();
which doesn't work either.
and the combination as well:
MyTableModol nm = new MyTableModel(data); //data had been changed
table.setModel(nm);
nm.fireTableDataChanged();
Could someone please give me some clue to change the TableModel object in an unchangable Jtable and update everytime.I dont want to change the tableModel Object because the calculation is huge, instead ,i Want to always create a new object with the construction method's parameter(changed data).
the most worst method is to remove the JScrollpane and rebuild one table/tablemodel/jscrollpane and re-add it, in which I have to call window.setVisible(true). window.repait() doesn't work,either,unless I move it.
I create a space-wasting but runnable program for demostration ,which most of them are nonsense.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Formatter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
class TM extends DefaultTableModel {
int[] inside;
static String[] columnNames = { "Hellium", "Radon",
};
TM(int[] data) {
super(columnNames, 0);
this.inside = data;
update();
}
void update() {
Object[][] data = new Object[2][columnNames.length];
for (int i = 0; i < 2; ++i) {
data[i][0] = inside[0];
data[i][1] = inside[1];
// setValueAt(aValue, row, column);
addRow(data[i]);
}
fireTableDataChanged();
}
}
class idkName {
TM tm;
JButton jb, jb2;
int data[] = { 1, 2 };
int data2[] = { 9, 10 };
JTable table;
JScrollPane jsp;
JFrame twindow;
idkName() {
JFrame window = new JFrame();
window.setSize(400, 400);
window.setLayout(new BorderLayout());
jb = new JButton("show");
jb2 = new JButton("change model");
window.add(jb, BorderLayout.EAST);
window.add(jb2, BorderLayout.WEST);
twindow = new JFrame();
jb.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
tm = new TM(data);
table = new JTable(tm);
jsp = new JScrollPane(table);
twindow.getContentPane().add(jsp);
twindow.setSize(500, 500);
twindow.setVisible(true);
}
});
window.setVisible(true);
jb2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// tm = new TM(data2);
tm = new TM(data2);
System.out.println(""+tm.getValueAt(0,0));
tm.fireTableDataChanged();
twindow.setVisible(true);
}
});
}
}
public class main2 {
TM tm;
public static void main(String[] args) {
idkName i = new idkName();
}
}
If you're going to extend DefaultTableModel, then when the data is changed, you need to update it in DefaultTableModel's internal representation using the setValueAt or setDataVector methods. If you have extensive changes (which it sounds like you do), use the setDataVector method to change all the cells and the table structure at once. Your example code looks like it's missing some updates because it's not pushing the new values in to the DefaultTableModel's data vectors.
But since you have a lot of updates, you're probably better off avoiding DefaultTableModel and just extending AbstractTableModel, using your own custom data storage, and calling the appropriate fireXxx() methods whenever your data changes. This will probably be more efficient in terms of both data conversion and number of events raised.
And then make sure all the event and value change work is done on the AWT event thread, using SwingWorkers or other threading support.
I open JDialog from main window to show set of data, retrieved according to parameters and mode.
(in this simplified version parameters doesn't matter, just "Mode", in real I fill it from recordset).I want
1.to fill table in Dialog Window initially
optionally use button to show another set of data, retrieved with different mode
First mission is OK. Creating table and filling is organised as procedure
that works properly from dialog constructor.
But when I run this procedure from ActionListener of button -- I get another copy of table in the same window - it makes sense because I create it once again as "new". I cant find the way I can refill data on the same table! All examples on the net are about creating a new one
Here is calling part
btnFreqHistory.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Running history");
DlgFreqHistory FreqHist= new DlgFreqHistory(1000,2,1 );
FreqHist.setDefaultCloseOperation(FreqHist.DISPOSE_ON_CLOSE);
FreqHist.setVisible(true);
}
});
Here is JDialog part:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import java.sql.ResultSet;
public class DlgFreqHistory extends JDialog {
private final JPanel contentPanel = new JPanel();
static int Freq;
static int Side;
static int Mode; //1- last 10 results, 2 - best results
/**
* Launch the application.
*/
public static void main(String[] args) {
}
/**
* Create the dialog.
*/
public DlgFreqHistory(int iFreq, int iSide, int iMode) {
Freq = iFreq;
Side = iSide;
Mode = iMode;
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setLayout(new FlowLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
this.setTitle("Last results for " + iFreq );
**// !! This call works fine**
GetHistoryByMode(iFreq, iSide, 2);
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
JButton btn_getData = new JButton("Get data");
btn_getData.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println("in action performed");
***// !! This call creates another table***
GetHistoryByMode(Freq, Side, 1);
}
} );
buttonPane.add(btn_getData);
getRootPane().setDefaultButton(btn_getData);
}
private void GetHistoryByMode(int Freq, int iSide, int Mode)
{
System.out.println("in GetHistoryByMode " + Freq + " " + iSide + " " + Mode);
String HistColumns[] = new String[2];
String HistData[][] = new String[10][2];
if (Mode==1) {
HistColumns[0] = "X1";
HistColumns[1] = "Result";
HistData[0][0] = "xx1";
HistData[0][1] = "15";
HistData[1][0] = "xx2";
HistData[1][1] = "20";
}
if (Mode==2) {
HistColumns[0] = "Y1";
HistColumns[1] = "Result";
HistData[0][0] = "yy1";
HistData[0][1] = "15";
HistData[1][0] = "yy2";
HistData[1][1] = "23";
}
JTable FreqHistory = new JTable(HistData, HistColumns);
contentPanel.add(FreqHistory);
JScrollPane scrollPane;
scrollPane = new JScrollPane( FreqHistory );
contentPanel.add( scrollPane, BorderLayout.CENTER );
}
}
Don't create the JTable in the GetHistoryByMode method, instead, create it within the constructor and add it to the UI there. Then in the GetHistoryByMode method, create a new TableModel and apply it (setModel) to the existing JTable
Take a look at How to Use Tables for more details
You might also like to have a read through Code Conventions for the Java TM Programming Language, it will make it easier for people to read your code and for you to read others
I have noticed that when I have a JTable with a TableRowSorter contained by a JScrollPane, the vertical scrollbar does not appear until after I have created SortKeys for the sorter (which is done by calling toggleSortOrder() for one of the columns).
My question is really why? What do SortKeys have to do with a vertical scrollbar?
Update: Added SSCCE that opens a JFrame with a JTable inside a JScrollPane, that sits in a Container along with a "Populate" button. When the table is initially painted, there is no data and hence no need for a scroll bar. After I populate it with 20 rows, there is a need for a scroll bar, but none appears.
There are two ways to make the scroll bar appear:
Click on either of the header cells to cause a sort to occur.
Remove the commented call toggleSortOrder() in the Container's refresh() method.
// table.getRowSorter().toggleSortOrder(0);
toggleSortOrder() calls setSortKeys() calls sort() calls fireRowSorterChanged() and eventually something catches the event and adds the scroll bar.
package test;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableRowSorter;
#SuppressWarnings("serial")
public class TestFrame extends JFrame
{
class MyTableModel extends AbstractTableModel
{
public List<String> names = new ArrayList<String>();
public int getRowCount ()
{
return names.size();
}
public int getColumnCount ()
{
return 2;
}
public String getColumnName (int columnIndex)
{
return columnIndex > 0 ? "Name" : "Number";
}
public Class<?> getColumnClass (int columnIndex)
{
return String.class;
}
public Object getValueAt (int row, int col)
{
return row < names.size() ? col == 0 ? Integer.toString(row) : names.get(row) : "";
}
public void refresh (List<String> names)
{
this.names = names;
}
}
class MyContainer extends java.awt.Container implements ActionListener
{
JTable table;
MyTableModel model = new MyTableModel();
private TableRowSorter<MyTableModel> sorter;
public MyContainer()
{
}
public void init ()
{
sorter = new TableRowSorter<MyTableModel>(model);
table = new JTable(model);
table.setBorder(BorderFactory.createEmptyBorder());
table.setRowHeight(35);
table.getTableHeader().setPreferredSize(new Dimension(200, 35));
table.setRowSorter(sorter);
table.setPreferredScrollableViewportSize(new Dimension(200, 70));
table.setFillsViewportHeight(true);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(10, 10, 200, 210);
//Add the scroll pane to this panel.
add(scrollPane);
JButton btn = new JButton("Populate");
btn.setActionCommand("populate");
btn.addActionListener(this);
btn.setBounds(10, 220, 200, 35);
add(btn);
}
public void refresh (List<String> rows)
{
model.refresh(rows);
try
{
// Notify sorter that model data (possibly number of rows) has changed.
// Without this call, the sorter assumes the number of rows is the same.
table.getRowSorter().allRowsChanged();
// Do we really want to toggle the sort order every time we refresh?
// User can toggle the sort order himself by clicking on the
// appropriate header cell.
List<?> keys = table.getRowSorter().getSortKeys();
if (null == keys || keys.isEmpty())
{
// table.getRowSorter().toggleSortOrder(0);
}
} catch (Exception e)
{
e.printStackTrace();
}
table.repaint();
}
public void actionPerformed(ActionEvent e)
{
if ("populate".equals(e.getActionCommand()))
{
List<String> rows = new ArrayList<String>();
for (int ii = 0; ii < 20; ii++)
{
rows.add(String.format("%02d", new Integer(ii)));
}
refresh(rows);
}
}
MyTableModel getModel ()
{
return model;
}
}
public static void main (String args[])
{
new TestFrame();
}
MyContainer myContainer = new MyContainer();
TestFrame()
{
myContainer.init();
myContainer.table.getSelectionModel().clearSelection();
add(myContainer);
this.setSize(240, 310);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//pack();
setVisible(true);
}
}
Well, that is not really a SSCCE because you are using a custom TableModel. If you would have created a proper SSCCE you would be using the DefaultTableModel so that you are testing your code with standard JDK classes. If you did this then you would have noticed that the code would work.
So then your next step would be to try the code with your custom TableModel and you would notice that the code did not work.
So then your question on the forum would be why doesn't the code work with my custom TableModel? The point of the SSCCE is to do basic debugging to isolate where the error is happening so we have information to work with. In your original question we had no idea you where using custom classes.
Anyway, the problem is that your custom TableModel is not notifying the table when a change to the data is made. In your refresh(...) method you need to add the following after you reset the List containing the data:
fireTableRowsInserted(0, names.size()-1);
There is no need for table.repaint() in any of your code.
Why won't table cell 0,1 change from aaa to XXXX?
import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
class MainFrame {
public static void main(String[] args) {
JFrame f = new JFrame("Refreshing JTable");
JPanel p = new JPanel(new GridBagLayout());
DefaultTableModel productsModel;
JTable productsTable;
f.setSize(800, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] tableTitle = new String[] {"ID", "Name"};
String[][] tableData = {{"1", "AAA"},{"2", "BBB"}};
productsModel = new DefaultTableModel(tableData, tableTitle);
productsTable = new JTable(productsModel) {
public boolean isCellEditable(int r, int c) {
return false;
}
};
JScrollPane scrollpane = new JScrollPane(productsTable);
tableData[0][1] = "XXXX";
f.add(p);
p.add(scrollpane);
f.validate();
f.setVisible(true);
}
}
REASON:
Apparently trying to update the array where data is stored will result in JTable not changing. Either DefaultTableModel needs to be updated or the whole table needs to be redrawn.
EDIT (possible Solution) One way is using Timer:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
class MainFrame {
static JFrame f = new JFrame("Refreshing JTable");
static JPanel p = new JPanel(new GridBagLayout());
static DefaultTableModel productsModel;
static JTable productsTable;
public static void main(String[] args) {
runGui();
}
#SuppressWarnings("serial")
public static void runGui() {
f.setSize(800, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] tableTitle = new String[] {"ID", "Name"};
String[][] tableData = {{"1", "AAA"},{"2", "BBB"}};
productsModel = new DefaultTableModel(tableData, tableTitle);
productsTable = new JTable(productsModel) {
public boolean isCellEditable(int r, int c) {
return false;
}
};
JScrollPane scrollpane = new JScrollPane(productsTable);
tableData[0][1] = "XXXX";
Timer t = new Timer(2000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
addColumns();
remakeData();
productsTable.setModel(productsModel);
}
});
t.start();
f.add(p);
p.add(scrollpane);
f.validate();
f.setVisible(true);
}
private static void addColumns() {
productsModel.setColumnCount(0);
productsModel.addColumn("ID");
productsModel.addColumn("Name");
}
private static void remakeData() {
productsModel.setRowCount(0);
productsModel.insertRow(productsModel.getRowCount(), new Object[] {"1", "Dummy item 1"});
productsModel.insertRow(productsModel.getRowCount(), new Object[] {"2", "Dummy itme 2"});
productsModel.insertRow(productsModel.getRowCount(), new Object[] {"3", "Dummy item 3"});
productsModel.insertRow(productsModel.getRowCount(), new Object[] {"4", "Dummy item 4"});
productsModel.insertRow(productsModel.getRowCount(), new Object[] {"5", "Dummy item 5"});
}
}
EDIT(much better solution, the way it worked for me flawlessly) Using a static method. Since I'm adding new data in array through another Frame, I created a static method in MainFrame, which I call every time I add/update/delete Object in array. This method will redo the whole model after update and will therefore refresh table.
One problem with the SSCCE posted on your related thread is that the code changes the array which originally formed the table model, whereas it should be changing the table model itself.
The solution to your problem (and the previous one) is rather simple, but unfortunately a bit too long to post it in a comment
You make sure you do some reading on how a JTable actually works. A good starting point is the Swing tutorial and perhaps even the class javadoc of the most important classes JTable and TableModel. Another good read about Swing and the use of MVC is this document. It is much broader then only JTables but will provide you with more insight in how all Swing components are designed, and how you should use them.
After reading that tutorial, you should understand that if you want to update the data which is shown in the table, you update the model of your table. The model will fire events which are received by the table, and the table will make sure it updates itself. After all, the table is only a view on the data (=the TableModel) so updates on the take should take place on the model side. If this is not clear, go back to step 1
Now take a look at your code and remove the listener. Instead, update the model on the table and see how the table updates itself.
If this does not work, you come back to this site with an SSCCE which you post. That way we can see that you tried something, what you tried and most likely what is wrong in your code. This SSCCE should include your code of the TableModel you use and the code that updates your TableModel since that is the most likely location for your problem.
I am creating a table at run time after fetching the result from the database. The flow of my application is like this.
Initializing a empty JScrollPane(); JScrollPane tableScroll = new JScrollPane();
Fetching result from the database;
Updating result to JTable and adding JTable to JScrollPane using following method:
Code:
private void setResultTable(Vector documents, Vector header) {
TableModel model = new DefaultTableModel(documents, header);
documentTable.setModel(model);
tableScroll.add(documentTable);
tableScroll.repaint();
}
my problem is that after calling setResultTable the result are not appearing in the table. Please help me with that. Thanks in advance !!!
You appear to be adding the JTable directly to the JScrollPane. If so this isn't correct and you'll want to change this so that you're actually adding the table to the scroll pane's viewport:
tableScroll.getViewport().add(documentTable);
There is no need to repaint the JScrollPane after doing this.
For example:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class AddTableToScroll {
public static void main(String[] args) {
final JScrollPane sPane = new JScrollPane();
// Sorry Jeanette!!
sPane.setPreferredSize(new Dimension(200, 150));
JButton button = new JButton(new AbstractAction("Press Me!") {
public void actionPerformed(ActionEvent arg0) {
DefaultTableModel model = new DefaultTableModel(new Integer[][] {
{ 1, 2 }, { 3, 4 } }, new String[] { "A", "B" });
JTable table = new JTable(model);
sPane.getViewport().add(table);
}
});
JPanel panel = new JPanel();
panel.add(sPane);
panel.add(button);
JOptionPane.showMessageDialog(null, panel);
}
}
If this doesn't help, you'll want to also tell us exactly what happens, what you see, and if possible supply us with a small compilable runnable program that demonstrates your problem, an SSCCE