Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

"this" and Broken methods in JTable

Posted on 2001-07-24
4
Medium Priority
?
465 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
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…
Suggested Courses

670 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