This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic