I am making a java project and i am using JTable
.
The thing i want to do is link a popupmenu
to the jtable
. The popupmenu
has four different items
. Some of the items
need to know selected row
in jtable
. The problem is in selectRow
-function. i cannot get a function to return a proper row
. i click on a jtable
with right mouse button
and i get a popupmenu
popped in the same column i clicked as it should be. but when i use addSelectedRow
or removeSelectedRow
-functions (in menuItem with label "addSelected" or "deleteSelected") and any row
(s) is no selected i use selectRow
-function to find a row
i am clicking at with right mousebutton
. Seems that rowAtPoint(Point p)
cant not just find a right row. It returns eather 0 or -1. I have been struggling with this problem almost a week so pleace pimp my code :D i hope code is clean enough :D
<--Edited--> http://painkiller.comlu.com/images/1.jpg
Here is a screenshot of a problem.
JTable
has allways at least one row. I click arbitrary any row (at this situation second row
). Row
is not selected by purpose. Now if i press "Lisää valittu" - which means "Add selected
", function selectRow()
will be called.
/**Locations
jTbl.getLocation(): java.awt.Point[x=0,y=0]
e.getPoints() java.awt.Point[x=4,y=8]
SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), jTbl) java.awt.Point[x=350,y=31]
first i tried e.getPoint()
* e.getPoints() -> java.awt.Point[x=4,y=8]
returned row index number from rowAtPoint()
: 0
then i tried SwingUtilities.convertPoint()
* SwingUtilities.convertPoint(tablePopMenu, e.getPoint(), jTbl);
-> java.awt.Point[x=28,y=10]
returned row index number from rowAtPoint()
: 2
so it seems to work but sometimes function throws -1 or 0. (need to ckeck out that bug) but mostly works pritty good.
<-- -->
private void TableMousePressed(java.awt.event.MouseEvent evt) {
try
{
/*
* The popupmenu content (items) are standart for all tables
* The label of popupmenu items are used to select proper
* command in select case
* 1. Add selected-> Add only selected row(s)
* 2. Add all -> Add all rows
* 3. Delete selected -> Remove only selected row(s)
* 4. Delete all-> Remove all row(s)
*/
javax.swing.JTable temp; //Temporar jTable alien
String label = ((javax.swing.JMenuItem)evt.getSource()).getText(); //Switch Case
temp = (javax.swing.JTable)((JPopupMenu)(evt.getComponent().getParent())).getInvoker();
switch(label) //Select rigt method by jMenuItem.getText() method (the label of menu item) {
case "Add selected":
addSelectedRow(evt, temp);
break;
case "Add all":
addAllRows(temp);
break;
case "Delete selected":
removeSelectedRow(evt, temp);
break;
case "Delete all":
removeAllRows(temp);
break;
}
}
catch(Exception ex){ex.printStackTrace();}
}
/**
* @param e - MouseEvent
* @param jTbl - JTable which will be used
*/
private void addSelectedRow(java.awt.event.MouseEvent e, javax.swing.JTable jTbl){
if(jTbl.getSelectedRow() == -1){ //If no row(s) is selected
selectRow(e, jTbl); //select row
}
int cellCount = jTbl.getModel().getColumnCount(); //amount of columns to apply
int rows[] = jTbl.getSelectedRows(); //amount of rows to apply
cellCollection = new Object[jTbl.getModel().getColumnCount()]; //Object which contains row and cells. will be added to a jTable as a new row
for(int row = 0; row < rows.length; row++){ //Creates new row by excisting data
for(int cell = 0; cell < cellCount; cell++){
cellCollection[cell] = jTbl.getModel().getValueAt(rows[row], cell);
}
((DefaultTableModel)jTbl.getModel()).addRow(cellCollection); //Adds a new row with data
}
}
/**
* @param jTbl - JTable which will be used
*/
private void addAllRows(javax.swing.JTable jTbl){
int rowCount = jTbl.getModel().getRowCount(); //Amount of adding rows
int cellCount = jTbl.getModel().getColumnCount(); //Amount of adding colums
cellCollection = new Object[jTbl.getModel().getColumnCount()]; //New object which contains a row with columns filled with excisting data in a for-loop
for(int row = 0; row < rowCount; row++){
for(int cell = 0; cell < cellCount; cell++){
cellCollection[cell] = jTbl.getModel().getValueAt(row, cell);
}
((DefaultTableModel)jTbl.getModel()).addRow(cellCollection);
}
}
/**
* @param e - MouseEvent
* @param jTbl - JTable which will be used
*/
private void removeSelectedRow(java.awt.event.MouseEvent e, javax.swing.JTable jTbl){
if(jTbl.getSelectedRow() == -1){ //If no row(s) is selected
selectRow(e, jTbl); //selects a row
}
int[] rows = jTbl.getSelectedRows(); //Get selected row(s)
for(int i = rows.length - 1; i >= 0; i--)
((DefaultTableModel)jTbl.getModel()).removeRow(rows[i]); //Removes the rows
getTableToLife(jTbl); //Adds a new empty row if jtable has no rows after removing
}
/**
* @param jTbl - JTable which will be used
*/
private void removeAllRows(javax.swing.JTable jTbl){
int rowCount = jTbl.getModel().getRowCount(); //Amount of removing rows
for(int row = rowCount-1; row >=0; row--){
((DefaultTableModel)jTbl.getModel()).removeRow(row); //Remove rows
}
getTableToLife(jTbl);//Adds a new empty row if jtable has no rows after removing
}
/**
* @param jTbl - JTable which will be saved
*/
private void getTableToLife(javax.swing.JTable jTbl){
if(jTbl.getModel().getRowCount() == 0){ //If jtable has no rows
int cellCount = jTbl.getModel().getColumnCount(); //How many cells will be added
cellCollection = new Object[cellCount];
for(int cell = 0; cell < cellCount; cell++){
cellCollection[cell] = null; //No new data is required }
((DefaultTableModel)jTbl.getModel()).addRow(cellCollection); //Adding a new empty row
}
}
/**
* @param e - Mouse event
*/
private void selectRow(java.awt.event.MouseEvent e, javax.swing.JTable jTbl)
{
// This is the main problem at the moment
// System.out.print("\r " + jTbl.rowAtPoint(e.getPoint()));
// ListSelectionModel selector = jTbl.getSelectionModel();
// selector.removeSelectionInterval(0, jTbl.getHeight()); //I though it he could help, but no effect (row [0] seems to be selected
// int p = jTbl.rowAtPoint(e.getPoint()); //or this function seems to be uncapable to find right row i clicked on jtable
// selector.setSelectionInterval(p, p); //returns 0 or -1. reason is unknown
// New solution
ListSelectionModel selector = jTbl.getSelectionModel();
Point newP = SwingUtilities.convertPoint(tablePopMenu, e.getPoint(), jTbl);
int p = jTbl.rowAtPoint(newP);
System.out.print("\rRow number: " + (p));
if(p <=0)
selector.setSelectionInterval(p, p);
else
selector.setSelectionInterval(p-1, p-1);
}
Thank you people for saving my day!
I am not entirely sure but it is might be that the getPoint() method of the MouseEvent returns a Point in the coordinate space of the popup menu.
What JTable's rowAtPoint(p) expects is a Point within the coordinate space of the table.
You can use SwingUtilities.convertPoint() to translate the coordinates.