Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Jtable with checkbox and Progressbar

Status
Not open for further replies.

FOR111

Programmer
Sep 29, 2005
103
MT
Hi All

I'm trying to implement a Jtable, each row must have a progrss bar and a checkbox. The checkbox, when clicked must initiate an event which increases the progressbar by 1, if the checkbox is false, then reduces it by one!

Can enyone give me a helping hand with this..i'm not too versitle with swing.

Thanks
Nick
 
So the checkbox will always be true or false, and - depending on the initial state of progressbar (assume: 0) and the checkbox (assume: false=unchecked), the progressbar will always be 0 or 1?

I don't understand the sense - but what do you have so far?
We help, but we don't do the whole work.

seeking a job as java-programmer in Berlin:
 
To display the progress bar, you'll need to provide a cell renderer to the JTable. Do this by implementing the TableCellRenderer interface and set it on the required table column.
Code:
table.getColumnModel().getColumn([i]<column index>[/i]).setCellRenderer([i]your renderer[/i]);

Please pay special attention to the 'Implementation Note' in the Java API Docs for the DefaultTableCellRenderer class. This class is the standard renderer for a JTable which is used by default and explains the way JTables are rendered.

As for the behaviour you requested, I'd subclass the AbstractTableModel to represent the table data and set this on the JTable. In the overridden setValueAt(int) method, you could process changes to a boolean value which represent the checkbox cells, and update the progress value (ints?) which represent the progress bar cells. If a progress value is changed, remember to inform the table using the fireTableCellUpdated method of the AbstractTableModel superclass.

Having a look through would also be of use, I think.

Tim
 
Hi stefan & timw

you're quite right, i should have forwarded my code; so here it goes:

Code:
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.EventObject;
import java.util.Hashtable;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;


public class TableSelectionTest extends JFrame  {

class CheckBoxRenderer extends JCheckBox implements TableCellRenderer {

  CheckBoxRenderer() {
    setHorizontalAlignment(JLabel.CENTER);
  }

  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    if (isSelected) {
      setForeground(table.getSelectionForeground());
      setBackground(table.getSelectionBackground());
    } else {
      setForeground(table.getForeground());
      setBackground(table.getBackground());
    }
    setSelected((value != null && ((Boolean) value).booleanValue()));
    return this;
  }
}

Action action = new AbstractAction("CheckBox") {
	
    public void actionPerformed(ActionEvent evt) {
    	
        JCheckBox cb = (JCheckBox)evt.getSource();

        boolean isSel = cb.isSelected();
        if (isSel) {
            System.out.println("true");
        } else {
        	System.out.println("false");
        }
    }
};



  public TableSelectionTest() {
    super("MultiComponent Table");

    DefaultTableModel dm = new DefaultTableModel() {
      public boolean isCellEditable(int row, int column) {
        if (column == 0) {
          return true;
        }
        return false;
      }
    };
    
    
    //Table Data
    dm.setDataVector(new Object[][] {
				    	{ new Boolean(false), "Franky", "50%", createBar(50, "50%")},
				        { new Boolean(false), "Joe", "60%", createBar(60, "60%") } }, 
        			new Object[] {
    					"CheckBox", "String", "Percentage", "Progress" });
    
    

    CheckBoxRenderer checkBoxRenderer = new CheckBoxRenderer();
    EachRowRenderer rowRenderer = new EachRowRenderer();
    
    rowRenderer.add(0, checkBoxRenderer);
    rowRenderer.add(1, checkBoxRenderer);
    
    
    JCheckBox checkBox = new JCheckBox(action);
    checkBox.setHorizontalAlignment(JLabel.CENTER);
    
    
    DefaultCellEditor checkBoxEditor = new DefaultCellEditor(checkBox);
    JTable table = new JTable(dm);


    EachRowEditor rowEditor = new EachRowEditor(table);
    
    rowEditor.setEditorAt(0, checkBoxEditor);
    rowEditor.setEditorAt(1, checkBoxEditor);


    table.getColumn("CheckBox").setCellRenderer(rowRenderer);
    table.getColumn("CheckBox").setCellEditor(rowEditor);


    
    JScrollPane scroll = new JScrollPane(table);
    getContentPane().add(scroll);
    setSize(400, 160);
    setVisible(true);
    
    
    table.getColumn("Progress").setCellRenderer(new ProgRenderer());
    
    
   
   
  }

   
  
  
  public static void main(String[] args) {
	  TableSelectionTest frame = new TableSelectionTest();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
  }
  
  public JProgressBar createBar(int percentDone, String text){
		JProgressBar progressBar = new JProgressBar(0, 100);
				
		progressBar.setStringPainted(true);
		progressBar.setValue(percentDone);
		progressBar.setString(text);
		
		return progressBar;
	}
}



class ProgRenderer implements TableCellRenderer{
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
		return (JProgressBar)value;
	}
	
}

class EachRowRenderer implements TableCellRenderer {
  protected Hashtable renderers;

  protected TableCellRenderer renderer, defaultRenderer;

  public EachRowRenderer() {
    renderers = new Hashtable();
    defaultRenderer = new DefaultTableCellRenderer();
  }

  public void add(int row, TableCellRenderer renderer) {
    renderers.put(new Integer(row), renderer);
  }

  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
	  
    renderer = (TableCellRenderer) renderers.get(new Integer(row));
    if (renderer == null) {
      renderer = defaultRenderer;
    }
    return renderer.getTableCellRendererComponent(table, value, isSelected,
        hasFocus, row, column);
  }
}

class EachRowEditor implements TableCellEditor {
  protected Hashtable editors;

  protected TableCellEditor editor, defaultEditor;

  JTable table;

  
  public EachRowEditor(JTable table) {
    this.table = table;
    editors = new Hashtable();
    defaultEditor = new DefaultCellEditor(new JTextField());
  }

    public void setEditorAt(int row, TableCellEditor editor) {
    editors.put(new Integer(row), editor);
  }

  public Component getTableCellEditorComponent(JTable table, Object value,
      boolean isSelected, int row, int column) {
    //editor = (TableCellEditor)editors.get(new Integer(row));
    //if (editor == null) {
    //  editor = defaultEditor;
    //}
    return editor.getTableCellEditorComponent(table, value, isSelected,
        row, column);
  }

  public Object getCellEditorValue() {
    return editor.getCellEditorValue();
  }

  public boolean stopCellEditing() {
    return editor.stopCellEditing();
  }

  public void cancelCellEditing() {
    editor.cancelCellEditing();
  }

  public boolean isCellEditable(EventObject anEvent) {
    selectEditor((MouseEvent) anEvent);
    return editor.isCellEditable(anEvent);
  }

  public void addCellEditorListener(CellEditorListener l) {
    editor.addCellEditorListener(l);
  }

  public void removeCellEditorListener(CellEditorListener l) {
    editor.removeCellEditorListener(l);
  }

  public boolean shouldSelectCell(EventObject anEvent) {
    selectEditor((MouseEvent) anEvent);
    return editor.shouldSelectCell(anEvent);
  }

  protected void selectEditor(MouseEvent e) {
    int row;
    if (e == null) {
      row = table.getSelectionModel().getAnchorSelectionIndex();
    } else {
      row = table.rowAtPoint(e.getPoint());
    }
    editor = (TableCellEditor) editors.get(new Integer(row));
    if (editor == null) {
      editor = defaultEditor;
    }
  }
}

The problem i'm having is that the action is set on the Object JcheckBox and not on the actual rendered checkbox, so i cannot retrieve row or anything else!

As regards to aim of this, its nothing special i just have to create a checkbox and a progressbar! why i need the checkbox to increase or decrease the progressbar...it has nothing to do with the actual thing, but more to learn how i could make events on rendered objects within the jtable..i will then use your help and implement further my aim.

thanks
Nick
 
As I said, using your own TableModel will make this happen for you. Reread my post. It can be done in the setValueAt() method in this model.

Tim
 
I'm not too sure why you need to subclass the checkbox... you should be able to just make that column Boolean and wait for "valuechanged" events.

Temporarily make your progress field a number, then put the custom renderer in when it's doing what you want.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top