Help updating jTable (model?)

I've got a jTable, which stores information that I retrieve from game servers (player's name, frag count, and time connected to the server). I'll admit first off that I'm very, very new to Swing and still new to Java as a whole. I've searched the forums several times (which is the only reason I got as far as I have) and I've also closely examined the tutorial, API, and examples. I probably have already looked at my answer, but it eludes me. My table doesn't seem to be updating its display or I'm not properly modifying the object's contents because my table will only display empty cells (although the correct number of rows). Here's some snippets of what I've got so far:
Object[][] data;
String[] columnNames = {"Name", "Frags", "Time"};
final DefaultTableModel model;
data = new Object[64][3];       
model = new DefaultTableModel(data, columnNames);
model.setRowCount(0);
playersListTable = new javax.swing.JTable();
playersListTable.setModel( model );
public void updatePlayerInfo() {
        int x = 0;
        if (settings.getServerProp("PlayerInfoNum") != null) {
            model.setRowCount(Integer.valueOf(settings.getServerProp("PlayerInfoNum")));
            while (Integer.valueOf(settings.getServerProp("PlayerInfoNum")) > x) {
                System.out.println("Updating player info... " + x);
                data[x][0] = settings.getServerProp("PlayerName" + x);
                data[x][1] = settings.getServerProp("PlayerScore" + x);
                data[x][2] = settings.getServerProp("PlayerTime" + x++);
            model.fireTableDataChanged();
    }I'm about to head off to work, so my apologies if I left out important details. I'll be around later to fill in any gaps I mistakenly forgot. Thanks again for the help!
EDIT: The table gets updated on one second intervals.
Edited by: mr0ldie on Nov 16, 2008 1:31 PM

Thank you very much! :) I was very confused on how tables worked--I thought I still had to manage the data myself and the table and model were simply containers and visible settings for that (probably not explaining how I thought of it well, but I didn't fully understand, so explaining is even harder ;). Anyway, I've got it working, thanks to your help!

Similar Messages

  • Update JTable model col through header name in fast way

    old day, i update my JTable model through the way :
    tableModel.setValueAt(aValue, rowIndex, jTable1.getColumn("HeaderName").getModelIndex());now, i am adding a feature to my table, where the user can remove column.
    when user remove the column, is just the JTable GUI column being removed,. the underlining TableModel column is still there.
    my new feature will broke my above code.
    hence, i change my code to :
    for(columnIndex=0; columnIndex<columnCount; columnIndex++) {
         String name = tableModel.getColumnName(columnIndex)
         if(name.equals("HeaderName"))
              tableModel.setValueAt(aValue, rowIndex, columnIndex)
    }instead of looping through, is there any way i can retrieve the column model index in a fast way?
    a way i can think off, is inherit from DefaultTableModel, and add a map member, so that it can directly map the header name to col index.
    is there any better way?
    thanks

    i don't know why but the KeyListener does work fine with another application that i have also created Not a good solution. First of all the column still exists, so the user will tab from one column to your "hidden" column and wonder whats happening.
    The correct solution is to remove the TableColumn from the TableColumnModel. You can still access the data in the TableModel:
    table.getModel().getValueAt(...)
    No need to use the convertColumnIndexToModel.

  • Help updating iPad model A1396

    Im connected to wifi and trying to update to version 6.1.3 and it keeps saying error please help me figure this out

    PLease someone

  • Clear and update JTable.

    Hi. this is a follow up from [Original Tread in "New To Java"|http://forums.sun.com/thread.jspa?messageID=10886612&#65533;]
    Hope you can help me here. For you who dont read the link. This application is build only to lay up here. So the Layout aint pretty. It compiles and run tho. What i want and cant get to work is to reload table when i push my JTabbedPane.
    Main Class
    package test;
    import javax.swing.*;
    import javax.swing.event.*;
    public class Main {
        public Main() {
            run();
        public void run(){
         frame();
        // clear and update JTable in class three
        public void clearThree() {
            Three t = new Three();
             t.clearVector();
        public JFrame frame() {
            JFrame frame = new JFrame();
            JTabbedPane tab = new JTabbedPane();
            tab.addTab("two",new Two());
            tab.addTab("three", new Three());
            tab.addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent e) {
                    clearThree();// this dont work
            frame.add(tab);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(800,400);
             frame.setVisible(true);
             frame.pack();
            return frame;
        public static void main(String[] args) {
             java.awt.EventQueue.invokeLater(new Runnable() {
                         public void run(){
           new Main();
    }// class two
    package test;
    import java.awt.event.*;
    import java.util.Vector;
    import javax.swing.*;
    // class two&#347; only purpose is to  call clear() in class Three
    public class Two extends JPanel{
    public Two(){
    toolBar();
      public void clearThree () {
       Three t = new Three();
       t.clearVector();
      public void toolBar(){
      JToolBar bar = new JToolBar();
      JButton button = new JButton("clear");
      button.addActionListener(new ActionListener (){
          public void actionPerformed(ActionEvent e){
          clearThree(); // this dont work
    bar.add(button);
    add(bar);
    }// class three hold the table.
    package test;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Vector;
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
    public class Three extends JPanel{ 
            DefaultTableModel model;
            JTable table;
            Vector v = new Vector();
            Vector a = new Vector();
            Vector<Vector> r = new Vector<Vector>();
        public Three() {
            jBar();
            jTable();
        public void clearVector(){
            v.removeAllElements();
            a.removeAllElements();
            r.removeAllElements();
            model.fireTableDataChanged();
        public void jBar() {
            JToolBar bar = new JToolBar();
            JButton button =  new JButton("clear");
            button.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    clearVector();// this does work
            bar.add(button);
            add(bar);
        public JScrollPane jTable(){
            v.addElement("ID");
            v.addElement("Name");
            a.addElement("01");
            a.addElement("Magnus");
            r.add(a);
            model = new DefaultTableModel(r,v);
            table = new JTable(model);
             JScrollPane pane = new JScrollPane(table);
             add(pane);
             return pane;
    }

    Thank you for your replay, I have taken all of your tips and modifed my original application. but the problem is still there. This has been a very messy thread. and it is becouse i thougth it were a table/model problem. But it&#347; not. In the code below I have a JTextField. When i push the JTabbedPanes i want the setText(); to kick in. this compile and run.
    // class One
    package test;
    import javax.swing.*;
    import javax.swing.event.*;
    public class Main {
        public Main() {
            run();
        public void run(){
         frame();
        public JFrame frame() {
            JTabbedPane tab = new JTabbedPane();
            final  Three t = new Three();
            JFrame frame = new JFrame();
            JPanel panel = new JPanel();
            tab.addTab("two",new Two());
            tab.addTab("three", new Three());
            tab.addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent e) {
                      t.setText(); // should call setText() in class Three.
            frame.add(panel);
            frame.add(tab);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(800,400);
             frame.setVisible(true);
             frame.pack();
            return frame;
        public static void main(String[] args) {
             java.awt.EventQueue.invokeLater(new Runnable() {
                         public void run(){
           new Main();
    }class Two. Does nothing more then holding an empty tab
    package test;
    import javax.swing.*;
    public class Two extends JPanel{
    public Two(){
    emptyPane();
      public void emptyPane () {
      JLabel  label = new JLabel("Just an empty JTabbedPane");
      add(label);
      }class Three
    package test;
    import javax.swing.*;
    public class Three extends JPanel{ 
             JTextField text;
        public Three() {
            text();
        public void setText() {
            text.setText("Hello"); // this piece of code i want to insert in my JTextField
            validate();                // when i push the JTabbedPanes.
            repaint();
            updateUI();
        public JTextField text(){
             JToolBar bar = new JToolBar();
            text = new JTextField("",20);
            bar.add(text);
            add(bar);
            return text;
    }

  • Update JTable

    Hello,
    I always thought that the table takes care for the view, whereas the tablemodel handles the data. But how come that an update with tblModel.setDataVector(...) destroys (resets to default) the preferred column width? Rather how can this be avoided?
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.table.*;
    public class UpdateTable
    { static String headers[]= {"Baum", "Blatt", "Frucht","H�usigkeit"};;
      static String data[][]= {
         {"Eiche", "gez�hnt","Eichel","ein"},
         {"Buche", "glatt", "Buchecker","ein"},
         {"Tanne", "Nadel", "Zapfen","ein"},
         {"Pappel", "wechselst�ndig","Kapsel","zwei"},
      static DefaultTableModel tblModel;
      public UpdateTable()
      { JFrame frame = new JFrame("UpdateTable");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container contentPane = frame.getContentPane();
        tblModel = new DefaultTableModel(data, headers)
        { // Make read-only
          public boolean isCellEditable(int x, int y)
          { return false;
        final JTable table = new JTable(tblModel);
        table.getColumnModel().getColumn(1).setPreferredWidth(200);
        table.getColumnModel().getColumn(3).setPreferredWidth(20);
          // Set selection to first row
        ListSelectionModel selectionModel = table.getSelectionModel();
        selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        selectionModel.addListSelectionListener (new ListSelectionListener()
        { public void valueChanged(javax.swing.event.ListSelectionEvent e)
          { if (e.getValueIsAdjusting()) return;
                System.out.println(table.getSelectedRow());
          // Add to screen so scrollable
        JScrollPane scrollPane = new JScrollPane (table);
        contentPane.add(scrollPane, BorderLayout.CENTER);
        frame.setSize(500, 100);
        frame.setVisible(true);
      public static void main(String args[])
      { new UpdateTable();
        try
        { Thread.sleep(3000);
        catch (InterruptedException e)
        { System.out.println ("Fehler: "+ e.toString());
        String headers_neu[] = {"Arbre", "Feuille", "Fruit", "Maisonette"};
    //  Even if the next line is taken out the width changes.
        headers = headers_neu;
        tblModel.setDataVector((Object[][])data, (Object[])headers);
    }Bye
    J�rg

    I agree, it doesn't make sense. Why does a setDataVector() method need a data vector and a column vector. Anyway I extended the DefaultTableModel and added a refreshDataVector() method that only needs a data vector.
    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.table.*;
    public class RefreshTableModel extends DefaultTableModel
        public RefreshTableModel()
            super();
        public RefreshTableModel(int numRows, int numColumns)
            super(numRows, numColumns);
        public RefreshTableModel(Object[][] data, Object[] columnNames)
            super(data, columnNames);
        public RefreshTableModel(Object[] columnNames, int numRows)
            super(columnNames, numRows);
        public RefreshTableModel(Vector columnNames, int numRows)
            super(columnNames, numRows);
        public RefreshTableModel(Vector data, Vector columnNames)
            super(data, columnNames);
        public void refreshDataVector(Vector data)
            fireTableRowsDeleted(0, getRowCount() - 1);
            dataVector = data;
            fireTableRowsInserted(0, getRowCount() - 1);
        public void refreshDataVector(Object[][] data)
            refreshDataVector( convertToVector(data) );
        public static void main(String[] args)
            Object[][] data = { {"four", "A"}, {"three", "B"}, {"two", "C"}, {"one", "D"} };
            String[] columnNames = {"Number", "Letter"};
            RefreshTableModel model = new RefreshTableModel(data, columnNames);
            JTable table = new JTable(model);
            table.getColumnModel().getColumn(0).setPreferredWidth(100);
            table.getColumnModel().getColumn(1).setPreferredWidth(200);
            JScrollPane scrollPane = new JScrollPane( table );
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            frame.getContentPane().add( scrollPane );
            frame.pack();
            frame.setVisible(true);
            try { Thread.sleep(3000); }
            catch (InterruptedException e) {}
            Object[][] refresh = { {"five", "E"}, {"six", "F"} };
            model.refreshDataVector( refresh );
    }

  • Update Jtable from access database

    as the title suggests help me guys... till now i have a database and some contents in it.. when the application starts the table is filled with contents of the database.. now whenever i add or remove a row in the databse i want to show it in the table... i have a Vector called dbData with the present contents of the database.. i want to update jtable with contents of dbData.... how to do it??

    the best things i can see to do is
    JTable table = new Jtable(.....); // this was ur table before;
    deleteRow(table); // you deleted the row
    // now you have a vector containing all the data for the rows;
    // but you need a vector containing the columns for the rows too
    //so now you do
    table = new JTable(dbData,columnnames);
    repaint();dbData must be a vector of vectors;
    dbData.elementAt(1) return a vector with all the data for row one
    so dbData.elementAt(1).elementAt(1) will be the data for row 1 column 1
    // dbData will be the data still remaining in the table
    or instead of the above you could use a nested for loop and set each element one at a time
    Object temp;
    int numberofRows = dbData.elementCount();
    int numberofColumns = db.elementAt(0).elementCount();
    for(int row = 0; row < numberofColumns;row++)
    for( int column = 0; column < numberofRows; column++)
      temp = dbData.elementAt(row).elementAt(column);
      table.setValueAt(temp,row,column);
    }

  • How to force update jtable display

    I am trying to add some filtering funtionality to my swing application that uses a JTable. What I do is to filter the JTable's datamodel, and call repaint() on the jtable and JPanel, and do jframe.pack(); Sometimes it works, sometimes it doesn't. I check the jtable model, and it is being updated properly. I guess it is a GUI update problem. Is there a better way to force the whole app's GUI to be updated?
    //update tableModel
    //repaint() jtable;
    //repaint() jpanel;
    jf.pack();

    A couple questions
    1. Did you write your own table model? Not calling fireTableDataChanged() can cause problems in this case.
    2. Do you update the table from a thread? You might need to put the updates on the event thread (SwingUtilities.invokeNoow() or invokeLater()) or manually call fireTableDataChanged() (I'm not sure if this needs to happen on the event thread)

  • HtmlDataTable (seem to be) not updating HtmlSelectOneListBox model values

    I have a table with multiple rows of model objects in a list. One of the controls in the row is a selectone list box with its own list of selectItem objects
    On submit and back, any text controls I have in the table and also in the form are retaining their state and updating the model values
    But the selctone list box is not updating the models?
    If I follow the phase events I see the update model phase passing through. Not sure why the data in the ui is not submitted to the model
    If I locate the ui control and ask its selected value I see the changed value. But this is not communicated to the the model.
    Is there a known issue with a data table and select drop downs, especially for keeping the state.
    The backing bean is session scope where the data for the table is maintained as a list of pojo objects. The drop down values are kept in a list of SelectItem objects
    Thanks in advance for help
    Satya

    In short, my fault.
    I have the list items bound to a list. But the I haven't bound the HtmlSelectOneList box with a value binding. I was under the impression that the selected item from this list is kept with the item as in the case of html. But in this case it is kept with the parent via its value attribute.
    Now the state is kept as the form goes back and forth.

  • I have Iconia Tab A500, running 3.0, need help updating using microsd card, got Acer update download

    I have Iconia Tab A500 running 3.0, need help updating, I downloaded all updaes list in the acer supprt, all versions, 3.0, 3.1, 3.2, and 4.something, I read on the acer support somewhere I can use a microsd card to update my device, because acer no longer provides automatically, when i try it says "poor network connection, move to anthoer location.", Do I need to update all the listed updates, or can i only update, using the 4. 0? to get the last known update. I also downloaded, "Documents, Apllications, Drivers, Patches, and O.S..... How do i intsall all these updates, I think am running the very lowest version available, Please Help me... Thank you in advance.

    the update listed on the acer website, that mentions the a500 update is 'kernal source code (for Android 4.0 Ice Cream Sandwich) it's 96.7MB's released 2012/05/08' is that the right one i need? There is also a list of 3 O.S updates, released in 2014/09/05, 391.5MB's, 394.3MB's, and 391.5MB's large, what are those updates, I clicked the model and followed the directions that the acer website asks, and i ended with a list of update released in 2014/09/05, and a Document update released in 2014/12/17, 112.7KB's large,  list of updates reased in 2014/.... and in the Android 4.0 culumn, there is an update that says O.S update released in 2014/09/05, 434.2MB's large, and a Patch Update, that was released in 2013/03/21, what are these updates? listedin the aver website... http://www.acer.ca/ac/en/CA/content/drivers 

  • I am trying to update Adobe Bridge and Photoshop CS6, because it is not opening my CR2 files taken by my Canon 6D.  I have tried to go to help updates, and the software says that it is "Up to Date".  However, if I view the plug-in, it says that Camera R

    I am trying to update Adobe Bridge and Photoshop CS6, because it is not opening my CR2 files taken by my Canon 6D.  I have tried to go to help > updates, and the software says that it is "Up to Date".  However, if I view the plug-in, it says that Camera Raw is only version 7.1.  I can not find a direct download for Camera Raw 7.3, only the DNG converter, NOT CAMERA RAW!  Please Help!

    Did you fix your issue?  I am having the same one

  • HT4623 Help updated my ipad with IOS 7.0.4. It asks me what language, chooses my wi-fi and then asks.........1 - set up as a new ipad, 2 - restore from iclouds or 3 - restore from itunes but won't restore as not enough memory! What do I do now?

    Help updated my ipad with IOS 7.0.4. It asks me what language, chooses my wi-fi and then asks.........1 - set up as a new ipad, 2 - restore from iclouds or 3 - restore from itunes but won't restore as not enough memory! What do I do now?
    I don't want to lose all my pics, songs etc
    Whats my next move as I CANNOT use the ipas at the moment!!!!!!!!

    Your iPhone's iOS must be same or newer than the one in backups. In your case it is older so you need to update your iPhone and then recover it with backup.

  • I was doing OS5 help update iphone but now no longer connects to PC and iTunes does not see the phone

    I was doing OS5 help update iphone but now no longer connects to PC and iTunesdoes not see the phone

    Hold Sleep Button with Home Button toghether until it restarts Always it takes 8 to 15 seconds, after it restarts leave the sleep button and remember do not leave the home button it automaticlly switches to DFU mode.

  • Help updating ATI graphics driver in bootcamp Windows 8.1 on iMac (Retina 5K, 27-inch) with 4 GHz Intel core i7, 16 GB 1600 MHz DDR3 and AMD Radeon R9 M295x 4096MB

    Help updating ATI graphics driver in bootcamp Windows 8.1 on iMac (Retina 5K, 27-inch) with 4 GHz Intel core i7, 16 GB 1600 MHz DDR3 and AMD Radeon R9 M295x 4096MB

    Of note there is a new AMD Catalyst Omega driver released earlier in the week that brings 5K resolution to AMD graphics cards. Here is the link (support.ad.com/en-us/download) to the AMD site. But none of these work with my iMac (Retina 5K, 27-inch, Late 2014), Windows 8 bootcamp.
    Please advise.
    Thanks in advace.

  • I have bought Canon EOS 70 D and have Photoshop Elements 12. The program has RAW 8.0. I need 8.2. I have tryed help - update and it is searching hour after hour. Nothing happens. The program i  brand new, Installed yesterday

    I have bought Canon EOS 70 D and have Photoshop Elements 12. I need RAw 8.2 and the program has 8.0.
    I have tryed help - update and it is searching for hours. Nothing happens. The program is installed yesterday.

    Are you updating from the Editor, if not go to Expert Mode and click:
    Help >> Updates
    But check first under the Help menu that you are signed-in - your email address used for you Adobe ID should be visible. You should be able to update to v8.5

  • I need help updating the graphic drivers for my laptop.

    Recently purchasd a new game and I am having some issues with the graphics. Every post I have read suggests it is an issue between HP and amd. I keep trying and seem to be going backwards. I would love to actually talk to someone from hp but they haven their number very well. I have tried every combination between hp and amd and I am obviously missing something. I dont have much hair left to pull out.

    Hi there ,  Thank you for visiting the HP Support Forums and Welcome! This is a great site to get answers and ask questions.  I read your post on the HP Support Forums and you had mentioned that you are looking for help Updating the Graphics Drivers on your HP Pavilion dv7t-6c00 CTO Notebook PC.  I have the actual drivers page for your specific HP Pavilion dv7t-6c00 CTO. It has the AMD Graphic Driver on this page. You could also update the drivers a couple other ways.  1. Using the HP Support Assistant2. Going into the Device Manager, Right click on the AMD Graphic and doing an update.  You had also mentioned that you wanted to actually talk to someone from HP. Please use the following link to create yourself a case number, then call and it may help speed up the call process:
    Step 1. Open link: www.hp.com/contacthp/Step 2. Enter Product number or select to auto detect
    Step 3. Scroll down to "Still need help? Complete the form to select your contact options"
    Step 4. Scroll down and click on: HP contact options - click on Get phone number
    Case number and phone number appear.
    They will be happy to assist you immediately. Let me know how everything goes.  Have a great day!

Maybe you are looking for