Results 1 to 2 of 2
  1. #1
    shanssat is offline Member
    Join Date
    Jan 2009
    Rep Power

    Default Problem in JTable row selection

    Hi All,
    I'm using JTable in my application. It has 4 columns and rows are fetched from database. 1st column contains JCheckBox to select the particular row in the table. 1st column header also contains JCheckBox. All rows in the table are selected and unselected by check and uncheck the column header JCheckBox.

    The problem is,
    If I check anyone of the row JCheckBox then the particular row got selected and If I check the column header JCheckBox then that check box is checked and all rows are selected.

    But If I check the 1st row JCheckBox then the 1st row got selected and If I check the Column header JCheckBox then column header JCheckBox is chekced but no rows are selected (1st row also deselected).

    I tested the same scenario for 2nd and 3rd rows but everything works fine. Only 1st row gives this problem.

    My Code is,

    Java Code:
    // Creates the JCheckBox in the table header
    		TableColumn tc = table.getColumnModel().getColumn(0);
    		tc.setHeaderRenderer(new APUCheckBoxHeader(new MyItemListener()));

    APUCheckBoxHeader class :

    Java Code:
    public class APUCheckBoxHeader extends JCheckBox implements TableCellRenderer, MouseListener {
    	private static final long serialVersionUID = 6418751901993722076L;
    	protected APUCheckBoxHeader rendererComponent;
    	protected int column;
    	protected boolean mousePressed = false;
    	public APUCheckBoxHeader(ItemListener itemListener) {
    		rendererComponent = this;
    	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
    			boolean hasFocus, int row, int column) {
    		if (table != null) {
    			JTableHeader header = table.getTableHeader();
    			if (header != null) {
    		// rendererComponent.setText("Check All");
    		return rendererComponent;
    	protected void setColumn(int column) {
    		this.column = column;
    	public int getColumn() {
    		return column;
    	protected void handleClickEvent(MouseEvent e) {
    		if (mousePressed) {
    			mousePressed = false;
    			JTableHeader header = (JTableHeader) (e.getSource());
    			JTable tableView = header.getTable();
    			TableColumnModel columnModel = tableView.getColumnModel();
    			int viewColumn = columnModel.getColumnIndexAtX(e.getX());
    			// int column = tableView.convertColumnIndexToModel(viewColumn);
    			// if ((viewColumn == this.column) && (e.getClickCount() == 1) && (column != -1)) {
    			if (viewColumn == this.column) {
    	public void mouseClicked(MouseEvent e) {
    		((JTableHeader) e.getSource()).repaint();
    	public void mousePressed(MouseEvent e) {
    	public void mouseReleased(MouseEvent e) {
    		mousePressed = true;
    	public void mouseEntered(MouseEvent e) {
    	public void mouseExited(MouseEvent e) {

    MyItemListener class :

    Java Code:
    class MyItemListener implements ItemListener {
    		public void itemStateChanged(ItemEvent e) {
    			Object source = e.getSource();
    			if ((source instanceof AbstractButton) == false) {
    			boolean checked = e.getStateChange() == ItemEvent.SELECTED;
    			if (checked) {
    				// Select all rows if JCheckBox in the table header is selected and enable the Archive button
    			} else {
    				// Uncheck all rows if JCheckBox in the table header is unselected and disable the Archive
    				// button

    Could anyone please tell me how to solve this issue?

    Thanks in advance,

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

Similar Threads

  1. Multiple selection on JTable
    By hendrix79 in forum New To Java
    Replies: 2
    Last Post: 01-30-2009, 06:11 PM
  2. JTable Combo Box Row Selection Problem
    By hemanthjava in forum AWT / Swing
    Replies: 2
    Last Post: 12-18-2008, 07:31 AM
  3. JTable problem
    By robbertds1 in forum AWT / Swing
    Replies: 1
    Last Post: 05-20-2008, 06:34 PM
  4. Replies: 2
    Last Post: 05-15-2008, 10:15 AM
  5. Problem with JTable
    By Felissa in forum AWT / Swing
    Replies: 2
    Last Post: 07-04-2007, 09:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts