Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 692
  • Last Modified:

Problems with JTable, DefaultTableModel


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
pkl_javaq
Asked:
pkl_javaq
  • 7
  • 6
1 Solution
 
objectsCommented:
You need to pass your column model in the ctor.
The ctor you're using generates default columns.

0
 
pkl_javaqAuthor Commented:
To objects :

Please expand on your comment. As It stands, I have absolutely no idea what your comment means
0
 
objectsCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
pkl_javaqAuthor Commented:
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
 
objectsCommented:
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
 
pkl_javaqAuthor Commented:
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
 
objectsCommented:
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
 
pkl_javaqAuthor Commented:
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
 
pkl_javaqAuthor Commented:
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
 
pkl_javaqAuthor Commented:
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
 
pkl_javaqAuthor Commented:
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
 
objectsCommented:
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
 
objectsCommented:
Good for nothing jrun.
Thanks for the points :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now