Solved

"this" and Broken methods in JTable

Posted on 2001-07-24
4
445 Views
Last Modified: 2008-03-06

I have a JTable whose methods worked fine until I reorganized the table in order to add a new function
(allowing copying a row from one table to another).

This table (table2) is actually instiantiated by another table (table1):
          table2 = new Table2();
 
The version that worked was organized like this:
     table2 = new JTable( t2Model, t2ColumnModel );
          table2.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );  // --------------- customize the JTable
          table2.getTableHeader().setReorderingAllowed( false );
          ...
          table2.addMouseListener( new T2RowDragger( table2, table2, t2DataVector ) ); // - Drag&Drop addMouseListener

The reorganized version looks like this:
          this.setModel( t2Model );                            // --------------- customize the JTable
          this.setColumnModel( t2ColumnModel );
          this.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
          ...    
//          this.addMouseListener( new T2RowDragger( table1, table2, t2DataVector ) ); // - Drag&Drop MouseListener

     In this case the Drag & Drop MouseListener must be added in Table1, the calling table:
          table2 = new Table2(); // -------------------------------- table2
               table2.addMouseListener( new T2RowDragger( table1, table2, t2DataVector ) ); // - Drag&Drop addMouseListener

Table2 interacts with the following methods and classes:    
     three methods - located in the Table2 class
     a TableCellRenderer - located in the Table2 class
     a Drag & Drop class - in a separate class

Version two now allows inter-table copying. However most of the other methods and classes are either
(1) flat-out broken, giving NullPointerExceptions as below, or

          java.lang.NullPointerException
               at Table2.rowValidator(Compiled Code)
               at Table2$1.actionPerformed(Table2.java:244)
               at javax.swing.KeyboardManager.fireBinding(KeyboardManager.java:279)
               at javax.swing.KeyboardManager.fireKeyboardAction(KeyboardManager.java:209)
               at javax.swing.JComponent.processKeyBindingsForAllComponents(JComponent.java:1652)
               at javax.swing.JFrame.processKeyEvent(JFrame.java:168)
               at java.awt.Component.processEvent(Component.java)

(2) partially broken. For example, in my Drag & Drop class (Macintosh D&D API) most of the
methods (dragGesture, dragEntered, etc) seem to work. The "dragDropped" method does not work.

in that case I switched from attaching Drag & Drop MouseListener in the calling table (Table1):
     table2 = new Table2(); // -------------------------------- table2
          table2.addMouseListener( new T2RowDragger( table1, table2, t2DataVector ) ); // - Drag&Drop addMouseListener

to attaching it in table2 itself, using this approach:
          final T2RowDragger t2RowDragger = new T2RowDragger( table1, table2, t2DataVector );
          this.addMouseListener( t2RowDragger );


Neither approach fixes the "dragDropped" problem.
If I go back to an earlier version of Table2, all these functions work (but inter-table copy doesn't)

Are there straightforward ways to reattach these functions, or any more-general ways of going about fixing this?

Any help greatly appreciated in advance.
0
Comment
Question by:javaqq2001
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
kylar earned 50 total points
ID: 6313998
The problem here (I think) is that the variable table2 and this point to 2 different tables. If I have something like this:

table2 = this;
table2 = new JTable();

then they are no longer equal.  You are trying to perform methods on a "this" which refers to the current instance of the enclosing class, not the table that you want to refer to. I can't be more specific or suggest a solution, b/c the code fragments are too small. If you could post the whole 2 classes that would be much more helpful. It is also unclear where the methods are being called from. If you can't post it (or they are too big or something) you can e-mail me at tbyrne@netscreen.com and I'll have a look.

Cheers,
Kylar
0
 
LVL 92

Assisted Solution

by:objects
objects earned 50 total points
ID: 6315456
Agree with kylar, really need to see more code.
Especially Table2.rowValidator()

On a side note, I'm interested in why you're adding mouse listeners for your dnd. Whenever I've implemented dnd in the past I've not need to add mouse listeners.
0
 
LVL 92

Expert Comment

by:objects
ID: 6315464
> this.setModel( t2Model );
> this.setColumnModel( t2ColumnModel );

If your calling these in your ctor then you can instead do this:

super(t2Model, t2ColumnModel);

And these will get passed down to the relevavnt JTable ctor.
         
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 9017768
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:


[split points between kylar and objects]


Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
sudhakar_koundinya
EE Cleanup Volunteer
---------------------
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post your concern in THIS thread.
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
Weekend adv creator 3 54
oracle 11g 23 107
spring jars download 1 35
jsp insert to database example 2 47
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

828 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