Solved

"this" and Broken methods in JTable

Posted on 2001-07-24
4
440 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java Filesystem manipulation of Word Docx file (find replace text) 3 65
eclipse shortcuts 9 45
servlet  URL Rewriting 1 25
difference of if loops 23 40
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

911 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

17 Experts available now in Live!

Get 1:1 Help Now