Solved

JTable: Weird column duplication of data

Posted on 2004-10-13
16
240 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
 

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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 500 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now