0

I am new to Java so please be gentle lol. My question is this... I am used to C#, where making multiple items and having all their..say...mouse clicked events, go to one method that would sort out what control fired it etc. The thing is, I cant figure out how to do this in Java. I have several JTables and needs ONE method to handle the tablemodellistener events.

import javax.swing.event.*;
import javax.swing.table.TableModel;

public class SimpleTableDemo ... implements TableModelListener {
    ...
    public SimpleTableDemo() {
        ...
        table.getModel().addTableModelListener(this);
        ...
    }

    public void tableChanged(TableModelEvent e) {
        int row = e.getFirstRow();
        int column = e.getColumn();
        TableModel model = (TableModel)e.getSource();
        String columnName = model.getColumnName(column);
        Object data = model.getValueAt(row, column);

        ...// Do something with the data...
    }
    ...
}

so I tried changing "table.getModel().addTableModelListener(this);"

to:

table.getModel().addTableModelListener([MyMethod of type TableModelListener]);

but it will not compile. Please provide a complete example or really good hint. Thanks!

EDIT:

I got the code from here: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#modelchange

FrostyFire
  • 3,212
  • 3
  • 29
  • 53

2 Answers2

1

Java event handling (unfortunately) doesn't work like C# event handling does.

So

table.getModel().addTableModelListener([MyMethod of type TableModelListener]);

Will not work.


UPDATE for Java 8 and above

With Java 8 and lambda expressions the above statement is no longer true. If you have a method in your class with a signature matching tableChanged(TableModelEvent) of TableModelListener you can write

private void tableChangedEventHandler(TableModelEvent evt) {
    //do stuff
}

table.getModel().addTableModelListener(this::tableChangedEventHandler);

Note that you don't need to implement the interface and the method name should not match the name of the method in the interface.


Now to your question, you need to implement TableModelListener at some other class that its sole purpose is to handle the events, let's call it MyTableModelListener. After that, you either instantiate it wherever you need it

table.getModel().addTableModelListener(new MyTableModelListener());

or you make it singleton if you really need one method to handle the event as you said

table.getModel().addTableModelListener(MyTableModelListener.getInstance());
Stelios Adamantidis
  • 1,866
  • 21
  • 36
  • so, "MyTableModelListener" would be a class of type "MyTableModelListener" with what method(s)? – FrostyFire Jan 26 '14 at 00:59
  • `MyTableModelListener` will implement `TableModelListener`, an interface. So it will have to contain all methods that the interface contains, namely tableChanged(TableModelEvent e). For details see http://docs.oracle.com/javase/7/docs/api/javax/swing/event/TableModelListener.html – Stelios Adamantidis Jan 26 '14 at 01:03
1

In this example, a single TableModel has several listeners, including the JTable itself and an adjacent JList. Each JComponent contains an EventListenerList, examined here and here, which contains an arbitrary number of event listeners for that model.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045