我是靠谱客的博主 年轻朋友,最近开发中收集的这篇文章主要介绍java 交易不返回,Java rowAtPoint()不返回正确的值,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

我正在制作一个java项目,我正在使用JTable。 我想要做的事情是链接popupmenu到jtable。 popupmenu有四个不同的items。一些items需要知道jtable中选定的row。问题在于selectRow-功能。我无法获得返回正确的row的功能。我点击jtable与right mouse button和我得到一个popupmenu弹出在同一列我点击它应该是。但是当我使用addSelectedRow或removeSelectedRow函数(在menuItem中,标签为“addSelected”或“deleteSelected”)以及任何row(s)未被选中时,我使用selectRow-功能找到row我点击right mousebutton。似乎rowAtPoint(Point p)不能找到一个正确的行。它返回eather 0或-1。我一直在近一个星期这个问题所困扰,从而pleace皮条客我的代码:d我希望代码非常干净:dJava rowAtPoint()不返回正确的值

这是一个问题的屏幕截图。 JTable总是至少有一行。我点击任意一行(在这种情况下第二个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.(需要删除该错误),但主要工作pritty好。

< - - >

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);

}

谢谢大家救了我的天!

最后

以上就是年轻朋友为你收集整理的java 交易不返回,Java rowAtPoint()不返回正确的值的全部内容,希望文章能够帮你解决java 交易不返回,Java rowAtPoint()不返回正确的值所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部