revalidate, repaint to refresh JTable
my jSlider1StateChanged is supposed to update the DefaultTableModel, however the changes are never reflected in the GUI. Do I have the correct approach for this method?
Code :
package net.bounceme.dur.nntp;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
public class MessagesJFrame extends javax.swing.JFrame {
private static final long serialVersionUID = 1L;
private static final Logger LOG = Logger.getLogger(MessagesJFrame.class.getName());
private DefaultTableModel defaultTableModel = new DefaultTableModel();
public MessagesJFrame() throws Exception {
MessageUtils.init();
defaultTableModel = MessageUtils.getDataTableModel();
initComponents();
}
/**
* 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTableMessages = new javax.swing.JTable();
jScrollPane2 = new javax.swing.JScrollPane();
jTextPaneContent = new javax.swing.JTextPane();
jSlider1 = new javax.swing.JSlider();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setAlwaysOnTop(true);
jTableMessages.setModel(defaultTableModel);
jTableMessages.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jTableMessages.setShowHorizontalLines(false);
jTableMessages.setShowVerticalLines(false);
jTableMessages.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jTableMessagesMouseClicked(evt);
}
});
jTableMessages.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
jTableMessagesKeyPressed(evt);
}
});
jScrollPane1.setViewportView(jTableMessages);
jScrollPane2.setViewportView(jTextPaneContent);
jSlider1.setMaximum(MessageUtils.getMax());
jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jSlider1StateChanged(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jSlider1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 905, Short.MAX_VALUE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void jTableMessagesMouseClicked(java.awt.event.MouseEvent evt) {
int row = jTableMessages.rowAtPoint(evt.getPoint());
setText(row);
}
private void jTableMessagesKeyPressed(java.awt.event.KeyEvent evt) {
//int row = jTableMessages.rowAtPoint(evt.getKeyLocation());
//setJFrame(row);
}
private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {
int index = jSlider1.getValue();
MessageUtils.setIndex(index);
defaultTableModel = MessageUtils.getDataTableModel();
revalidate();
repaint();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MessagesJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MessagesJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MessagesJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MessagesJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new MessagesJFrame().setVisible(true);
} catch (Exception ex) {
Logger.getLogger(MessagesJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JSlider jSlider1;
private javax.swing.JTable jTableMessages;
private javax.swing.JTextPane jTextPaneContent;
// End of variables declaration
private void setText(int row) {
MessageBean mb = MessageUtils.getMessageBean(row);
jTextPaneContent.setText(mb.getContent());
jTextPaneContent.setContentType("text/html");
}
}
As can be seen in this console output, the DefaultTableModel is actually getting logged and passed to the JFrame:
Code :
Apr 25, 2012 3:14:37 AM net.bounceme.dur.nntp.MessageUtils setIndex
INFO: MessageUtils.setIndex..trying 100
Apr 25, 2012 3:14:37 AM net.bounceme.dur.nntp.MessageUtils setIndex
INFO: just right, setting..100
Apr 25, 2012 3:14:37 AM net.bounceme.dur.nntp.MessageUtils getDataTableModel
INFO: Thu Apr 19 09:58:24 PDT 2012North Korea’s prison camps: The gulag behind the goose-steps
Apr 25, 2012 3:14:37 AM net.bounceme.dur.nntp.MessageUtils getDataTableModel
INFO: Thu Apr 19 09:58:24 PDT 2012Attacks in Afghanistan: Spectacular
Apr 25, 2012 3:14:37 AM net.bounceme.dur.nntp.MessageUtils getDataTableModel
INFO: Thu Apr 19 09:58:25 PDT 2012Pakistan and alcohol: Hope in the hops
Apr 25, 2012 3:14:37 AM net.bounceme.dur.nntp.MessageUtils getDataTableModel
INFO: Thu Apr 19 09:58:25 PDT 2012Banyan: Mischief Minister
Apr 25, 2012 3:14:37 AM net.bounceme.dur.nntp.MessageUtils getDataTableModel
INFO: Sun Apr 22 02:58:20 PDT 2012GDP forecasts
Apr 25, 2012 3:14:37 AM net.bounceme.dur.nntp.MessageUtils getDataTableModel
INFO: Sun Apr 22 02:58:21 PDT 2012Trade, exchange rates, budget balances and interest rates
Apr 25, 2012 3:14:37 AM net.bounceme.dur.nntp.MessageUtils getDataTableModel
which originates with the log message in getDataTableModel:
Code :
package net.bounceme.dur.nntp;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.swing.table.DefaultTableModel;
public class MessageUtils {
private static final long serialVersionUID = 1L;
private static final Logger LOG = Logger.getLogger(MessageUtils.class.getName());
private static DefaultTableModel defaultTableModel = new DefaultTableModel();
private static EnumNNTP nntp = EnumNNTP.INSTANCE;
private static Vector messages = new Vector();
private static int pageSize = 10;
private static int index;
private static int max;
public static void init() {
setMax(nntp.getSize());
setIndex(getMax() - 10);
loadMessages();
}
private static void loadMessages() {
List<Message> listOfMessages = nntp.getMessages(getIndex() - pageSize, getIndex());
for (Message m : listOfMessages) {
MessageBean messageBean = null;
try {
messageBean = new MessageBean(m);
} catch (Exception ex) {
Logger.getLogger(MessageUtils.class.getName()).log(Level.SEVERE, null, ex);
}
messages.add(messageBean);
}
loadTableModel();
}
private static void loadTableModel() {
defaultTableModel = new DefaultTableModel();
defaultTableModel.addColumn("sent");
defaultTableModel.addColumn("subject");
for (Object o : messages) { //awkward Vector manipulation
MessageBean messageBean = (MessageBean) o;
Vector messageBeanAsVector = messageBean.getVector();
defaultTableModel.addRow(messageBeanAsVector);
}
}
public static DefaultTableModel getDataTableModel() {
Vector vector = defaultTableModel.getDataVector();
Iterator it = vector.iterator();
while (it.hasNext()) {
Vector v = (Vector) it.next();
Iterator i = v.iterator();
StringBuilder row = new StringBuilder();
while (i.hasNext()) {
row.append(i.next());
}
LOG.info(row.toString());
}
return defaultTableModel;
}
public static void setDataTableModel(DefaultTableModel dataTableModel) {
MessageUtils.defaultTableModel = dataTableModel;
}
public static Vector getMessages() {
return messages;
}
public static void setMessages(Vector messages) {
MessageUtils.messages = messages;
}
public static MessageBean getMessageBean(int row) {
MessageBean messageBean = (MessageBean) messages.elementAt(row);
return messageBean;
}
public static int getIndex() {
return index;
}
public static void setIndex(int aIndex) {
LOG.log(Level.INFO, "MessageUtils.setIndex..trying {0}", aIndex);
if (aIndex < pageSize + 1) { //goldilocks check on aIndex
aIndex = pageSize + 1;
LOG.log(Level.INFO, "too small, setting default..{0}", aIndex);
} else if (aIndex >= getMax() - pageSize) {
aIndex = getMax() - pageSize;
LOG.log(Level.INFO, "too big, setting default..{0}", aIndex);
} else {
LOG.log(Level.INFO, "just right, setting..{0}", aIndex);
}
index = aIndex;
loadMessages();
}
public static int getMax() {
return max;
}
public static void setMax(int aMax) {
max = aMax;
}
}
Re: revalidate, repaint to refresh JTable
That's quite a bit of code to go through, so I would suggest boiling this down to an SSCCE that demonstrates the problem that we can just copy and paste to run and see what you're talking about.
So just guessing at your actual problem, I'll remind you that the view isn't automatically updated when the model changes. You have to tell the view to update itself. There are a few methods to do that. I would check out the API for AbstractTableModel for some useful methods: Java Platform SE 6
Re: revalidate, repaint to refresh JTable
Quote:
Originally Posted by
KevinWorkman
That's quite a bit of code to go through, so I would suggest boiling this down to an
SSCCE that demonstrates the problem that we can just copy and paste to run and see what you're talking about.
So just guessing at your actual problem, I'll remind you that the view isn't automatically updated when the model changes. You have to tell the view to update itself. There are a few methods to do that. I would check out the API for AbstractTableModel for some useful methods:
Java Platform SE 6
Could you be more specific about how tell a JTable to "renew" itself? I specifically want to populate the model outside of the JFrame, which happens, but how do I trigger the JTable to refresh? There are many methods in AbstractTableModel.
Re: revalidate, repaint to refresh JTable
Look at the all the methods in that class that begin with fire...for instance, fireTableDataChanged()
Re: revalidate, repaint to refresh JTable
Quote:
Originally Posted by
THUFIR
Could you be more specific about how tell a JTable to "renew" itself? I specifically want to populate the model outside of the JFrame, which happens, but how do I trigger the JTable to refresh? There are many methods in AbstractTableModel.
I'm not sure what you mean by the model outside of the JFrame. But there really aren't that many methods in AbstractTableModel (heck, they all fit on a single page), so reading through them shouldn't be THAT much to ask, especially since you should become more familiar with the model/view/controller relationship anyway.
Re: revalidate, repaint to refresh JTable
Quote:
Originally Posted by
KevinWorkman
I'm not sure what you mean by the model outside of the JFrame.
What I mean is the DefaultTableModel instance in the controller, can MessageUtils
Code :
defaultTableModel.fireTableDataChanged();
cause changes to the JList?
Re: revalidate, repaint to refresh JTable
Quote:
Originally Posted by
THUFIR
What I mean is the DefaultTableModel instance in the controller, can MessageUtils
Code :
defaultTableModel.fireTableDataChanged();
cause changes to the JList?
Huh? What changes? What happened when you tried?