JTable selection listener

puneetverma0711 picture puneetverma0711 · Apr 9, 2013 · Viewed 37.1k times · Source

I have a code which displays Table in applets & consists of two columns:-

  1. image icon
  2. description

Here's my code:

    import javax.swing.table.*;

    public class TableIcon extends JFrame
     {
    public TableIcon()
    {
    ImageIcon aboutIcon = new ImageIcon("about16.gif");
    ImageIcon addIcon = new ImageIcon("add16.gif");
    ImageIcon copyIcon = new ImageIcon("copy16.gif");

    String[] columnNames = {"Picture", "Description"};
    Object[][] data =
    {
        {aboutIcon, "About"},
        {addIcon, "Add"},
        {copyIcon, "Copy"},
    };

    DefaultTableModel model = new DefaultTableModel(data, columnNames);
    JTable table = new JTable( model )
    {
        //  Returning the Class of each column will allow different
        //  renderers to be used based on Class
        public Class getColumnClass(int column)
        {
            return getValueAt(0, column).getClass();
        }
    };
    table.setPreferredScrollableViewportSize(table.getPreferredSize());

    JScrollPane scrollPane = new JScrollPane( table );
    getContentPane().add( scrollPane );
}

public static void main(String[] args)
{
    TableIcon frame = new TableIcon();
    frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
    frame.pack();
    frame.setVisible(true);
  }

} 

Now what i want to know is how can I implement selection listener or mouse listener event on my table , such that it should select a particular image from my table and display on text area or text field(my table contains path of image file)?

Can I add text field on table & table on frame? Please feel free to ask queries if required.

Answer

MackTheKnife picture MackTheKnife · Nov 29, 2013

In my code I have a table where I set single selection mode; in my case, listener described in How to Write a List Selection Listener (with a for loop from getMinSelectionIndex to getMaxSelectionIndex) is not useful because releasing mouse button I'm sure I have just one row selected.

So I've solved as follows:

....

int iSelectedIndex =-1;

....

JTable jtable = new JTable(tableModel); // tableModel defined elsewhere
jtable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

ListSelectionModel selectionModel = jtable.getSelectionModel();

selectionModel.addListSelectionListener(new ListSelectionListener() {
    public void valueChanged(ListSelectionEvent e) {
        handleSelectionEvent(e);
    }
});

....

protected void handleSelectionEvent(ListSelectionEvent e) {
    if (e.getValueIsAdjusting())
        return;

    // e.getSource() returns an object like this
    // javax.swing.DefaultListSelectionModel 1052752867 ={11}
    // where 11 is the index of selected element when mouse button is released

    String strSource= e.getSource().toString();
    int start = strSource.indexOf("{")+1,
        stop  = strSource.length()-1;
    iSelectedIndex = Integer.parseInt(strSource.substring(start, stop));
}

I think this solution, that does not require a for loop between start and stop to check which element is selectes, is more suitable when table is in single selection mode