Solved

"this" and Broken methods in JTable

Posted on 2001-07-24
4
438 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
Comment Utility
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
Comment Utility
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
Comment Utility
> 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
Comment Utility
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

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

Join & Write a Comment

Suggested Solutions

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 first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

771 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

8 Experts available now in Live!

Get 1:1 Help Now