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

JTable单元格渲染器

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

JTable单元格渲染器

您的渲染器曾经使用过吗?您将其设置为包含String的单元格的默认渲染器,但是是否重载了模型的

getColumnClass
方法,以便它知道某些单元格包含String

因此,首先,我将使用println语句查看渲染器是否被调用,否则,我将覆盖上面提到的模型方法。

编辑1
同样,您的if结果注定是奇怪的。在if部分中,您更改了前景;在else部分中,您更改了背景-
没有任何意义。您可能应该在if与else块中进行互补的状态变化,而不是正交变化。

编辑2
例如:

import java.awt.*;import java.util.Random;import javax.swing.*;import javax.swing.table.*;public class Board extends JPanel {   private static final long serialVersionUID = 1L;   int boardHeight = 20;   int boardWidth = 10;   JTable table;   Random random = new Random();   public Board() {      setLayout(new BorderLayout()); // !!      DefaultTableModel model = new DefaultTableModel(boardHeight, boardWidth) {         @Override         public Class<?> getColumnClass(int columnIndex) { return String.class;         }      };      // !! table = new JTable(this.boardHeight, this.boardWidth);      table = new JTable(model);      for (int row = 0; row < model.getRowCount(); row++) {         for (int col = 0; col < model.getColumnCount(); col++) { String s = random.nextBoolean() ? "red" : "yellow"; model.setValueAt(s, row, col);         }      }      table.setDefaultRenderer(String.class, new BoardTableCellRenderer());      table.setFocusable(false);      table.setShowGrid(false);      table.setRowMargin(0);      table.setIntercellSpacing(new Dimension(0, 0));      table.setRowSelectionAllowed(false);      table.setVisible(true);      this.add(table);      this.setPreferredSize(new Dimension(table.getPreferredSize().width,    (table.getPreferredSize().height + 85)));   }   private static void createAndShowUI() {      Jframe frame = new Jframe("Board");      frame.getContentPane().add(new Board());      frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);      frame.pack();      frame.setLocationRelativeTo(null);      frame.setVisible(true);   }   public static void main(String[] args) {      java.awt.EventQueue.invokeLater(new Runnable() {         public void run() { createAndShowUI();         }      });   }}class BoardTableCellRenderer extends DefaultTableCellRenderer {   private static final long serialVersionUID = 1L;   public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {      Component c = super.getTableCellRendererComponent(table, value,    isSelected, hasFocus, row, col);      Object valueAt = table.getModel().getValueAt(row, col);      String s = "";      if (valueAt != null) {         s = valueAt.toString();      }      if (s.equalsIgnoreCase("yellow")) {         c.setForeground(Color.YELLOW);         c.setBackground(Color.gray);      } else {         c.setForeground(Color.black);         c.setBackground(Color.WHITE);      }      return c;   }}


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

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

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