java:访问 AbstractTableModel 中的 JProgressBar

发布于 2024-11-15 04:17:23 字数 4747 浏览 2 评论 0原文

我正在使用以下代码,其中包含 AbstractTableModel 内的 JProgressBar。 JProgressBar 进度值在 getValueAt() 函数的“情况 5:”中通过返回 Float 进行更新。我想要弄清楚的是如何访问底层 JProgressBar 实例,以便我可以修改它的一些属性,而不是进度值(例如 setVisible())。

package org.jdamico.jhu.runtime;

import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

// This class manages the upload table's data.
class TransfersTableModel extends AbstractTableModel implements Observer {

    /**
     * 
     */
    private static final long serialVersionUID = 2740117506937325535L;

    // These are the names for the table's columns.
    private static final String[] columnNames = { "Type","Name", "UUID","Status",
            "Stage","Progress","Total Start Time","Total End Time" };

    // These are the classes for each column's values.
    private static final Class[] columnClasses = { String.class,String.class,String.class, 
            String.class,String.class, JProgressBar.class, String.class, String.class};

    // The table's list of uploads.
    private ArrayList<ParentEntry> transferList = new ArrayList<ParentEntry>();

    // Add a new upload to the table.
    public void addTransfer(ParentEntry pe) {

        // Register to be notified when the upload changes.
        pe.addObserver(this);

        transferList.add(pe);

        // Fire table row insertion notification to table.
        fireTableRowsInserted(getRowCount(), getRowCount());
    }

    // Get a upload for the specified row.
    public ParentEntry getTransfer(int row) {
        return transferList.get(row);
    }

    // Remove a upload from the list.
    public void clearTransfer(int row) {
        transferList.remove(row);

        // Fire table row deletion notification to table.
        fireTableRowsDeleted(row, row);
    }

    // Get table's column count.
    public int getColumnCount() {
        return columnNames.length;
    }

    // Get a column's name.
    public String getColumnName(int col) {
        return columnNames[col];
    }

    // Get a column's class.
    public Class getColumnClass(int col) {
        return columnClasses[col];
    }

    // Get table's row count.
    public int getRowCount() {
        return transferList.size();
    }

    // Get value for a specific row and column combination.
    public Object getValueAt(int row, int col) {

        ParentEntry pe = transferList.get(row);
        switch (col) {
        case 0:
            return pe.getType();
        case 1: // URL
            return pe.getUrl();
        case 2: //UUID
            return pe.getUUID() + "";
        case 3: // Status
            return pe.getStatus().getDisplay();
        case 4: // Stage
            return pe.getStage().getDisplay();
        case 5: //Progress
            return new Float(pe.getProgress());
        case 6:
            if (pe.getTotalStartTime() != null)
                return pe.getTotalStartTime().getTime().toString();
            else
                return "";
        case 7:
            if (pe.getTotalEndTime() != null)
                return pe.getTotalEndTime().getTime().toString();
            else
                return "";
        }
        return "";
    }

    /*
     * Update is called when a Upload notifies its observers of any changes
     */
    public void update(Observable o, Object arg) {
        int index = transferList.indexOf(o);

        // Fire table row update notification to table.
        fireTableRowsUpdated(index, index);
    }
}

更新:这最初不是我的代码,我仍在熟悉它,但我刚刚发现有一个 ProgressRenderer 类扩展了 JProgressBar 并实现了 TableCellRenderer。这就是用于在表中呈现进度条的内容。因此,我将更改此代码以更改进度条的显示方式:

package org.jdamico.jhu.runtime;

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

/*import org.jdamico.jhu.components.Controller;
import org.jdamico.jhu.components.FilePartition;
import org.vikulin.utils.Constants;*/

// This class renders a JProgressBar in a table cell.
class ProgressRenderer extends JProgressBar implements TableCellRenderer {




    // Constructor for ProgressRenderer.
    public ProgressRenderer(int min, int max) {
        super(min, max);
    }

    /*
     * Returns this JProgressBar as the renderer for the given table cell.
     */
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        // Set JProgressBar's percent complete value.
        Float tmp = ((Float) value).floatValue();
        if (tmp == -1) {
            this.setVisible(false);
        }
        else {
            setValue((int) ((Float) value).floatValue());
            if (this.isVisible() == false)
                this.setVisible(true);
        }
        return this;
    }



}

I'm working with the following code that contains a JProgressBar inside of an AbstractTableModel. The JProgressBar progress value is updated in "case 5: " of the getValueAt() function by returning a Float. What I'm trying to figure out is how to access the underlying JProgressBar instance so I can modify some of it's properties other than the progress value (such as setVisible()).

package org.jdamico.jhu.runtime;

import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

// This class manages the upload table's data.
class TransfersTableModel extends AbstractTableModel implements Observer {

    /**
     * 
     */
    private static final long serialVersionUID = 2740117506937325535L;

    // These are the names for the table's columns.
    private static final String[] columnNames = { "Type","Name", "UUID","Status",
            "Stage","Progress","Total Start Time","Total End Time" };

    // These are the classes for each column's values.
    private static final Class[] columnClasses = { String.class,String.class,String.class, 
            String.class,String.class, JProgressBar.class, String.class, String.class};

    // The table's list of uploads.
    private ArrayList<ParentEntry> transferList = new ArrayList<ParentEntry>();

    // Add a new upload to the table.
    public void addTransfer(ParentEntry pe) {

        // Register to be notified when the upload changes.
        pe.addObserver(this);

        transferList.add(pe);

        // Fire table row insertion notification to table.
        fireTableRowsInserted(getRowCount(), getRowCount());
    }

    // Get a upload for the specified row.
    public ParentEntry getTransfer(int row) {
        return transferList.get(row);
    }

    // Remove a upload from the list.
    public void clearTransfer(int row) {
        transferList.remove(row);

        // Fire table row deletion notification to table.
        fireTableRowsDeleted(row, row);
    }

    // Get table's column count.
    public int getColumnCount() {
        return columnNames.length;
    }

    // Get a column's name.
    public String getColumnName(int col) {
        return columnNames[col];
    }

    // Get a column's class.
    public Class getColumnClass(int col) {
        return columnClasses[col];
    }

    // Get table's row count.
    public int getRowCount() {
        return transferList.size();
    }

    // Get value for a specific row and column combination.
    public Object getValueAt(int row, int col) {

        ParentEntry pe = transferList.get(row);
        switch (col) {
        case 0:
            return pe.getType();
        case 1: // URL
            return pe.getUrl();
        case 2: //UUID
            return pe.getUUID() + "";
        case 3: // Status
            return pe.getStatus().getDisplay();
        case 4: // Stage
            return pe.getStage().getDisplay();
        case 5: //Progress
            return new Float(pe.getProgress());
        case 6:
            if (pe.getTotalStartTime() != null)
                return pe.getTotalStartTime().getTime().toString();
            else
                return "";
        case 7:
            if (pe.getTotalEndTime() != null)
                return pe.getTotalEndTime().getTime().toString();
            else
                return "";
        }
        return "";
    }

    /*
     * Update is called when a Upload notifies its observers of any changes
     */
    public void update(Observable o, Object arg) {
        int index = transferList.indexOf(o);

        // Fire table row update notification to table.
        fireTableRowsUpdated(index, index);
    }
}

Update: This wasn't originally my code and I'm still familiarizing myself with it, but I just discovered that there was a ProgressRenderer class that extends JProgressBar and implements TableCellRenderer. This is what is being used to render the progress bar in the table. So I'm going to alter this code to alter how the progress bar is being displayed:

package org.jdamico.jhu.runtime;

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

/*import org.jdamico.jhu.components.Controller;
import org.jdamico.jhu.components.FilePartition;
import org.vikulin.utils.Constants;*/

// This class renders a JProgressBar in a table cell.
class ProgressRenderer extends JProgressBar implements TableCellRenderer {




    // Constructor for ProgressRenderer.
    public ProgressRenderer(int min, int max) {
        super(min, max);
    }

    /*
     * Returns this JProgressBar as the renderer for the given table cell.
     */
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        // Set JProgressBar's percent complete value.
        Float tmp = ((Float) value).floatValue();
        if (tmp == -1) {
            this.setVisible(false);
        }
        else {
            setValue((int) ((Float) value).floatValue());
            if (this.isVisible() == false)
                this.setVisible(true);
        }
        return this;
    }



}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傻比既视感 2024-11-22 04:17:23

您可以向该类添加一个方法,该方法允许您获取与感兴趣的行关联的 JProgressBar,如下所示:

   public JProgressBar getProgressBar(int row) {
      ParentEntry pe = transferList.get(row);
      return pe.getProgress();
   }

对我来说,模型将保存 JProgressBar 而不是表示完成百分比的简单 int ,这似乎很有趣。对我来说,将进度条显示的任何代码放在 JTable 的单元格渲染器中会更有意义。

编辑 1
回复opike的评论如下:

在模型外部使用 JProgressBar 与在模型内部使用 JProgressBar 真的会让代码更加高效吗?收益是否足以花费时间重写代码?现有代码在大多数情况下都按照我需要的方式工作,因此我宁愿只对其进行调整,而不是进行更重要的重写。

我的问题和担忧是,您的单元格渲染器是否使用模型持有的 JProgressBar?如果是的话,没有办法获取JProgressBar怎么办?如果仅使用获得的 Float 值,则模型保存未使用的信息和资源根本没有意义。是的,这可能会显着减慢你的程序速度。

You could add a method to the class that allows you to get the JProgressBar associated with the row of interest, something like:

   public JProgressBar getProgressBar(int row) {
      ParentEntry pe = transferList.get(row);
      return pe.getProgress();
   }

It seems kind of funny to me that the model would hold a JProgressBar rather than a simple int representing percentage complete. It would make more sense to me to have any code for progress bar display be in the JTable's cell renderer.

Edit 1
In reply to opike's comment below:

Does it really make the code that much more efficient to use the JProgressBar outside of the model vs inside of it? Is the gain enough so to expend time rewriting the code? The existing code works the way I need it to for the most part so I would prefer just tweaking it vs a more significant rewrite.

My question and my concern is, is your cell renderer using the JProgressBar held by the model? If so, how can it if there is no way for it to obtain the JProgressBar? If it's only using the Float value obtained, it simply makes no sense for the model to hold information and resources that are not used. And yes, this could slow your program down noticeably.

春庭雪 2024-11-22 04:17:23

为什么使用 AbstractTableModel,也不在此代码块内添加 JTable 值,实际上您不需要这样做,例如 DefalultTableModel 可以做到这一点

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;

public class ProgressBarTableCellRendererExample {

    private Random random = new Random();
    private JTable table;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ProgressBarTableCellRendererExample().makeUI();
            }
        });
    }

    public void makeUI() {
        table = new JTable(11, 1) {

            private Object[][] data;
            private static final long serialVersionUID = 1L;

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }

            @Override
            public Class<?> getColumnClass(int column) {
                return Integer.class;
            }
        };
        randomize();
        table.setDefaultRenderer(Integer.class, new ProgressBarTableCellRenderer());
        table.setRowHeight(20);
        table.setRowMargin(2);
        JButton button = new JButton("Randomize");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                randomize();
                getTableValue();
            }
        });
        JFrame frame = new JFrame();
        frame.add(new JScrollPane(table), BorderLayout.CENTER);
        frame.add(button, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(150, 150);
        frame.setPreferredSize(new Dimension(600, 300));
        frame.pack();
        frame.setVisible(true);
    }

    private void randomize() {
        for (int i = 0; i < table.getRowCount(); i++) {
            table.setValueAt(random.nextInt(100), i, 0);
        }
    }

    private void getTableValue() {
        for (int i = 0; i < table.getRowCount(); i++) {
            System.out.println(String.valueOf(table.getValueAt(i, 0)));
        }
    }

    private class ProgressBarTableCellRenderer extends JProgressBar implements TableCellRenderer {

        private static final long serialVersionUID = 1L;

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                boolean hasFocus, int row, int column) {
            setValue((Integer) value);
            System.out.println("Tables Row: " + row + ", Column : " + column + ", has value - " + value);
            return this;
        }
    }
}

why using AbstractTableModel, nor Adds JTable value inside this code block, really you don't need that, DefalultTableModel can do that, for example

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;

public class ProgressBarTableCellRendererExample {

    private Random random = new Random();
    private JTable table;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ProgressBarTableCellRendererExample().makeUI();
            }
        });
    }

    public void makeUI() {
        table = new JTable(11, 1) {

            private Object[][] data;
            private static final long serialVersionUID = 1L;

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }

            @Override
            public Class<?> getColumnClass(int column) {
                return Integer.class;
            }
        };
        randomize();
        table.setDefaultRenderer(Integer.class, new ProgressBarTableCellRenderer());
        table.setRowHeight(20);
        table.setRowMargin(2);
        JButton button = new JButton("Randomize");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                randomize();
                getTableValue();
            }
        });
        JFrame frame = new JFrame();
        frame.add(new JScrollPane(table), BorderLayout.CENTER);
        frame.add(button, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(150, 150);
        frame.setPreferredSize(new Dimension(600, 300));
        frame.pack();
        frame.setVisible(true);
    }

    private void randomize() {
        for (int i = 0; i < table.getRowCount(); i++) {
            table.setValueAt(random.nextInt(100), i, 0);
        }
    }

    private void getTableValue() {
        for (int i = 0; i < table.getRowCount(); i++) {
            System.out.println(String.valueOf(table.getValueAt(i, 0)));
        }
    }

    private class ProgressBarTableCellRenderer extends JProgressBar implements TableCellRenderer {

        private static final long serialVersionUID = 1L;

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                boolean hasFocus, int row, int column) {
            setValue((Integer) value);
            System.out.println("Tables Row: " + row + ", Column : " + column + ", has value - " + value);
            return this;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文