English 中文(简体)
移除JTable公司最后一行的问题
原标题:Problem with removing last row of JTable

这是我第一次在这里要求我这样宽恕,如果我的英语不错的话,那是不合适的,也是令人不安的。

简言之,目前,我正在用桌子开发一个 Java式 app,我有问题。 我每行各行各有一顿可以删除。 一切都是一种征兆(一)在试图删除最后一行之前,可以删除无问题的措辞。 最后一行可以删除,但显然有例外,例如:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 4 >= 4
    at java.util.Vector.elementAt(Vector.java:427)
    at javax.swing.table.DefaultTableModel.setValueAt(DefaultTableModel.java:648)
    at javax.swing.JTable.setValueAt(JTable.java:2710)
    at javax.swing.JTable.editingStopped(JTable.java:4712)
    at javax.swing.AbstractCellEditor.fireEditingStopped(AbstractCellEditor.java:125)

从记分来看,“4”似乎是我以前删除的最后一行指数,但我对如何处理这一点没有任何想法。 我已经寻求解决办法,但我仍然能够解决问题。 请注意,在删除最后一行时,还有其他各行,然后可以删除其他各行。 同一例外也是在一点点点击删除的纽扣吨时造成的。

Oh和I利用DefaultTableModel的“Row()”来删除行文。 任何帮助都将受到赞赏。 感谢。

EDIT These are the code I use.

public class ViewDetail extends JInternalFrame implements ActionListener {
    ...
    String column[] = { "Aaa", "Bbb", "Delete This"};

    tableModel = new DefaultTableModel();
    Object row[][] = new Object[size][column.length];

    //fill the data from db
    int r = 0;
    for (int i = 0; i < size; i++) {
    row[r][0] = ...;
    row[r][1] = ...;
    row[r][2] = "Delete";
    r++;
    }

    tableModel.setDataVector(row, column);
    table = new JTable(tableModel);

    tableColumn = table.getColumn("Aaa");
    tableColumn.setPreferredWidth(75);
    tableColumn = table.getColumn("Bbb");
    tableColumn.setPreferredWidth(75);
    tableColumn = table.getColumn("Delete This");
    tableColumn.setPreferredWidth(75);
    tableColumn.setCellRenderer(new ButtonRenderer());
    tableColumn.setCellEditor(new ButtonEditor(new JCheckBox());
     ...
}

public class ButtonRenderer extends JButton implements TableCellRenderer {
    public ButtonRenderer() {
        setOpaque(true);
    }
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        if (isSelected) {
            setForeground(table.getSelectionForeground());
            setBackground(table.getSelectionBackground());
        } else {
            setForeground(table.getForeground());
            setBackground(UIManager.getColor("Button.background"));
        }
        setText((value == null) ? "" : value.toString());
        return this;
    }
}

public class ButtonEditor extends DefaultCellEditor {
    protected JButton button;
    private String label;
    private boolean isPushed;
    private JTable table;
    public ButtonEditor(JCheckBox checkBox) {
    super(checkBox);
    button = new JButton();
    button.setOpaque(true);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            fireEditingStopped();
            }
        });
    }

    public Component getTableCellEditorComponent(JTable table, Object value,
        boolean isSelected, int row, int column) {
    if (isSelected) {
        button.setForeground(table.getSelectionForeground());
        button.setBackground(table.getSelectionBackground());
    } else {
        button.setForeground(table.getForeground());
        button.setBackground(table.getBackground());
    }
    label = (value == null) ? "" : value.toString();
    button.setText(label);
    isPushed = true;
    this.table = table;
    return button;
    }

    public Object getCellEditorValue() {
    if (isPushed) {
                  DefaultTableModel tableModel = (DefaultTableModel) table.getModel();                            
                  tableModel.removeRow(table.getSelectedRow());                
            }
            isPushed = false;
    return new String(label);
    }

    public boolean stopCellEditing() {
    isPushed = false;
    return super.stopCellEditing();
}

protected void fireEditingStopped() {
        super.fireEditingStopped();
    }
}
最佳回答

醒来正试图把新价值带入你所走的一段! 要求将移解法变成可操作的,并在旋转的公用事业类别中使用“Later”来执行。

问题回答

您是否可以在《刑法》中添加内容并删除。

My first guess is that you are either doing the classical mistake "I have 4 elements in my table so to delete the last one I remove(4)" when you your table goes from 0 to 3 in indices

or you are trying to remove several object in the same loop like

  for(int i = 0; i<lenght; i++){
       if(i%2==0){//every even number
         tab.remove(i);
        }
    }

那么,你正在修改桌子的大小,这些内容已经改变,但你在删除时没有考虑到这一点。

这些人只是问问题的原因。 如果是这样的话,请在您的代码上填写。

表Button Column 显示,你如何能够将一栏作为纽芬兰文,以及如何在纽特被点击时添加执行的行动。 例如,采取行动是为了表明如何删除一行。

这里,你只需要删除原第1行。

private void refreshTable() { int rowCount= model.getRowCount(); // System.out.println(rowCount);

   for(int i=0;i<rowCount;i++ ){
        model.removeRow(0);
        //System.out.println(i);
   }

}

您可以增加新的可使用性,并检查烟火:

public class ButtonEditor extends DefaultCellEditor {

//create new variable 
private boolean isDeleteRow = false;
/////////////////

protected JButton button;
private String label;
private boolean isPushed;
private JTable table;
public ButtonEditor(JCheckBox checkBox) {
super(checkBox);
button = new JButton();
button.setOpaque(true);
button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        fireEditingStopped();
        }
    });
}

public Component getTableCellEditorComponent(JTable table, Object value,
    boolean isSelected, int row, int column) {
if (isSelected) {
    button.setForeground(table.getSelectionForeground());
    button.setBackground(table.getSelectionBackground());
} else {
    button.setForeground(table.getForeground());
    button.setBackground(table.getBackground());
}
label = (value == null) ? "" : value.toString();
button.setText(label);
isPushed = true;
this.table = table;

// set false when click to button
isDeleteRow = false; 
/////////////////
return button;
}

public Object getCellEditorValue() {
if (isPushed) {

              // set true when isPushed button
              isDeleteRow = true;   
              /////////////////

        }
        isPushed = false;
return new String(label);
}

public boolean stopCellEditing() {
isPushed = false;
return super.stopCellEditing();
}

protected void fireEditingStopped() {
    super.fireEditingStopped();

     //check if isDeleteRow, remove row
     if(isDeleteRow)
     {
          DefaultTableModel tableModel = (DefaultTableModel) table.getModel();                            
          tableModel.removeRow(table.getSelectedRow());   
     }
     /////////////////
}
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签