Home Segments Top Top Previous Next

1006: Mainline

If you wish to allow row selection, you have three options: You can allow to be selected only single rows; only contiguous rows; or any rows. You specify your choice by calling the setSelectionMode method with one of three values stored as static variable values in the ListSelectionModel interface, such as SINGLE_INTERVAL_SELECTION. You drop the statement that calls the setRowSelectionAllowed method with false as the argument.

import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class RatingTable extends JTable {
 public RatingTable (Vector rows, Vector columns) {
  super(new RatingTableModel(rows, columns));
  TableColumnModel model = getColumnModel();
  for (int i = 0; i < columns.size(); ++i) {
   TableColumn column = model.getColumn(i);
   if (i == 0) {
    column.setPreferredWidth(417);
   }
   else {
    column.setPreferredWidth(83);
   }
  }
  setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); 
  MovieTableCellRenderer renderer = new MovieTableCellRenderer();        
  setDefaultRenderer(Integer.class, renderer);                           
  setDefaultRenderer(String.class, renderer);                            
 } 
} 

Other argument choices are MULTIPLE_INTERVAL_SELECTION and SINGLE_SELECTION.