我正在制作一个java项目,我正在使用JTable
。
我想要做的是将popupmenu
与jtable
相关联。 popupmenu
有四种不同的items
。有些items
需要知道row
中选定的jtable
。问题出在selectRow
函数中。我无法获得返回正确row
的函数。我点击jtable
right mouse button
,然后在我点击的同一列中弹出popupmenu
。但当我使用addSelectedRow
或removeSelectedRow
-functions(在menuItem中带有标签“addSelected”或“deleteSelected”)并且未选择任何row
(s)时,我使用selectRow
功能,找到row
我点击right mousebutton
。似乎rowAtPoint(Point p)
不能找到正确的行。它返回eather 0或-1。我一直在努力解决这个问题差不多一个星期,所以我的代码很糟糕:我希望代码足够干净:D
< - 编辑 - > http://painkiller.comlu.com/images/1.jpg
以下是问题的屏幕截图。
JTable
总共至少有一行。我点击任意一行(在这种情况下第二row
)。未按目的选择Row
。现在,如果我按“Lisäävalittu” - 意思是“Add selected
”,将调用函数selectRow()
。
/ * *位置
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]
首先我尝试e.getPoint()
* e.getPoints() -> java.awt.Point[x=4,y=8]
从rowAtPoint()
返回行索引号:0
然后我试了SwingUtilities.convertPoint()
* SwingUtilities.convertPoint(tablePopMenu, e.getPoint(), jTbl);
- > java.awt.Point[x=28,y=10]
从rowAtPoint()
返回行索引号:2
所以它似乎工作但有时函数抛出-1或0.(需要克服那个bug),但大多数工作很好。
< - - >
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);
}
感谢大家拯救我的一天!
答案 0 :(得分:2)
我不完全确定,但可能是MouseEvent的getPoint()方法在弹出菜单的坐标空间中返回一个Point。
JTable的rowAtPoint(p)期望的是表格坐标空间内的一个点。
您可以使用SwingUtilities.convertPoint()来转换坐标。
答案 1 :(得分:1)
此代码应显示真正的问题,取决于此代码剪切之前/之后的代码或是否使用
2)Sorting and Filtering,阅读有关convertXxxIndexToXxx方法的信息
修改您的问题答案 2 :(得分:1)
作为替代方案,请使用自定义TableCellEditor
,如图所示here。当编辑器结束时,您将能够在模型的setValueAt()
实现中明确地确定受影响的行。