wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable" Watch "JTable" New topic
Author

JTable

mohamed hamdy
Ranch Hand

Joined: Feb 13, 2003
Posts: 72
hi,
i wanted to use TableCellRenderer to renderer a progress bar whose value is changed by a manipulator thread.
i intended to insert new progress bar (in new row ) by pressing a start button giving the maximum value and the sleep time by two text fields.
so i wrote the following code but i'd confused with the dispatch event exceptions thron when i pressed the start button.
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Table2Test extends JFrame{
private JTextField max,time;
private ArrayList progressBars;
private int rowCount;
private MyTableModel tableModel;
Table2Test(){
super("table");
Container contentPane=getContentPane();
contentPane.setLayout(new BorderLayout());
JPanel p=new JPanel();
p.add(new JLabel("max:"));
max=new JTextField();
max.setColumns(5);
p.add(max);
p.add(new JLabel("sleep time:"));
time=new JTextField();
time.setColumns(5);
p.add(time);
JButton start=new JButton("start");
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int maxV=Integer.parseInt(max.getText());
int sleepTime=Integer.parseInt(time.getText());
JProgressBar progress=new JProgressBar(0,maxV);
progressBars.add(rowCount,progress);
new ProgressManipulator(rowCount,maxV,sleepTime);
tableModel.fireTableRowsInserted(rowCount,rowCount);
rowCount++;
}
});
p.add(start);
contentPane.add(p,BorderLayout.NORTH);
tableModel=new MyTableModel();
JTable table=new JTable(tableModel);
table.setDefaultRenderer(Integer.class,new MyCellRenderer());
JScrollPane jScrollPane=new JScrollPane(table);
contentPane.add(jScrollPane,BorderLayout.CENTER);
setSize(600,600);
setVisible(true);
}
class ProgressManipulator extends Thread{
private int row,max,sleepTime,value;
ProgressManipulator(int row,int max,int sleepTime){
this.row=row;
this.max=max;
this.sleepTime=sleepTime;
}
public void run(){
while(value<=max){
value++;
tableModel.setValueAt(new Integer(value),row,2);
tableModel.fireTableCellUpdated(row,2);
try{
sleep(sleepTime*1000);
}
catch(InterruptedException e){
}
}
}
}
class MyCellRenderer implements TableCellRenderer{
public Component getTableCellRendererComponent(
JTable table,Object value,boolean isSelected,
boolean hasFocus,int row,int column){
JPanel p=new JPanel();
JProgressBar progress=(JProgressBar)progressBars.get(row);
Integer x=(Integer)value;
progress.setValue(x.intValue());
p.add(progress);
return p;
}
}
public static void main(String[] args){
new Table2Test();
}
}
class MyTableModel extends AbstractTableModel{
private ArrayList data;
private final String dummyStr="zzzzzzz";
MyTableModel(){
data=new ArrayList();
}
public int getRowCount(){return data.size();}
public int getColumnCount(){return 3;}
public Object getValueAt(int row,int column){
if(column==2) return(Integer)data.get(row); else return dummyStr;
}
public void setValueAt(Object value,int row,int column){
if(column==2) data.add(row,(Integer)value);
}
public String getColumnName(int column){
return "column"+(column+1);
}
}
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Take a look at this link.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable
 
Similar Threads
Working with Jtable
about JTable
Dynamic JTable
JRadioButton in JTable
Color in JTable