3

I right clicked the JTable and inserted some code into "post listeners code" in an awful kludge.

I don't see an option to add

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

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

maybe it's a bug?

Row selection change events are produced by ListSelectionModel of JTable, not by JTable itself - so the event cannot be presented in Component Inspector (as event of JTable). Handling this event must be done manually, e.g. like:

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

Although maybe there's a way to get the ListSelectionModel for a JTable outside of the "blue", "managed," code?

Thufir
  • 8,216
  • 28
  • 125
  • 273
  • What exactly are you asking? There's not a question mark in your question. Also, as a side note, [condense your code a little](http://meta.stackexchange.com/a/129787/181088) – gobernador May 06 '12 at 01:33
  • 1
    How to add a valueChanged listener to a JTable via the "design" view of Netbeans? I don't see that option in the GUI builder. – Thufir May 06 '12 at 01:59
  • @Thufir: A designer source file can be compiled, but changing the design requires the corresponding `.form` file. Even then, it's a pain to reconstruct. Also, nothing _requires_ that you use the designer for _everything_, nor is `GroupLayout` mandatory. It's a power saw: use it to cut treads to length, not to carve chess pieces. – trashgod May 06 '12 at 07:36
  • good point. I like the "clicky" stuff for GUI building, but maybe I'll make a custom JPane for my JTables, and then add those to other JPane's with the designer...? – Thufir May 06 '12 at 09:37
  • 1
    @Thufir: Exactly, you can add such panel(s) to your main layout, as shown [here](http://stackoverflow.com/a/2561540/230513). Changing a panel's layout is a good way to learn about the less common ones. – trashgod May 06 '12 at 09:47
  • Almost exactly re-asked this question [again](http://stackoverflow.com/a/11658767/262852). – Thufir Jul 26 '12 at 01:02

2 Answers2

5

You can create your own ListSelectionListener in the editable part of the source. You can add an instance of the listener to the selection model of the class variable jTable1 in your table's Post-init Code property:

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

The listener itself might look like this:

private static class MyListener implements ListSelectionListener {

    @Override
    public void valueChanged(ListSelectionEvent e) {
        System.out.println(e.getFirstIndex());
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

Perhaps you could extend InputVerifier.

It's not exactly what it was intended to do, but you could adapt it for your uses.

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;
    }
}
gobernador
  • 5,659
  • 3
  • 32
  • 51