Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

JTable

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at this link.
 
Water proof donuts! Eat them while reading this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic