gpt4 book ai didi

java - JTable 中的 JComboBox 中的图像在选择后仅显示路径名

转载 作者:行者123 更新时间:2023-11-30 03:02:29 29 4
gpt4 key购买 nike

我想要一个包含 jTable 中图像的组合框。但是,虽然我可以在下拉菜单中看到图像,但一旦选择,我只能在表中看到它们的路径名作为字符串。有人能指出我正确的方向吗?

/* Modified from http://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#TableRenderDemo */

/*
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* -Redistribution of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* -Redistribution in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
* AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
* AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
* REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
* INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
* OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed, licensed or intended
* for use in the design, construction, operation or maintenance of any
* nuclear facility.
*/

package tablerenderdemo;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.ImageIcon;

public class TableRenderDemo extends JPanel {
public TableRenderDemo() {
super(new GridLayout(1,0));

JTable table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
setUpSportColumn(table, table.getColumnModel().getColumn(1));
add(scrollPane);
}

public void setUpSportColumn(JTable table, TableColumn symbolColumn) {
JComboBox comboBox = new JComboBox();
comboBox.addItem( new ImageIcon("symbols/circle.png") );
comboBox.addItem( new ImageIcon("symbols/diamond.png") );
symbolColumn.setCellEditor(new DefaultCellEditor(comboBox));

DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
symbolColumn.setCellRenderer(renderer);
}

class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"Last Name","Symbol","# of Years","Vegetarian"};
private Object[][] data = {
{"Smith",
new ImageIcon("symbols/circle.png"), new Integer(5), new Boolean(false)},
{"Doe",
new ImageIcon("symbols/circle.png"), new Integer(3), new Boolean(true)}
};

public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
if (col < 1) {
return false;
} else {
return true;
}
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
}

private static void createAndShowGUI() {
JFrame frame = new JFrame("TableRenderDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

TableRenderDemo newContentPane = new TableRenderDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);

frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

最佳答案

您已替换了表的 default renderer ,它知道如何使用 DefaultTableCellRenderer 的实例渲染 Icon ,但事实并非如此。后者是一个 JLabel,它仅使用图标的 toString() 方法调用 setText()。删除这些行即可查看效果。

DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
symbolColumn.setCellRenderer(renderer);

关于java - JTable 中的 JComboBox 中的图像在选择后仅显示路径名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35615858/

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