Java – All in One → Java JTable – Individual Row Selection tutorial – Camposha

Java – All in One

Java – All in One

Java – All in One

Java JTable – Individual Row Selection tutorial

This is a java jtable tutorial of how to allow only a single row selection.

The Plan

  • Simple JTable With several columns and Rows.
  • When selected we allow only single row to be selected.

Overview

  • JTable derives from Javax.Swing.JComponent.
  • Implements a couple of interfaces including ListSeelectionListener, CellEditorListener, TableModelListener, Scrollable among others.
  • It’s used to edit and display regular two-dimensional data in cells.
  • It doesn’t cache data, just displays it.
  • It allows a lot of customizations in terms of its rendering.
  • They are typically contained inside a JScrollpane.
  • Can enable filtering and sorting using Rowsorter.
  • DefaultTableModel is a model implementation using, yes, Vector of Vectors of Objects to store the values of cells.

===

Our Project

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class IndividualSelection extends JFrame {

  public IndividualSelection()
  {
    //SET TITLE
    super("Individual Selection");

    //DATA FOR OUR TABLE
    Object[][] data=
      {
        {"1","Man Utd",new Integer(2013),"21"},
        {"2","Man City",new Integer(2014),"3"},
        {"3","Chelsea",new Integer(2015),"7"},
        {"4","Arsenal",new Integer(1999),"10"},
        {"5","Liverpool",new Integer(1990),"19"},
        {"6","Everton",new Integer(1974),"1"},
      };

    //COLUMN HEADERS
    String columnHeaders[]={"Position","Team","Last Year Won","Trophies"};

    //CREATE OUR TABLE AND SET HEADER
    JTable table=new JTable(data,columnHeaders);

    //SCROLLPANE,SET SZE,SET CLOSE OPERATION
    JScrollPane pane=new JScrollPane(table);
    getContentPane().add(pane);
    setSize(450,100);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    //SET SELECTION MODE
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    ListSelectionModel model=table.getSelectionModel();

    //add listener
    model.addListSelectionListener(new ListSelectionListener() {

      @Override
      public void valueChanged(ListSelectionEvent e) {

        // JUST IGNORE WHEN USER HAS ATLEAST ONE SELECTION
        if(e.getValueIsAdjusting())
        {
          return;
        }
        ListSelectionModel lsm=(ListSelectionModel) e.getSource();

        if(lsm.isSelectionEmpty())
        {
          JOptionPane.showMessageDialog(null, "No selection");
        }else
        {
          int selectedRow=lsm.getMinSelectionIndex();
          JOptionPane.showMessageDialog(null, "Selected Row "+selectedRow);
        }
      }
    });

  }

  //MAIN METHOD
  public static void main(String[] args) {
    IndividualSelection i=new IndividualSelection();
    i.setVisible(true);
  }

}

Best Regards.

Leave a Reply

Your email address will not be published. Required fields are marked *

Rating

Not enough ratings to display
Join Us
X