Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

JTable: Weird column duplication of data

Posted on 2004-10-13
16
Medium Priority
?
250 Views
Last Modified: 2010-03-31
Hi,

I created a JTable. Here is a code snippet:

----------

        final JTable table = new JTable(aNumRows+1, aNumCols);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));

        // Insert top-left corner column (move col A from 0 to 1 position)
        TableColumn c = new TableColumn();
        c.setHeaderValue("Row");
        table.addColumn(c);
        table.moveColumn(aNumCols,0);
       
        // Write all the Row Numbers
        for (int i=1;i<=aNumRows;i++)
          table.setValueAt(new Integer(i), i, 0);

---------

Anyhow, when I run this it shows '1' in both the Row (Col 0) and 'A' (Col 1) columns and '2' and '2' in those colums on the next row, and '3' and '3', etc.

Any idea why the data is duplicated? I just want it in Col 0.
0
Comment
Question by:ycomp
  • 8
  • 8
16 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12301045
final JTable table = new JTable(10+1, 10);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));

        // Insert top-left corner column (move col A from 0 to 1 position)
        TableColumn c = new TableColumn();
        c.setHeaderValue("Row");
        table.addColumn(c);
        table.moveColumn(10,0);

        // Write all the Row Numbers
        for (int i=0;i<10;i++)
          table.setValueAt(new Integer(i+1), i, 0);
          JPanel panel=new JPanel();
          panel.add(table);
          this.getContentPane().add(panel);
          this.setSize(400,400);
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12301057
 table.setValueAt(new Integer(i), i, 0);

should be

  table.setValueAt(new Integer(i), i-0, 0);
0
 

Author Comment

by:ycomp
ID: 12301302
I don't understand what's going on though.

After I run this code, if i do a setValueAt("Hello", 0, 1) it will display Hello in both 0,0 and 0,1

Why is this? Somehow TableColumn c is linked to Column A (which was initially in c's position)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:ycomp
ID: 12301316
sorry, I mean after I run my original code posted. I'm not sure if it happens with yours because I can't get it to compile because of the "this" . I no nothing about swing so I am not sure how to modify it to get it to work.

Anyhow I'd just like to know why the colums are linked.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12301340
package org.prithvi.test;

import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class JTableTest extends JFrame {
  BorderLayout borderLayout1 = new BorderLayout();

  public JTableTest() {
    try {
      jbInit();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }

  void jbInit() throws Exception {
    this.getContentPane().setLayout(borderLayout1);
    final JTable table = new JTable(10+1, 10);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));

        // Insert top-left corner column (move col A from 0 to 1 position)
        TableColumn c = new TableColumn();
        c.setHeaderValue("Row");
        table.addColumn(c);
        table.moveColumn(10,0);

        // Write all the Row Numbers
        for (int i=0;i<10;i++)
          table.setValueAt(new Integer(i+1), i, 0);
          JPanel panel=new JPanel();
          panel.add(table);
          this.getContentPane().add(panel);
          this.setSize(400,400);

  }

  public static void main(String[] args) {
    JTableTest JTableTest = new JTableTest();
    JTableTest.show();
  }
}
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12301346
try above code and let me know if that is ur requirement
0
 

Author Comment

by:ycomp
ID: 12301392
when I run your code, it does the same thing.

1 1
2 2
3 3
etc..
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12301487
Model should be like this. Isn't it??

import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;
import javax.swing.table.DefaultTableModel;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class JTableTest
    extends JFrame {
  BorderLayout borderLayout1 = new BorderLayout();

  public JTableTest() {
    try {
      jbInit();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  void jbInit() throws Exception {
    this.getContentPane().setLayout(borderLayout1);
    int aNumRows = 10;
    int aNumCols = 2;
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);

    // Create a couple of columns
    model.addColumn("Col1");
    model.addColumn("Col2");
    for (int i = 0; i < aNumRows; i++) {

      model.addRow(new Object[] {"v"+i});

    }
    for (int i = 0; i < aNumRows; i++) {
      table.setValueAt("H"+i,i,0);
    }
   

    JPanel panel = new JPanel();
    panel.add(table);
    this.getContentPane().add(panel);
    this.setSize(400, 400);

  }

  public static void main(String[] args) {
    JTableTest JTableTest = new JTableTest();
    JTableTest.show();
  }
}
0
 

Author Comment

by:ycomp
ID: 12301565
I don't know...

you see I'm trying to take advantage of JTables spreadsheet like column heading by default.

A, B... AA, AB, etc.

but I want to stick a row col just before them

anyhow I just found a workaround.

I created an extra column in the constructor of the table then I moved the last column to the first column. This way the columns are not linked. Works for me.
0
 

Author Comment

by:ycomp
ID: 12301574
which answer do you think I should accept?
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 1500 total points
ID: 12301608
void jbInit() throws Exception {
    this.getContentPane().setLayout(borderLayout1);
    int aNumRows = 10;
    int aNumCols = 2;
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(aNumRows,aNumCols);

    for (int i = 0; i < aNumRows; i++) {
      table.setValueAt("Hello" + i, i, 0);
    }

    JPanel panel = new JPanel();
    panel.add(table);
    this.getContentPane().add(panel);
    this.setSize(400, 400);

  }
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12301628
>>which answer do you think I should accept?

It is your wish my friend.

BTW, May I see ur code??

0
 

Author Comment

by:ycomp
ID: 12301652
       table = new JTable(aNumRows, aNumCols+1);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));

        // Insert top-left corner column (move col A from 0 to 1 position)
        TableColumn c = table.getColumn(table.getColumnName(aNumCols));
        c.setHeaderValue("Row");
        table.moveColumn(aNumCols,0);
       
        // Write all the Row Numbers
        for (int i=0;i<aNumRows;i++)
          table.setValueAt(new Integer(i+1), i, 0);
0
 

Author Comment

by:ycomp
ID: 12301672
don't know if that's the best way to get c but I couldn't find a way to get it directly by index.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12301710
It depends, how we are preffering the model

The following creates a table that uses an efficient underlying storage implementation. Although cell values can be changed, rows and columns cannot be added or deleted.

    // Create with initial data
    Object[][] cellData = {
        {"row1-col1", "row1-col2"},
        {"row2-col1", "row2-col2"}};
    String[] columnNames = {"col1", "col2"};
   
    JTable table = new JTable(cellData, columnNames);

The following creates tables that allow rows and columns to be added or deleted:

    // Create a table with empty cells
    int rows = 10;
    int cols = 5;
    table = new JTable(rows, cols);
   
    // Create a table with initial data
    Vector rowData = new Vector();
    for (int i=0; i<cellData.length; i++) {
        Vector colData = new Vector(Arrays.asList(cellData[i]));
        rowData.add(colData);
    }
    Vector columnNamesV = new Vector(Arrays.asList(columnNames));
   
    table = new JTable(rowData, columnNamesV);
0
 

Author Comment

by:ycomp
ID: 12301751
ah I see now.

Thanks for all the help. Now I understand what is going on.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question