gpt4 book ai didi

java - 在 JTable 中显示 JLabel

转载 作者:行者123 更新时间:2023-11-29 07:52:24 24 4
gpt4 key购买 nike

我正在尝试通过覆盖 getColumnClass 方法在 JTable 中显示 JLabel

        JLabel myLabel = new JLabel("ok");
String[] columnNames = {"","",""};
Object[][] data =
{
{myLabel, myLabel, myLabel},
{myLabel, myLabel, myLabel},
{myLabel, myLabel, myLabel},
};


DefaultTableModel model = new DefaultTableModel(data, columnNames);
table = new JTable( model )
{
public Class getColumnClass(int column)
{
return JLabel.class;
}
};

但它会显示类似这样的内容(哈希码?)

javax.swing.JLabel[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=ok,verticalAlignment=CENTER,verticalTextPosition=CENTER]

问题是什么,我该如何解决?

编辑

如果使用相同的逻辑来显示 ImageIcon,代码工作正常

        ImageIcon myLabel = new ImageIcon(path);  // ignore the variable name

并更改为 getColumnClass

            public Class getColumnClass(int column)
{
return ImageIcon.class;
}

最佳答案

.

- Boolean — rendered with a check box.
- Number — rendered by a right-aligned label.
- Double, Float — same as Number, but the object-to-text translation is performed
by a NumberFormat instance (using the default number format for the current locale).
- Date — rendered by a label, with the object-to-text translation performed
by a DateFormat instance (using a short style for the date and time).
- ImageIcon, Icon — rendered by a centered label.
- Object — rendered by a label that displays the object's string value.
  • Renderer 默认返回 JLabel/JComponent

  • 阅读 Oracle 教程 How to use Tables用于工作代码示例

关于java - 在 JTable 中显示 JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20139633/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com