Hello all,
I am writing a java application where I have a Jtable with checkboxes next to the data. If the user checks a box, I want to store a specific value from the data table. I can do this inelegantly by setting setCellSelectionEnabled(true) and adding the following to the event listener:

private class selectionListener implements ListSelectionListener {
        public void valueChanged(ListSelectionEvent event) {
            if (event.getValueIsAdjusting()) {
                return;
            }
            // This will return any value where the user clicks on a cell:
            Object value =  table.getValueAt(table.getSelectedRow(), (table.getSelectedColumn() - 1));
            //debug message for verification:
            System.out.println("Selected: " + value.toString());
            //Trying to determine who fired the event:
            Object source = event.getSource();
            System.out.println("From: " + source.toString());
            // An array list to store selected values:
            selection_array.add(value);
            
        }

Here is some sample output:

Selected: DONE C
From: javax.swing.DefaultListSelectionModel 27296482 ={7}

Is there a method I can invoke that will tell me who the caller was? The checkboxes are embedded in the table via an override to the Default Table model, they weren't explicitly defined by me. So, I think that the listener just knows that it is a cell selection event and is not concerned about the type.

Recommended Answers

All 7 Replies

I guess that if I know specically which column my checkboxes occur in, I can just specify something to the effect of:

if (table.getSelectedColumn() == the_column_the_checkboxes_are_in)
{
     Object value =  table.getValueAt(table.getSelectedRow(), (table.getSelectedColumn() - 1));
. . .
}

But my curiosity lays more with determining greater detail about event data. Thanks!

Is there a method I can invoke that will tell me who the caller was?

You basically already did. Object source = event.getSource() returns the Object that caused the event to be fired. So you could just use '==' to determine which Object it was at that point, if you need to.

You basically already did. Object source = event.getSource() returns the Object that caused the event to be fired. So you could just use '==' to determine which Object it was at that point, if you need to.

Right, but by calling the toString method, it returned the caller as the ListSelectionModel. Which is fine, I guess. It isn't completely necessary, and I've kludged a harmonious solution to what I want, but if I can embed event handlers and objects in form data, then I should be able to expose those behaviors. Like being able to fire an event when a check box is selected on a form. The way my code is structured now, an event fires whenever a cell is selected. The fact that there is a checkbox there or not is completely arbitrary. The code block I posted is ran everytime a user clicks on a cell on the data table (As it is caused by a listSelection event) i just added logic to limit what happens based on where something was selected.

To be honest, I'm not quite sure what you're asking, but it sounds a bit out of my league. The only reason JTable itself knows to even *put* the JCheckBoxes into your JTable is because it uses getValueAt(row,column).getClass() to see what class it is from. From the code you posted, you already know where the event is happening. If you want to know what type it is, you could use instanceof, I suppose. But I'm sure you know about that so I'm probably not being very helpful.

PS: You might want to rephrase your question, because there are a lot of people that regularly post in the Java forum who are much better with Swing than I am, so the fact that they haven't responded might indicate that they didn't understand your question either.

That's quite alright, maybe some other time I will investigate it further. I merely placed constraints on what will happen when a cellSelection event occurs by using getColumn(). If it matches the columns that are checked, then it adds a value to an array. That is all I need basically. As far as phrasing the question, it was tough to know exactly what it was that I needed.

You have a few options to work with.
- Use a TableModelListener to respond to the model data value change. Your check box is just a renderer/editor for a column value in your data model. The check box is not the data.

- Use a custom cell editor that extends JCheckBox to respond however you like to changes in the component value.

You can find examples of both in the Swing tutorial for using JTable:
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html


You can find examples of both in the Swing tutorial for using JTable:
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

Thanks! That tutorial was very helpful but was also the reason why I posted this in the first place. In the Sun tutorial, they add components and listeners for objects created outside of the data. There are checkboxes and radio buttons that fire events. The only event fired within the data table itself are cell editing events. I used that code as a basis for my table model very successfully and am pleased with that tutorial.
Where my question arises, if you run the jnlp, you can select checkboxes in the table data, and they fire events, but the event that is fired is no different than any other cell selection. It makes no difference if it's a checkbox, string or other object / component.
So what I did was add constraints on which selected cells (columns in this case) fired which events. I suppose I could call getColumnClass as well and if it's boolean, handle it that way. Either way, it works as intended, except the actionPerformed method isn't fired from a cell selection event, not sure how to make it do that, but it's ok, I have it working well enough at this point

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.