栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java Swing,尝试用图像图标复选框替换JTable中的布尔复选框

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java Swing,尝试用图像图标复选框替换JTable中的布尔复选框

您需要提供自己的自定义TableCellRenderer功能,该功能能够提供所需的功能…

import java.awt.BorderLayout;import java.awt.Component;import java.awt.Dimension;import java.awt.EventQueue;import java.io.IOException;import java.util.logging.Level;import java.util.logging.Logger;import javax.imageio.ImageIO;import javax.swing.ImageIcon;import javax.swing.JCheckBox;import javax.swing.Jframe;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException;import javax.swing.table.AbstractTableModel;import javax.swing.table.TableCellRenderer;import javax.swing.table.TableModel;public class TestTableRenderer {    public static void main(String[] args) {        new TestTableRenderer();    }    public TestTableRenderer() {        EventQueue.invokeLater(new Runnable() { @Override public void run() {     try {         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());     } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {     }     Jframe frame = new Jframe("Testing");     frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);     frame.setLayout(new BorderLayout());     frame.add(new TestPane());     frame.pack();     frame.setLocationRelativeTo(null);     frame.setVisible(true); }        });    }    public class TestPane extends JPanel {        public TestPane() { TableModel model = new AbstractTableModel() {     @Override     public int getRowCount() {         return 2;     }     @Override     public int getColumnCount() {         return 1;     }     @Override     public Object getValueAt(int rowIndex, int columnIndex) {         return rowIndex == 0 ? true : false;     }     @Override     public Class<?> getColumnClass(int columnIndex) {         return Boolean.class;     } }; JTable table = new JTable(model); table.setDefaultRenderer(Boolean.class, new CustomBooleanCellRenderer()); setLayout(new BorderLayout()); add(new JScrollPane(table));        }        @Override        public Dimension getPreferredSize() { return new Dimension(200, 200);        }    }    public class CustomBooleanCellRenderer extends JCheckBox implements TableCellRenderer {        private ImageIcon sad;        private ImageIcon happy;        public CustomBooleanCellRenderer() { try {     happy = new ImageIcon(ImageIO.read(getClass().getResource("/Happy.png")));     sad = new ImageIcon(ImageIO.read(getClass().getResource("/Sad.png"))); } catch (IOException ex) {     Logger.getLogger(TestTableRenderer.class.getName()).log(Level.SEVERE, null, ex); }        }        @Override        public void setSelected(boolean selected) { super.setSelected(selected);  if (selected) {     setIcon(happy); } else {     setIcon(sad); }        }        @Override        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (value instanceof Boolean) {     boolean selected = (boolean) value;     setSelected(selected); } return this;        }    }}

现在,您可以轻松地使用aJLabel或DefaultTableCellRenderer,
检查/的Object值并相应地设置图标……但 这样做的乐趣何在?)truefalse

已更新,包括编辑器…

我已经稍微重新排列了代码,以包括单元格编辑器…

public class CustomCheckBox extends JCheckBox {    private ImageIcon sad;    private ImageIcon happy;    public CustomCheckBox() {        try { happy = new ImageIcon(ImageIO.read(getClass().getResource("/Happy.png"))); sad = new ImageIcon(ImageIO.read(getClass().getResource("/Sad.png")));        } catch (IOException ex) { ex.printStackTrace();        }    }    @Override    public void setSelected(boolean selected) {        super.setSelected(selected);        if (selected) { setIcon(happy);        } else { setIcon(sad);        }    }}public class CustomBooleanCellRenderer extends CustomCheckBox implements TableCellRenderer {    @Override    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {        if (value instanceof Boolean) { boolean selected = (boolean) value; setSelected(selected);        }        return this;    }}public class CustomBooleanCellEditor extends AbstractCellEditor implements TableCellEditor {    private CustomCheckBox editor;    public CustomBooleanCellEditor() {        editor = new CustomCheckBox();    }    @Override    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {        if (value instanceof Boolean) { boolean selected = (boolean) value; editor.setSelected(selected);        }        return editor;    }    @Override    public Object getCellEditorValue() {        return editor.isSelected();    }}

You can apply the in a similar way you did the renderer…

table.setDefaultEditor(Boolean.class, new CustomBooleanCellEditor());


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/440650.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号