aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTable problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable problem" Watch "JTable problem" New topic
Author

JTable problem

Pooka Jeter
Greenhorn

Joined: Feb 23, 2004
Posts: 6
Dear friends
i have a small issue regarding my JTable display. Here is what i am trying to do.
XMLtimer = new Timer(One_Sec, new ActionListener() {
public void actionPerformed(ActionEvent evt) {

System.out.println("Before calling JXMLmessageretrieve");
SwingUtilities.invokeLater(new Runnable() {
public void run(){
System.out.println("Invoking button method");
Xml_message_list_retrieve();
jButton1ActionPerformed(null);
}
});
}
});
XMLtimer.start();
----------------------------------------------------------------------------
public void Xml_message_list_retrieve() {

try{
XmsMessageListQuery q = new XmsMessageListQuery();
q.retrieveContent();
ArrayList l = q.getList();
DefaultTableModel tm = (DefaultTableModel)messageTable.getModel();
tm.setNumRows(0);
tm.setNumRows(l.size());
for (int i = 0; i < l.size(); i++) {
Entry e = (Entry)l.get(i);
messageHash.put(e.id, e);
tm.setValueAt(e.id, i, 1);
tm.setValueAt(e.time, i, 2);
tm.setValueAt(e.subj, i, 3);
}
} catch(NullPointerException e) {
e.printStackTrace();
}
}
------------------------------------------------------------------------
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Xml_message_list_retrieve();
}
-----------------------------------------------------------------------------
ok let me explain.....
i am trying to use Timer to retrived my XML list and output to the Jtable every 10 secs... ( this list should updated itself every 10 secs to the JTable).. however, Timer is working fine except that JTable won't show the newest update of the list, infact it won't show it at all...
But if i Call the Same function (Xml_message_list_retrieve) function by pushing a JButton then it works..
Weird but i don't know why.. so if anyone noticed this problem and have a answer to it plase let me know.. my goal here is to have the timer handle it automatically instead of having myslef pushing the button to trigger the action..
THKs...
Pooka Jeter
Greenhorn

Joined: Feb 23, 2004
Posts: 6
is it my question not making sense at all to everyone or is it too hard to be answered?
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
Hi Pooka,
A bit of both. Your code is not complete so a little difficult to run it. If it's a repaint problem then you can prove that by dragging the edge of your frame (resizing it) which forces a repaint. If your new values then show up you need to call validate or repaint on your table or frame. If not, then your values are not getting to the table.
Ed


The nice thing about Standards is that there are so many to choose from!
Pooka Jeter
Greenhorn

Joined: Feb 23, 2004
Posts: 6
Here is the complete version
/*
* MessageViewerInternalFrame.java
*
* Created on February 9, 2004, 10:04 AM
*/
package com.rolands.whip.msgview;
import com.jclark.xml.sax.CommentDriver;
import com.jclark.xsl.sax.FileDestination;
import com.jclark.xsl.sax.OutputMethodHandlerImpl;
import com.jclark.xsl.sax.XSLProcessor;
import com.jclark.xsl.sax.XSLProcessorImpl;
import com.rolands.framework.ManagedInternalFrame;
import com.rolands.whip.AppInfo;
import com.rolands.whip.net.DataManager;
import com.rolands.xms.XmsMessageListQuery;
import com.rolands.xms.XmsMessageListQuery.Entry;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/**
*
* @author xp
*/
public class MessageViewerInternalFrame extends ManagedInternalFrame {
AppInfo appinfo;
private final static int One_Sec =5000;
private String Language_Mode;

/** Creates new form MessageViewerInternalFrame */
public MessageViewerInternalFrame() {
initComponents();

setClosableFrame(true);
setIconifiableFrame(true);
setMaximizableFrame(true);
setResizableFrame(true);
SetEnglishLanguagMode();
new JInternalFrame("Message Viewer "+GetLanguageMode());

refreshTimer = new Timer(60000, taskPerformer);
refreshTimer.setInitialDelay(0);
refreshTimer.start();

messageTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

pack();
}

HashMap messageHash = new HashMap();

Timer refreshTimer;
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {

//buildHierarchy();
refreshTimer.stop();

}
};

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
jSplitPane1 = new javax.swing.JSplitPane();
jScrollPane1 = new javax.swing.JScrollPane();
messageTable = new javax.swing.JTable();
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
ReadButton = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jMenuBar1 = new javax.swing.JMenuBar();
MENUoption = new javax.swing.JMenu();
MENUITEMlang = new javax.swing.JMenu();
MENUITEMeng = new javax.swing.JMenuItem();
jSeparator3 = new javax.swing.JSeparator();
MENUITEMmtf = new javax.swing.JMenuItem();
MENUhelp = new javax.swing.JMenu();
MENUITEMabout = new javax.swing.JMenuItem();
setClosable(true);
setResizable(true);
setClosableFrame(true);
setDoubleBuffered(true);
setMaximizableFrame(true);
setResizableFrame(true);
jSplitPane1.setDividerLocation(133);
jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
jSplitPane1.setContinuousLayout(true);
jSplitPane1.setDoubleBuffered(true);
jSplitPane1.setAutoscrolls(true);
jScrollPane1.setDoubleBuffered(true);
jScrollPane1.setAutoscrolls(true);
messageTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{new Boolean(false), "", "", ""},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Read", "Message ID", "Date Time Group", "Subject"
}
) {
Class[] types = new Class [] {
java.lang.Boolean.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
};
boolean[] canEdit = new boolean [] {
false, false, false, false
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
messageTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_LAST_COLUMN);
messageTable.setDoubleBuffered(true);
messageTable.setEditingColumn(1);
messageTable.setEditingRow(1);
messageTable.setFocusCycleRoot(true);
messageTable.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
messageTableMouseClicked(evt);
}
});
jScrollPane1.setViewportView(messageTable);
jSplitPane1.setLeftComponent(jScrollPane1);
jPanel1.setLayout(new java.awt.BorderLayout());
jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
ReadButton.setText("Read");
ReadButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ReadButtonActionPerformed(evt);
}
});
jPanel2.add(ReadButton);
jButton1.setText("get list");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jPanel2.add(jButton1);
jPanel1.add(jPanel2, java.awt.BorderLayout.NORTH);
jTextArea1.setEditable(false);
jTextArea1.setDoubleBuffered(true);
jTextArea1.setDragEnabled(true);
jScrollPane2.setViewportView(jTextArea1);
jPanel1.add(jScrollPane2, java.awt.BorderLayout.CENTER);
jSplitPane1.setRightComponent(jPanel1);
getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER);
jMenuBar1.setDoubleBuffered(true);
jMenuBar1.setMargin(new java.awt.Insets(0, 0, 0, 2));
MENUoption.setText("Options Menu");
MENUoption.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MENUoptionActionPerformed(evt);
}
});
MENUITEMlang.setText("Language Mode");
MENUITEMeng.setText("English");
MENUITEMeng.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MENUITEMengActionPerformed(evt);
}
});
MENUITEMlang.add(MENUITEMeng);
MENUITEMlang.add(jSeparator3);
MENUITEMmtf.setText("MTF");
MENUITEMmtf.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MENUITEMmtfActionPerformed(evt);
}
});
MENUITEMlang.add(MENUITEMmtf);
MENUoption.add(MENUITEMlang);
jMenuBar1.add(MENUoption);
MENUhelp.setText("Help");
MENUITEMabout.setText("About");
MENUhelp.add(MENUITEMabout);
jMenuBar1.add(MENUhelp);
setJMenuBar(jMenuBar1);
pack();
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Xml_message_list_retrieve();
}

private void messageTableMouseClicked(java.awt.event.MouseEvent evt) {
// Add your handling code here:
if (evt.getClickCount() > 1) {
System.out.println("Here before calling AppInfo.instance().getDataManager()");
DefaultTableModel controller =(DefaultTableModel)messageTable.getModel();
controller.setValueAt(Boolean.TRUE, messageTable.getSelectedRow(), 0);
ReadButtonActionPerformed(null);
}
}

private void MENUITEMmtfActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
System.out.println(" SetMtfLanguageMode ");
SetMtfLanguageMode();
}

private void MENUITEMengActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println(" SetEnglishLanguagMode ");
SetEnglishLanguagMode();
}

private void MENUoptionActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
System.out.println(" MENUoptionActionPerformed ");
}

private void ReadButtonActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
System.out.println("Trying to read message.");
DefaultTableModel test=(DefaultTableModel)messageTable.getModel();


//test.getValueAt(messageTable.getSelectedRow(), 1);
int row = messageTable.getSelectedRow();
if (row > -1) {
ShowMessageInPanel(messageTable.getSelectedRow());
}
}

public void ShowMessageInPanel(int rowNumber )

{


DataManager dm = AppInfo.getDataManager();
String id = (String)messageTable.getValueAt(rowNumber, 1);
Entry entry = (Entry)messageHash.get(id);
File localfile = dm.localFilename(entry.path);
File stylesheet = dm.localFilename(entry.stylesheet(GetLanguageMode()));
File outputfile = new File(""+localfile+".txt");
jTextArea1.append(localfile.toString());
jTextArea1.append("\n");
jTextArea1.append(stylesheet.toString());

try {


XSLProcessor xsl = new XSLProcessorImpl();
xsl.setParser(new CommentDriver());
xsl.loadStylesheet(new InputSource(new URL("file", "", stylesheet.toString()).toString()));
OutputMethodHandlerImpl outputMethodHandler = new OutputMethodHandlerImpl(xsl);
xsl.setOutputMethodHandler(outputMethodHandler);
ByteArrayOutputStream baos = new ByteArrayOutputStream(32000);
outputMethodHandler.setDestination(new FileDestination(outputfile));

xsl.parse(new URL("file", "", localfile.toString()).toString());
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(new FileReader(outputfile));
String line;
jTextArea1.setText("");
while ((line = br.readLine()) != null) {
jTextArea1.append(line);
jTextArea1.append("\n");
}

} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
public void SetEnglishLanguagMode() {
Language_Mode = "English";
}

public void SetMtfLanguageMode() {
Language_Mode = "MTF";
}

public String GetLanguageMode() {
return Language_Mode;
}


public void Xml_message_list_retrieve() {

try{
XmsMessageListQuery q = new XmsMessageListQuery();
q.retrieveContent();
ArrayList l = q.getList();
DefaultTableModel tm = (DefaultTableModel)messageTable.getModel();
tm.setNumRows(0);
tm.setNumRows(l.size());
for (int i = 0; i < l.size(); i++) {
Entry e = (Entry)l.get(i);
messageHash.put(e.id, e);
tm.setValueAt(e.id, i, 1);
tm.setValueAt(e.time, i, 2);
tm.setValueAt(e.subj, i, 3);
}
} catch(NullPointerException e) {
e.printStackTrace();
}
}
Timer XMLtimer;

public void CreateAndShowGui() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JDesktopPane d = new JDesktopPane();
f.getContentPane().add(d);
f.setSize(475,555);
MessageViewerInternalFrame l = new MessageViewerInternalFrame();

d.add(l);
l.show();
f.show();
XMLtimer = new Timer(One_Sec, new ActionListener() {
public void actionPerformed(ActionEvent evt) {

System.out.println("Before calling JXMLmessageretrieve");
SwingUtilities.invokeLater(new Runnable() {
public void run(){

Xml_message_list_retrieve();
jButton1ActionPerformed(null);
}
});
}
});
XMLtimer.start();

}

public void dispose() {
super.dispose();
// AppInfo.instance().getJtlsWebInterface().removeFromDesktopToolBar(this);
}


// Variables declaration - do not modify
private javax.swing.JMenuItem MENUITEMabout;
private javax.swing.JMenuItem MENUITEMeng;
private javax.swing.JMenu MENUITEMlang;
private javax.swing.JMenuItem MENUITEMmtf;
private javax.swing.JMenu MENUhelp;
private javax.swing.JMenu MENUoption;
private javax.swing.JButton ReadButton;
private javax.swing.JButton jButton1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JSeparator jSeparator3;
private javax.swing.JSplitPane jSplitPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTable messageTable;
// End of variables declaration

}
------------------------------------------------------------------------
/*
* InternalFrameMain.java
*
* Created on February 13, 2004, 10:04 AM
*/
package com.rolands.whip.msgview;
import com.rolands.whip.AppInfo;
import com.rolands.whip.net.DataManager;
import com.rolands.xms.XmsMessageListQuery;
/**
*
* @author xp
*/
public class InternalFrameMain extends MessageViewerInternalFrame{

/** Creates a new instance of InternalFrameMain */
public InternalFrameMain() {
super();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
AppInfo ai = AppInfo.instance();
ai.setConnectionParameters(args[0]);
ai.login(args[1], "NONE");
DataManager dm = ai.getDataManager();
XmsMessageListQuery.configure("xxx", "1025", "/xxx/"+ai.getScenarioName()+"/xms", ai.getUserName());
new MessageViewerInternalFrame().CreateAndShowGui();

}


}

-------------------------------------------------------------------------
Any suggesstions are welcome.. thx
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch pooka!
Please take a look at our
naming policy and adjust your displayed name accordingly.
It would be easier for readers if you ident your code and follow the Java naming conventions.
A javax.swing.Timer is run in the Event-dispatching thread, thus there is no need to call SwingUtilities.invokeLater.
The method setNumRows is obsolete. Have you tried setRowCount? which according to the API adds, or delete rows appropriately.
Calling setValueAt as many times as cells exist in the model causes many tableChanged notifications. They could be avoided if you contruct a new DefaultTableModel when you have all the data, set it as the model for the table and notifies the table with a single JTable.tableChanged(TableModelEvent)
By the way, Is XmsMessageListQuery.retrieveContent taking a long time to execute? If this is the case it should have its own thread of execution.
Is it an asynchronous call?
[ February 24, 2004: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
Pooka Jeter
Greenhorn

Joined: Feb 23, 2004
Posts: 6
ok how about i replace my defaulttable.setValue() with defaulttable.addRow(Vector or array)? and then i call a JTable.fireTableChanged() after.... let me know what you guys think..
again the goal is to have timer control and load the newest data into JTable every 10 secs while the table is capable to display the most current data to the screen....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable problem
 
Similar Threads
JTable Problem with netbeans
JTable ........
JTable problem
jtable
how to do big table pagination reading from database with jtable?