Java 如何使用导航按钮选择下一行?

Java 如何使用导航按钮选择下一行?,java,swing,awt,Java,Swing,Awt,下一个导航按钮未按预期工作 我已经创建了“上一个导航”按钮来选择GUI中的上一行,但“下一个导航”按钮未按预期工作。当我在表格中选择一行并单击“下一步”按钮时,它将选择表格中的最后一行。我怎样才能解决这个问题 这是我的项目的截图: 这是以前的导航按钮代码: private void btPreviousActionPerformed(java.awt.event.ActionEvent evt) {

下一个导航按钮未按预期工作

我已经创建了“上一个导航”按钮来选择GUI中的上一行,但“下一个导航”按钮未按预期工作。当我在表格中选择一行并单击“下一步”按钮时,它将选择表格中的最后一行。我怎样才能解决这个问题

这是我的项目的截图:

这是以前的导航按钮代码:

private void btPreviousActionPerformed(java.awt.event.ActionEvent evt) {                                           
    int rowCount = tbDataContact.getSelectedRow();

    for (int i = 0; i < rowCount; i++) {
        tbDataContact.clearSelection();
        tbDataContact.changeSelection(i, 0, false, false);
        Object col = tbDataContact.getValueAt(rowCount, 0);
        //lbPrevious.setText(String.valueOf(rowCount));
        showSelectedRow(i);
    }
} 
private void bPreviousActionPerformed(java.awt.event.ActionEvent evt){
int rowCount=tbDataContact.getSelectedRow();
对于(int i=0;i
我希望下一个按钮像上一个按钮一样工作

这是下一个Button代码:

private void btNextActionPerformed(java.awt.event.ActionEvent evt) {                                       

    int row = tbDataContact.getSelectedRow();
    int rowCount = tbDataContact.getRowCount();

    for (int i = 0; i < rowCount ; i++) {
        tbDataContact.clearSelection();
        tbDataContact.changeSelection(i, i, true, false);

        // get the column number and show in jlabel
        Object col = tbDataContact.getValueAt(i, 0);
        lbNext.setText(String.valueOf(rowCount));

        // Show data into form
        showSelectedRow(i); 
    }
}     
    private void btNextActionPerformed(java.awt.event.ActionEvent evt) {                                       

    int row = tbDataContact.getSelectedRow();
    int rowCount = tbDataContact.getRowCount();

    for (int i = 0; i < rowCount ; i++) {
        tbDataContact.clearSelection();
        tbDataContact.changeSelection(i, i, true, false);

        // get the column number and show in jlabel
        Object col = tbDataContact.getValueAt(i, 0);
        lbNext.setText(String.valueOf(rowCount));

        // Show data into form
        showSelectedRow(i); 
    }
}     
private void btNextActionPerformed(java.awt.event.ActionEvent evt){
int row=tbDataContact.getSelectedRow();
int rowCount=tbDataContact.getRowCount();
对于(int i=0;i

这是下一个Button代码:

private void btNextActionPerformed(java.awt.event.ActionEvent evt) {                                       

    int row = tbDataContact.getSelectedRow();
    int rowCount = tbDataContact.getRowCount();

    for (int i = 0; i < rowCount ; i++) {
        tbDataContact.clearSelection();
        tbDataContact.changeSelection(i, i, true, false);

        // get the column number and show in jlabel
        Object col = tbDataContact.getValueAt(i, 0);
        lbNext.setText(String.valueOf(rowCount));

        // Show data into form
        showSelectedRow(i); 
    }
}     
    private void btNextActionPerformed(java.awt.event.ActionEvent evt) {                                       

    int row = tbDataContact.getSelectedRow();
    int rowCount = tbDataContact.getRowCount();

    for (int i = 0; i < rowCount ; i++) {
        tbDataContact.clearSelection();
        tbDataContact.changeSelection(i, i, true, false);

        // get the column number and show in jlabel
        Object col = tbDataContact.getValueAt(i, 0);
        lbNext.setText(String.valueOf(rowCount));

        // Show data into form
        showSelectedRow(i); 
    }
}     
private void btNextActionPerformed(java.awt.event.ActionEvent evt){
int row=tbDataContact.getSelectedRow();
int rowCount=tbDataContact.getRowCount();
对于(int i=0;i
您已经编写了下一个按钮侦听器

private void btNextActionPerformed(java.awt.event.ActionEvent evt){
int row=tbDataContact.getSelectedRow();
int rowCount=tbDataContact.getRowCount();
对于(int i=0;i
其中有for循环,循环次数从0到总行数,每次使用
changeSelection(i,i,true,false)
正确的语法

public void changeSelection(introwindex,
int列索引,
布尔切换,
布尔扩展)
根据两个标志的状态更新表的选择模型:
切换
扩展
。UI接收到的键盘或鼠标事件对选择的大多数更改都通过此方法传递,因此该行为可能会被子类覆盖

此实现使用以下约定:
切换和扩展:

 1. toggle: false, extend: false. Clear the previous selection and ensure the new cell is selected.
 2. toggle: false, extend: true. Extend the previous selection from the anchor to the specified cell, clearing all other selections.
 3. toggle: true, extend: false. If the specified cell is selected, deselect it. If it is not selected, select it.
 4. toggle: true, extend: true. Apply the selection state of the anchor to all cells between it and the specified cell.
rowIndex - affects the selection at row
columnIndex - affects the selection at column
toggle - see description above
extend - if true, extend the current selection
从上面可以看出,第3条语句用于下一个按钮侦听器,第1条语句用于上一个按钮侦听器

参数:

 1. toggle: false, extend: false. Clear the previous selection and ensure the new cell is selected.
 2. toggle: false, extend: true. Extend the previous selection from the anchor to the specified cell, clearing all other selections.
 3. toggle: true, extend: false. If the specified cell is selected, deselect it. If it is not selected, select it.
 4. toggle: true, extend: true. Apply the selection state of the anchor to all cells between it and the specified cell.
rowIndex - affects the selection at row
columnIndex - affects the selection at column
toggle - see description above
extend - if true, extend the current selection
在每次迭代中,它将使用
tbDataContact.changeSelection(i,i,true,false)更改选择方法


长话短说,这里您不需要迭代循环进行选择。 您可以使用
getSelectedRow()
获取当前选定的行,并验证选定行是否为最后一行,如果不是,则增加行值并将其置于
changeSelection(increasedIndexNo,ColumnIndex,toggle,index)

示例:

 1. toggle: false, extend: false. Clear the previous selection and ensure the new cell is selected.
 2. toggle: false, extend: true. Extend the previous selection from the anchor to the specified cell, clearing all other selections.
 3. toggle: true, extend: false. If the specified cell is selected, deselect it. If it is not selected, select it.
 4. toggle: true, extend: true. Apply the selection state of the anchor to all cells between it and the specified cell.
rowIndex - affects the selection at row
columnIndex - affects the selection at column
toggle - see description above
extend - if true, extend the current selection
int row=tbDataContact.getSelectedRow();
int rowCount=tbDataContact.getRowCount();

如果(row欢迎使用Stack Overflow,请使用、阅读并发布一个复制您的问题的正确代码。到目前为止,您发布了
上一个
按钮的代码,但根据您的描述,您的错误代码是
下一个
按钮,但您没有显示任何代码。您是否使用Swing?请阅读链接和您要提供的问题丢失的信息(代码)你能用下一个导航按钮代码更新你的问题吗?这样我们就可以找出你下一个导航按钮代码不起作用的原因或者你缺少的地方吗?@DushyantTankariya我在问题中加入了OP答案中的代码,以防你想看一下。我不会,因为OP似乎根本不想看我提供的链接ded或发布正确的。谢谢您,先生。当我将真值更改为假值时,它工作。tbDataContact.changeSelection(++行,0,假,假);