精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-12-07
最后修改:2010-03-18
Swing编程中JTable应该是个经常被用到的组件,进度条也不赖,有了它,不至于给用户“程序是不是死掉了”的疑惑,当然如果能做到像迅雷等下载工具那样,把表格和进度条组合在一起,那就太酷了! /** * 状态区表格模型类 * @author SavageGarden * */ class StatusTableModel extends DefaultTableModel { public Object[] rowData = {"..", 0, "", "", ""}; public StatusTableModel() { super(); addColumn("Name"); addColumn("Status"); addColumn("Size"); addColumn("Speed"); } /** * 设置为不可编辑 */ public boolean isCellEditable(int row, int column) { return false; } /** * 向状态区添加一个进度条 * */ public void addProgressBar() { rowData[0] = "文件" + (this.getRowCount() + 1); addRow(rowData); } }
/** * 工具条的渲染器 * @author SavageGarden * */ class ProgressBarRenderer extends DefaultTableCellRenderer{ private static final long serialVersionUID = 1L; private final JProgressBar b; public ProgressBarRenderer(){ super(); setOpaque(true); b = new JProgressBar(); //是否显示进度字符串 b.setStringPainted(true); b.setMinimum(0); b.setMaximum(100); //是否绘制边框 b.setBorderPainted(true); b.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1)); } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Integer i = (Integer) value; b.setValue(i); return b; } }
/** * 按钮的事件响应类 * @author SavageGarden * */ class AddButtonActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { statusTableModel.addProgressBar(); } }
/** * 主面板,显示表格和按钮 * @author SavageGarden * */ class FrameTest extends JFrame { private static int WIDTH; private static int HEIGHT; public JButton addButton; public JScrollPane statusScrollPane1; public static JTable statusTable; public static StatusTableModel statusTableModel; public FrameTest() { Toolkit kit = Toolkit.getDefaultToolkit(); Dimension screenSize = kit.getScreenSize(); WIDTH = screenSize.width/2; HEIGHT = screenSize.height/2; setSize(WIDTH, HEIGHT); setLocationRelativeTo(null); setResizable(false); setLayout(null); setTitle("FrameTest"); setVisible(false); createStatusPanel(); statusScrollPane1.setBounds(0, 0, WIDTH, 330); getContentPane().add(statusScrollPane1); addButton = new JButton("添加"); addButton.setBounds(WIDTH/2 -40, 350 , 80, 20); addButton.addActionListener(new AddButtonActionListener()); getContentPane().add(addButton); } /** * 创建状态区 * */ private void createStatusPanel() { statusTableModel = new StatusTableModel(); statusTable = new JTable(statusTableModel); //设置"Status"列由定制的ProgressBarRenderer渲染 TableColumn statusColumn = statusTable.getColumn("Status"); statusColumn.setCellRenderer(new ProgressBarRenderer()); statusScrollPane1 = new JScrollPane(statusTable); } }
/** * 主程序 * @author SavageGarden * */ public class SwingTest { public static void main(String args[]) { FrameTest frame = new FrameTest(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Integer i = (Integer) value; b.setValue(i); return b; }
public void actionPerformed(ActionEvent event) { statusTableModel.addProgressBar(); int price = 0; int index = statusTableModel.getRowCount()-1; while (price < 100) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } price ++; statusTableModel.setValueAt(price, index, 1); } }
public void actionPerformed(ActionEvent event) { statusTableModel.addProgressBar(); Thread t = new ProgressBarThread(); t.start(); }
/** * 进度条线程 * @author SavageGarden * */ class ProgressBarThread extends Thread { int price = 0; int index = statusTableModel.getRowCount()-1; public void run() { while (price < 100) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } price ++; SwingUtilities.invokeLater(new Runnable(){ public void run() { statusTableModel.setValueAt(price, index, 1); } }); } } }
/** * 用于实现观察者观察的对象---进度条的显示值 * @author SavageGarden * */ class ProgressBarObservable extends Observable { private String price; public String getPrice() { return price; } public void setPrice(String price) { this.price = price; setChanged(); notifyObservers(price); } } /** * 用于实现观察者 ---进度条的显示值变化时更新statusTableModel * @author SavageGarden * */ class ProgressBarObserver implements Observer { public void update(Observable o, Object arg) { String showValue = (String)arg; statusTableModel.setValueAt(Integer.parseInt(showValue.split(":")[1]), Integer.parseInt(showValue.split(":")[0]), 1); } }
progressBarObservable = new ProgressBarObservable(); progressBarObserver = new ProgressBarObserver(); progressBarObservable.addObserver(progressBarObserver);
SwingUtilities.invokeLater(new Runnable(){ public void run() { //statusTableModel.setValueAt(price, index, 1); progressBarObservable.setPrice(rowIndex + ":" + price); } });
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
浏览 6965 次