Solved

Problems with JTable, DefaultTableModel

Posted on 2001-06-09
13
672 Views
Last Modified: 2010-04-16

The code below gives me a JTable with most of the features that I programmed, but with one big flaw: It gives me TWICE the number of columns that I want.
It produces the 6 columns (with specified headers and column widths) that I want, preceded by 6 columns with generic headers and column widths.

Can someone point out what I am doing wrong, and how to fix it?

Many thanks in advance.

Code starts here:

/*
     TigerTable1.java
*/

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

class TigerTable extends JFrame {

ExitableJFrame frame;
JTable table;
//TableColumnModel t1ColumnModel;
     
     public TigerTable() {

     TableOneModel t1m = new TableOneModel( 40, 6 ); // ============================ TableModel
//DefaultTableModel enid = new DefaultTableModel( 40, 6 );


     TableColumn Column1 = new TableColumn( 0, 41 );
          Column1.setHeaderValue( "Column1" );
     TableColumn Column2 = new TableColumn( 1, 200 );
          Column2.setHeaderValue( "Column2" );
     TableColumn Column3 = new TableColumn( 2, 135 );
          Column3.setHeaderValue( "Column3" );
     TableColumn Column4 = new TableColumn( 3, 41 );
          Column4.setHeaderValue( "Column4" );
     TableColumn Column5 = new TableColumn( 4, 135 );
          Column5.setHeaderValue( "Column5" );
     TableColumn Column6 = new TableColumn( 5, 41 );
          Column6.setHeaderValue( "Column6" );


     table = new JTable( t1m ); // ================================================== JTable
          table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
          table.getTableHeader().setReorderingAllowed( false );
          table.setCellSelectionEnabled( true );
          table.setRowSelectionAllowed( false );
          table.setRowHeight( 20 );
          table.setGridColor( Color.red );
          table.setShowHorizontalLines( false );
          //table.setShowVerticalLines( false );

    TableColumnModel t1ColumnModel = table.getColumnModel();// ===================== TableColumnModel
         t1ColumnModel.addColumn( Column1 );
         t1ColumnModel.addColumn( Column2 );
         t1ColumnModel.addColumn( Column3 );
         t1ColumnModel.addColumn( Column4 );
         t1ColumnModel.addColumn( Column5 );
         t1ColumnModel.addColumn( Column6 );


    TableCellRenderer renderer = new EvenOddRenderer();
    table.setDefaultRenderer(Object.class, renderer);
   
     frame = new ExitableJFrame( "TigerTable1" );

     JScrollPane scrollPane = new JScrollPane( table );

     frame.getContentPane().add( scrollPane, BorderLayout.CENTER );
     frame.setSize(600, 350);
//     frame.setSize(642, 350); // Use this if I substitute "t1CoverTypeColumn" (90px) for "t1YearColumn" (41px).
     frame.setVisible(true);

     }
}

public class TigerTable1 {
     public static void main(String args[]) {
          TigerTable tigertable = new TigerTable();

     }
}

import javax.swing.JScrollPane;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;

class TableOneModel extends DefaultTableModel {

private boolean DEBUG = true;
     
     public TableOneModel( int numRows, int numColumns  ) {
     
          super( numRows, numColumns );
     
     }    
          public void moveRow( int startIndex,
                               int endIndex,
                               int toIndex ) {
                             
          }
         
          public boolean isCellEditable(int row, int col) {
     
               if (col > 4) {
                    return false;
               } else {
                    return true;
               }
          }

} // .......................................... closes out "TableOneModel extends DefaultTableModel"


0
Comment
Question by:pkl_javaq
  • 7
  • 6
13 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
You need to pass your column model in the ctor.
The ctor you're using generates default columns.

0
 

Author Comment

by:pkl_javaq
Comment Utility
To objects :

Please expand on your comment. As It stands, I have absolutely no idea what your comment means
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Sorry, I'm a bit busy at the moment.
When you create a table w/out specifying a table model it creates a table column model and generates a set of columns automatically for your data. These are the first 6 columns u are seeing.
What u need to do is create your own column model BEFORE you create your table and pass it using the following ctor:

JTable(TableModel dm, TableColumnModel cm)


0
 

Author Comment

by:pkl_javaq
Comment Utility
To: objects

a Problem. Placing the TableColumnModel before the JTable gives me a NullPointerException at runtime:

"Exception Occurred:
java.lang.NullPointerException
     at TigerTable.<init>(TigerTable1.java:34) // ================= the table is instantiated here
     at TigerTable1.main(TigerTable1.java:72) // ================= the main method
"
This even though the table is declared ahead of the constructor. Can you see any caue for this?
/*
     TigerTable1.java
*/

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

class TigerTable extends JFrame {

ExitableJFrame frame;
JTable table;
//TableColumnModel t1ColumnModel;
     
     public TigerTable() {

     TableOneModel t1m = new TableOneModel( 40, 6 ); // ============================ TableModel
//DefaultTableModel enid = new DefaultTableModel( 40, 6 );


     TableColumn Column1 = new TableColumn( 0, 41 );
          Column1.setHeaderValue( "Column1" );
     TableColumn Column2 = new TableColumn( 1, 200 );
          Column2.setHeaderValue( "Column2" );
     TableColumn Column3 = new TableColumn( 2, 135 );
          Column3.setHeaderValue( "Column3" );
     TableColumn Column4 = new TableColumn( 3, 41 );
          Column4.setHeaderValue( "Column4" );
     TableColumn Column5 = new TableColumn( 4, 135 );
          Column5.setHeaderValue( "Column5" );
     TableColumn Column6 = new TableColumn( 5, 41 );
          Column6.setHeaderValue( "Column6" );

    TableColumnModel t1ColumnModel = table.getColumnModel();// ===================== TableColumnModel
         t1ColumnModel.addColumn( Column1 );
         t1ColumnModel.addColumn( Column2 );
         t1ColumnModel.addColumn( Column3 );
         t1ColumnModel.addColumn( Column4 );
         t1ColumnModel.addColumn( Column5 );
         t1ColumnModel.addColumn( Column6 );


     table = new JTable( t1m, t1ColumnModel ); // --------------------------- LINE 34
          table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
          table.getTableHeader().setReorderingAllowed( false );
          table.setCellSelectionEnabled( true );
          table.setRowSelectionAllowed( false );
          table.setRowHeight( 20 );
          table.setGridColor( Color.red );
          table.setShowHorizontalLines( false );
          //table.setShowVerticalLines( false );


    TableCellRenderer renderer = new EvenOddRenderer();
    table.setDefaultRenderer(Object.class, renderer);
   
     frame = new ExitableJFrame( "TigerTable1" );

     JScrollPane scrollPane = new JScrollPane( table );

     frame.getContentPane().add( scrollPane, BorderLayout.CENTER );
     frame.setSize(600, 350);
//     frame.setSize(642, 350); // Use this if I substitute "t1CoverTypeColumn" (90px) for "t1YearColumn" (41px).
     frame.setVisible(true);

     }
}


public class TigerTable1 {
     public static void main(String args[]) { // --------------------------- LINE 72
          TigerTable tigertable = new TigerTable();

     }
}

0
 
LVL 92

Expert Comment

by:objects
Comment Utility
You can't get the model from the table before you create the table (as the table does not exist yet), you need to create your own instance of a TableColumnModel and use it. The DefaultTableColumnModel should meet your needs.

TableColumnModel cm = new DefaultTableColumnModel();
cm.add...
....
table = new JTabel(model, cm);

I'm knocking off for the day, but I'll check how you went in the morning.
0
 

Author Comment

by:pkl_javaq
Comment Utility
Hi objects,

That fixes it! Thanks for the help!
I'll send you your 100 points as soon as I figure out how.

For another 50 points, I would like to ask you a related  question:
Please take a look at the class "TableOneModel " and tell me if anything needs to be fixed, ESPECIALLY regarding the "moveRow" method.
 I have not found any code examples implementing that method so I would like your ideas on coding it properly, for later use via Drag and Drop.


Thanks again
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 92

Accepted Solution

by:
objects earned 150 total points
Comment Utility
Can't see the code for the moveRow method here, but I think I remember seeing it in another question.
If I get time I'll have a look at it for u.

Good to hear u got your columns working.
I'll propose an answer and see if that makes it easire to assign points:)
0
 

Author Comment

by:pkl_javaq
Comment Utility
Hi objects, the code for "moveRow" is in the second class in my original post.
In the meantime, here are your well-deserved points.

Many thanks.
0
 

Author Comment

by:pkl_javaq
Comment Utility
Hi objects, the code for "moveRow" is in the second class in my original post.
In the meantime, here are your well-deserved points.

Many thanks.
0
 

Author Comment

by:pkl_javaq
Comment Utility
Hi objects, the code for "moveRow" is in the second class in my original post.
In the meantime, here are your well-deserved points.

Many thanks.
0
 

Author Comment

by:pkl_javaq
Comment Utility
Hi objects;
I have attempted to Accept your answer with a comment. that has resulted in the following error:

"500 Internal Server Error

/jsp/qManageQuestion.jsp:

null
java.lang.NullPointerException
     at jrun__jsp__qManageQuestion2ejsp18._jspService(jru..."

Do YOU know how to operate this damned thing?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
There's an echo in here, and no points :-(

Your moveRow method is empty, I don't think that'll work :)
I'm a bit tied up at the moment so I don't really have time to code a solution. But what you need to do is remove the row, then insert the row in it's new location. If you can have a go at it, I'm more than happy to point you in the right direction when u get stuck :)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Good for nothing jrun.
Thanks for the points :)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bigHeights  challenge 13 55
Enhanced For loop vs ForEach Loop 4 54
strDist challenge 35 84
wordmultiple challenge 12 89
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…

772 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

11 Experts available now in Live!

Get 1:1 Help Now