Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

"this" and Broken methods in JTable

Posted on 2001-07-24
4
Medium Priority
?
473 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 200 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 200 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

886 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