Netbeans: How to add valueChanged listener to JTable from GUI constructor?

I right-clicked JTable and pasted some code into the "mail listener code" in a terrible kludge.

I do not see the possibility to add

table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
  public void valueChanged(ListSelectionEvent evt) {

to "events" in the design view for JTable. I am sure there is a way to add valueChanged (ListSelectionEvent evt) from the project view, but how?

maybe this is a bug ?

Row selection change events are created using the ListSelectionModel JTable, not the JTable itself - so the event cannot be represented in the Component Inspector (as a JTable event). The processing of this event must be done manually, for example. as:

jTable1.getSelectionModel().addListSelectionListener(
    new javax.swing.event.ListSelectionListener() {
        public void valueChanged(ListSelectionEvent evt) {
            customRowSelectionEventHandler(evt);
        }
    }
);

, ListSelectionModel JTable "", "" ?

+3
2

ListSelectionListener . jTable1 Post-init Code:

jTable1.getSelectionModel().addListSelectionListener(new MyListener());

:

private static class MyListener implements ListSelectionListener {

    @Override
    public void valueChanged(ListSelectionEvent e) {
        System.out.println(e.getFirstIndex());
    }
}
+5

, InputVerifier.

, , .

public class TableVerifier extends InputVerifier {

    @Override
    public boolean verify(JComponent input) {
        assert input instanceof JTable : "I told you I wanted a table!";

        JTable inputTable = (JTable) input;
        int numberColumns = inputTable.getColumnCount();
        int numberRows = inputTable.getRowCount();

        for (int column = 0; column < numberColumns; column++) {
            for (int row = 0; row < numberRows; row++) {
                //DO YOUR STUFF
            }
        }
        return true;
    }
}
+2

All Articles