Sunday, December 29, 2013

Detect selection on individual cell in JTable

Last post of "Java Swing JTable and ListSelectionListener" implement MyListSelectionListener() for listSelectionModel. It can be noticed from the demo video that if click on another cell in the same row, MyListSelectionListener() will not be called.

In this post, two seperated ListSelectionListener, MyRowListener and MyColListener, are implemented and added to jTable.getSelectionModel() and jTable.getColumnModel().getSelectionModel(), to monitor individual cell selected .

Detect selection on individual cell in JTable
Detect selection on individual cell in JTable

package javamyframe;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Label;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaMyFrame extends JFrame {

    Label labelInfo;
    JTable jTable;
    ListSelectionModel listSelectionModel;
    
    static final String DayOfWeek[] = {
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday"};
    
    Object[][] tableData = {
        {1, 2, 3, 4},
        {4, 3, 2, 1},
        {12, 20, 13, 14},
        {13, 29, 23, 24},
        {2, 4, 6, 8},
        {11, 21, 33, 4}};

    public static void main(String[] args) {
        
        SwingUtilities.invokeLater(() -> {
            createAndShowGUI();
        });
    }

    private static void createAndShowGUI() {
        JavaMyFrame myFrame = new JavaMyFrame();
        myFrame.setTitle("java-buddy.blogspot.com");

        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        myFrame.prepareUI();

        myFrame.pack();
        myFrame.setVisible(true);
    }
    
    private void prepareUI(){
        
        JPanel vPanel = new JPanel();
        vPanel.setLayout(new BoxLayout(vPanel, BoxLayout.Y_AXIS));
        
        jTable = new JTable(tableData, DayOfWeek);
        jTable.setMaximumSize(new Dimension(10,10));

        listSelectionModel = jTable.getSelectionModel();
        jTable.getSelectionModel()
                .addListSelectionListener(new MyRowListener());
        jTable.getColumnModel().getSelectionModel()
                .addListSelectionListener(new MyColListener());
        jTable.setSelectionModel(listSelectionModel);

        jTable.setFillsViewportHeight(true);
        JScrollPane jScrollPane = new JScrollPane(jTable);
        jScrollPane.setPreferredSize(new Dimension(450, 100));
        vPanel.add(jScrollPane);

        labelInfo = new Label();

        vPanel.add(labelInfo);
        
        getContentPane().add(vPanel, BorderLayout.CENTER);
    }
    
    private class MyRowListener implements ListSelectionListener {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                System.out.println("valueChanged: " + e.toString());
                int row = jTable.getSelectedRow();
                int col = jTable.getSelectedColumn();
                int selectedItem = (int)jTable.getValueAt(row, col);
                labelInfo.setText("MyRowListener: " 
                        + row + " : " + col + " = " + selectedItem);
            }
        }
    }
    
    private class MyColListener implements ListSelectionListener {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                System.out.println("valueChanged: " + e.toString());
                int row = jTable.getSelectedRow();
                int col = jTable.getSelectedColumn();
                int selectedItem = (int)jTable.getValueAt(row, col);
                labelInfo.setText("MyColListener: " 
                        + row + " : " + col + " = " + selectedItem);
            }
        }
    }
}



Next: JTable with TableModel

No comments:

Post a Comment