Posts tagged ‘JProgressBar en JTable’

noviembre 13, 2010

JProgressBar en JTable

import java.awt.Component;
import java.util.EventObject;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.event.CellEditorListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

/**
* @autor Leyer
*/
public class progressFrame extends JFrame {
	private static final long serialVersionUID = 1L;
    class TABLE_MODEL extends DefaultTableModel{
		private static final long serialVersionUID = 1L;
		public TABLE_MODEL(){
			addColumn("Progress");
			addColumn("Archivo");
		}
		public void addRow(String value){
			Object rowData[]={new JProgressBar(),value};
			addRow(rowData);
		}
	}
    class CELL_RENDERER implements TableCellRenderer{
		@Override
		public Component getTableCellRendererComponent(JTable arg0,
				Object arg1, boolean arg2, boolean arg3, int arg4, int arg5) {
	    	try{
	        return (JProgressBar)arg1;
	    	}catch (Exception e) {}
	     return (JProgressBar)arg1;
		}
    }
	public progressFrame(){
		this.setSize(500,300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		TABLE_MODEL mod=new TABLE_MODEL();
		JTable table=new JTable(mod);
		mod.addRow("dctr1.doc");
		mod.addRow("java.txt");
		mod.addRow("dctr3.doc");
		mod.addRow("dctr4.doc");
		mod.addRow("dctr5.doc");
		table.getColumnModel().getColumn(0).setCellRenderer(new CELL_RENDERER());
		table.getColumnModel().getColumn(0).setCellEditor(new TableCellEditor() {
			@Override
			public boolean stopCellEditing() {
				return false;
			}
			@Override
			public boolean shouldSelectCell(EventObject arg0) {
				return false;
			}
			@Override
			public void removeCellEditorListener(CellEditorListener arg0) {}
			@Override
			public boolean isCellEditable(EventObject arg0) {
	             return false;
			}
			@Override
			public Object getCellEditorValue() {
				return null;
			}
			@Override
			public void cancelCellEditing() {}
			@Override
			public void addCellEditorListener(CellEditorListener arg0) {	}	
			@Override
			public Component getTableCellEditorComponent(JTable table, Object value,
					boolean isSelected, int row, int column) {
				return null;
			}
		});
		JProgressBar progressBar1=(JProgressBar)table.getValueAt(0, 0);
		progressBar1.setStringPainted(true);
		progressBar1.setValue(10);
		table.repaint();
		JProgressBar progressBar2=(JProgressBar)table.getValueAt(1, 0);
		progressBar2.setStringPainted(true);
		progressBar2.setValue(45);
		table.repaint();
		JProgressBar progressBar3=(JProgressBar)table.getValueAt(2, 0);
		progressBar3.setStringPainted(true);
		progressBar3.setValue(25);
		table.repaint();
		JProgressBar progressBar4=(JProgressBar)table.getValueAt(3, 0);
		progressBar4.setStringPainted(true);
		progressBar4.setValue(15);
		table.repaint();
		JScrollPane scrollPane=new JScrollPane(table);
		getContentPane().add(scrollPane);
	}
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				new progressFrame().setVisible(true);
			}
		});
	}
}
Anuncios