Solved

JTable: Weird column duplication of data

Posted on 2004-10-13
16
245 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

752 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