修改此类渲染,将进度条的大小更改为变量?

时间:2014-03-14 09:03:04

标签: java swing jtable rendering jprogressbar

我有这段代码:

TableColumn column = jTable2.getColumnModel().getColumn(1);
column.setCellRenderer(new ProgressRenderer());

class ProgressRenderer:

class ProgressRenderer extends DefaultTableCellRenderer {
    private final JProgressBar b = new JProgressBar(0, 100);// I NEED RECEIVE DE VALUE,                //LIKE THIS new JProgressBar(0, MAX);
    private final JPanel p = new JPanel(new BorderLayout());

    public ProgressRenderer() {
        super();
        setOpaque(true);
    b.setStringPainted(true);
        p.add(b);
        //p.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
    }
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        Integer i = (Integer) value;

        String text = "Finalizou o Lote";
        if (i < 0) {
            text = "Pausa";
        } else if (i < 100) {
            b.setValue(i);
            return p;
        }
        super.getTableCellRendererComponent(table, text, isSelected, hasFocus, row, column);
        return this;
    }

    @Override
    public void updateUI() {
        super.updateUI();
        if (p != null) {
            SwingUtilities.updateComponentTreeUI(p);
        }
    }
}

现在,我需要,当ProgressRenderer调用函数()时,一个参数是进度条的最大值,但我不能这样做,不知道吗?

像这样:

TableColumn column = jTable2.getColumnModel().getColumn(1);
    column.setCellRenderer(new ProgressRenderer(MAX));

Class ProgressRenderer()

class ProgressRenderer extends DefaultTableCellRenderer {
    private final JProgressBar b = new JProgressBar(0, MAX);// I NEED RECEIVE DE VALUE,                //LIKE THIS new JProgressBar(0, MAX);
    private final JPanel p = new JPanel(new BorderLayout());

    public ProgressRenderer() {
        super();
        setOpaque(true);
...
...

----编辑---------- 谢谢你,但现在有了我的大问题,因为这个:

TableColumn column = jTable2.getColumnModel () GetColumn (1).; 
    column.setCellRenderer (new ProgressRenderer (maxValue));//

//我如何知道何时添加对象以更改maxValue

我将此代码用于jtable中的插入值

private LinhasOnline getLinha() {

    LinhasOnline linha = new LinhasOnline();
    linha.setNome("Linha "+j);
            linha.setcont(j*(10));
            linha.setmax(100);

            j++;
            return linha;
}

private void addLinha() {
    getModel().addLinha(getLinha());// 
}

//我的getmodel

private LinhasOnlineTableModel getModel() {
    if (model == null) {
        model = (LinhasOnlineTableModel) getTblLinhasOnline().getModel();
    }
    return model;
}

//我的getTblLinhasOnline()

 private JTable getTblLinhasOnline() {
    if (jTable2 == null) {
        jTable2 = new JTable();
        jTable2.setModel(new LinhasOnlineTableModel());
                    TableColumn column = jTable2.getColumnModel().getColumn(1);
                    column.setCellRenderer(new ProgressRenderer(max)); // need put the value of object( setmax)
    }
    return jTable2;
}

1 个答案:

答案 0 :(得分:0)

您可以像下一样更改您的课程:

class ProgressRenderer extends DefaultTableCellRenderer {
    private final JProgressBar b;// I NEED RECEIVE DE VALUE,                //LIKE THIS new JProgressBar(0, MAX);
    private final JPanel p = new JPanel(new BorderLayout());

    public ProgressRenderer(int maxValue) {
        super();
        b  = new JProgressBar(0, maxValue);
        setOpaque(true);
        b.setStringPainted(true);
        p.add(b);
    }
    ...
}