Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JTable row selection

ChangNov 17 2010 — edited Nov 28 2010
Hello

I need a JTable where row selection is allowed, but not just with the whole row, but individual cells in a single interval of the row.
When the user has the mouse button pressed and moves the mouse inside the row the cells get added or released to the selection.
When he leaves the current row and releases the mouse button outside the row nothing is selected.
Additionally it would be nice that when the user leaves the row with pressed mouse button and again enters the row, the selection with the cell the user started
is highlighted again.
import javax.swing.*;
import javax.swing.event.*;


public class TestTable extends JPanel{

	private JTable table;
	private JFrame frame;
	/**
	 * @param args
	 */
	public TestTable(JFrame frame)
	{
		this.frame = frame;
		this.table = new JTable(10, 10);
		this.table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		//this.table.setRowSelectionAllowed( false );
		//this.table.setColumnSelectionAllowed( true );
		this.table.setCellSelectionEnabled(true);
		
		ListSelectionModel rowSelMod = this.table.getSelectionModel();
		rowSelMod.addListSelectionListener( (new ListSelectionListener() {

			@Override
			public void valueChanged(ListSelectionEvent e) {
				ListSelectionModel lsm = (ListSelectionModel)e.getSource();
				System.out.println(lsm.getAnchorSelectionIndex());	
			
			}
		}));
		this.add(this.table);
		
	}
	
	public static void main(String[] args) 
	{
		TableView.setMetalLookAndFeel();
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
	}
	
	private static void createAndShowGUI() 
	{
        JFrame frame = new JFrame("Example");
        //frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        TestTable newContentPane = new TestTable(frame);
        
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        frame.pack();
        frame.setVisible(true);
        frame.setLocation(10, 20);
    }

}
I tried a lot with the SelectionModel, JTable.rowSelectionAllowed() and JTable.columnSelectionAllowed(), but I did not get the behaviour i want to have.
The ListSelectionModel model in the valueChanged method has just information about the current column index, but no information about the row index.

Kind regards,
Chang
This post has been answered by 800346 on Nov 19 2010
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 26 2010
Added on Nov 17 2010
2 comments
301 views