Sunday, December 29, 2013

Java Swing JTable and ListSelectionListener

Example of Swing JTable and ListSelectionListener. JTable is used to display and edit regular two-dimensional tables of cells.

Example of JTable
Example of 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();
        listSelectionModel.addListSelectionListener(new MyListSelectionListener());
        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);
 
    }

    class MyListSelectionListener implements ListSelectionListener{

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



It can be noticed from the demo video that if click on another cell in the same row, MyListSelectionListener() will not be called. Read next post "Detect selection on individual cell in JTable".

No comments:

Post a Comment