Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using a the selected JTable name  RSS feed

 
Josh Lanierd
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a program I am writing that has multiple JTables. The user right clicks on the JTable and a JPopup appears and they make their selection. The selection they will use most will move the value of the right clicked row into another JTable. I have multiple JTables that will use this same call.
I can get it to work for a specific JTable. For example I can move the value from JTable1 onto JTableMain. I don't want to write the same function 5 times just because I have 5 JTables. The question I have is how can find out the name of the JTable that was right clicked on so that I only have 1 generic function.
Here is the function I am working with:
public void jPopupSelectPlayer_actionPerformed(ActionEvent e) {
this.fillTable((String)JTable1.getValueAt(JTable1.rowAtPoint (rightSelectPoint),0));
DefaultTableModel model = (DefaultTableModel)JTable1.getModel();
model.removeRow(JTable1.rowAtPoint(rightSelectPoint));
}
Obviously this will only work for one specific JTable, but how can I convert this to work for any JTable. fillTable is another function that takes a String.
Any help or insight would be greatly appreciated. This seems like something that would have come up for someone before, but a search turned up nothing. Probably because I couldn't figure out what keywords to use.
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

D.
 
Josh Lanierd
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JTable table = (JTable) e.getSource();
I tried that cast before and I just tried it again I couldn't get it to work. I always get a ClassCastException on that line. I put in a breakpoint and it dies on that line. It prints out:
Hit uncaught exception java.lang.ClassCastException
java.lang.ClassCastException
<prints stack>
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what class is e.getSource() ?
do a :
System.out.println( e.getSource().getClass() );
D.
 
Josh Lanierd
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That does shed some light on the problem.
The class is: class javax.swing.JMenuItem
I will have to think about this some more. I might not even need to make the calls to a specific JTable if the JMenuItem knows where it came from. Is my logic on the right track?
At first blush I would think the parent or super of JMenuItem would be the JTable it came from. Therefore, I can just make some calls to super. I might be completely whacked in my logic, but I will look at it some more. Thanks for provoking some more thought on this.
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh I see, I thought we were in the mouseevent of the jtable, but in fact we're in the action event of the menuitem.
You need to use getParent to get a handle to the popupmenu and getInvoker to get the table. Something like this :

hope that helps,
D.
 
Josh Lanierd
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That worked like a charm. I really appreciate the help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!