Solved

Problems with JTable, DefaultTableModel

Posted on 2001-06-09
13
677 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
ID: 6172772
You need to pass your column model in the ctor.
The ctor you're using generates default columns.

0
 

Author Comment

by:pkl_javaq
ID: 6172801
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
ID: 6172813
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:pkl_javaq
ID: 6172863
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
ID: 6172909
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
ID: 6173552
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
 
LVL 92

Accepted Solution

by:
objects earned 150 total points
ID: 6174824
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
ID: 6175658
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
ID: 6175660
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
ID: 6175667
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
ID: 6175680
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
ID: 6175690
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
ID: 6179371
Good for nothing jrun.
Thanks for the points :)
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

Suggested Solutions

Title # Comments Views Activity
How to convert String matching to regex in java 4 48
servlet filter example 37 71
type mismatch (Object[] to double[] 4 33
login form jsp example 2 35
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

808 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