Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: JTable CheckBoxes

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default JTable CheckBoxes

    I have a JTable, X Rows by 4 Columns. The first column is text, but the other 3 are CheckBoxes. I am attempting to make it so when 1 CheckBox is selected, the other 2 in its row deselect. However I cant seem to get that to work. To specify, the Checkboxes just don't deselect.

    Here is my current attempt:
    table.getColumnModel().getColumn(1).setCellRenderer(new TableCellRenderer()
    {
    	public Component getTableCellRendererComponent(
    		JTable table, Object value, boolean isSelected,boolean isFocused, int row, int col)
    		{
    			boolean marked = (Boolean) value;
    			JCheckBox rendererComponent = new JCheckBox(modelSel.get(row).B717+"");
    			if(marked)
    			{
    				rendererComponent.setSelected(true);
    			}
    		return rendererComponent;
    		}
    	});
    DefaultCellEditor cellEditor1 = new DefaultCellEditor(new JCheckBox());
    cellEditor1.setClickCountToStart(1);
    cellEditor1.addCellEditorListener(new CellEditorListener(){
    	public void editingCanceled(ChangeEvent e)
    	{ 		}
    	public void editingStopped(ChangeEvent e)
    	{
    		if(((JCheckBox)((DefaultCellEditor)e.getSource()).getComponent()).isSelected())
    	    	{
    	    		data[table.getSelectedRow()][2] = false;
    			data[table.getSelectedRow()][3] = false;
    		}
    		else
    		{
    	    		data[table.getSelectedRow()][2] = true;
    			data[table.getSelectedRow()][3] = false;
    		}
    	data[table.getSelectedRow()][1] = ((JCheckBox)((DefaultCellEditor)e.getSource()).getComponent()).isSelected();
    	}
    });
    table.getColumnModel().getColumn(1).setCellEditor(cellEditor1);

    In this attempt, I tried to make the values change in the CellEditorListener, but that doesnt seem to do it.


    On a parallel, I have an issue when I close my JDialog when it reference those cells. It should try to find the column in each row that has a check, and set the value that corresponds to that column.

    For a 5 Entry Test, I make the following Selections at Runtime:
    Entry 1: Column 2
    Entry 2: Column 3
    Entry 3: Column 1
    Entry 4: Column 2
    Entry 5: Column 3

    My printout is:
    Entry 1 chose B737
    Entry 2 chose B717
    Entry 3 chose B717
    Entry 4 chose B737
    Entry 5 chose Blended

    The printout should be:
    Entry 1 chose B737
    Entry 2 chose Blended
    Entry 3 chose B717
    Entry 4 chose B737
    Entry 5 chose Blended

    I would think the problem is most likely a result of my selections not working correctly, but I'm not totally sure of that yet, and I wont be until I get the selections working right.

    tempDialog.addWindowListener(new WindowAdapter()
    {
    	public void windowClosing(WindowEvent e)
    	{
        		for(int i=0;i<data.length;i++)
        		{
        			String name = data[i][0].toString();
        			int index = -1;
        			for(int x=0;x<marketList.size();x++)
        			{
        				if(marketList.get(x).Name.equals(name))
        				{
        					index = x;
        					break;
        				}
        			}
        			int number = 0;
        			Object[] it = data[i];
        			if((Boolean)(data[i][1]))
        			{
        				marketList.get(index).Cost = modelSel.get(i).B717;
        				System.out.println(name+" chose B717");
        			}
        			else if((Boolean)(data[i][2]))
        			{
        				marketList.get(index).Cost = modelSel.get(i).B737;
        				System.out.println(name+" chose B737");
        			}
        			else if((Boolean)(data[i][3]))
        			{
        				marketList.get(index).Cost = modelSel.get(i).Blended;
        				System.out.println(name+" chose Blended");
        			}
        			else
        			{
        				System.out.println("Error with "+name);
        			}
        		}
        	}
    });


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: JTable CheckBoxes

    Lol, nevermind. I figured both out like the second I posted this.

    If anyone has a similar issue, I forgot the most important statement:
    table.setValueAt(true,table.getSelectedRow(),1);

    Adding those fixed both issues.

Similar Threads

  1. Populating a JTable
    By crism85 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 20th, 2010, 01:57 PM
  2. JTable in JScrollPane
    By alwayslearner in forum AWT / Java Swing
    Replies: 1
    Last Post: January 31st, 2010, 10:24 PM
  3. Printing a JTable
    By hundu in forum AWT / Java Swing
    Replies: 0
    Last Post: June 29th, 2009, 08:15 AM
  4. How to printing a Jtable
    By hundu in forum AWT / Java Swing
    Replies: 0
    Last Post: June 29th, 2009, 06:57 AM
  5. Printing JTable that retrieve data from the Database
    By hundu in forum AWT / Java Swing
    Replies: 3
    Last Post: June 28th, 2009, 01:50 PM