Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JTable: Weird column duplication of data

Posted on 2004-10-13
16
Medium Priority
?
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

670 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