我试图将JLabel
放入我的Swing JTable
中,但我得到的显然是对象的名称:
这是我用来将JLabel
加载到我的表中的类:
/**
* Main Class.
*/
public class Test extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private MyTable tableDest;
private MyTable tableSource;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
* @throws Exception
*/
public Test() throws Exception {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JSplitPane splitPane = new JSplitPane();
splitPane.setResizeWeight(0.5);
contentPane.add(splitPane, BorderLayout.CENTER);
tableSource = new MyTable(true);
splitPane.setRightComponent(tableSource);
tableDest = new MyTable(false);
splitPane.setLeftComponent(tableDest);
TransferHandler transferHandler = new MyTransferHandler();
tableSource.setDragEnabled(true);
tableSource.setTransferHandler(transferHandler);
tableSource.setDropMode(DropMode.ON);
tableDest.setDragEnabled(true);
tableDest.setTransferHandler(transferHandler);
tableDest.setDropMode(DropMode.ON);
}
}
/**
* Class for The Table
*/
class MyTable extends JTable {
private static final long serialVersionUID = 1L;
public MyTable(boolean withData) throws Exception {
this( new MyTableModel(withData));
}
public MyTable(MyTableModel tableModel) {
super(tableModel);
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}
@Override
public MyTableModel getModel() {
return (MyTableModel)super.getModel();
}
}
/**
* Class for The JPanel import into The table
*/
class MyTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
private JLabel[] dataSource = new JLabel[16];
public MyTableModel(boolean fill) {
if(fill) {
for(int i = 0; i < dataSource.length; i++) {
dataSource[i] = new JLabel("<html>Text color: <font color='red'>red</font></html>");
}
}
}
@Override
public int getRowCount() {
return 4;
}
@Override
public int getColumnCount() {
return 4;
}
@Override
public Object getValueAt(int row, int col) {
int index = getIndex(row, col);
return dataSource[index];
}
@Override
public void setValueAt(Object aValue, int row, int col) {
int index = getIndex(row, col);
dataSource[index] = (JLabel)aValue;
}
@Override
public Class<?> getColumnClass(int col) {
return JLabel.class;
}
public void removeAt(int row, int col) {
int index = getIndex(row, col);
dataSource[index] = null;
fireTableCellUpdated(row, col);
}
private int getIndex(int row, int col) {
return row*4 + col;
}
}
/**
* Class for the drag'n drop Stuff
*/
class MyTransferHandler extends TransferHandler {
private static final long serialVersionUID = 1L;
@Override
public int getSourceActions(JComponent comp) {
return MOVE;
}
int selectedRow;
int selectedCol;
@Override
public Transferable createTransferable(JComponent comp) {
System.out.println("createTransferable");
MyTable table = (MyTable)comp;
selectedRow = table.getSelectedRow();
selectedCol = table.getSelectedColumn();
String text = (String) table.getValueAt(selectedRow, selectedCol);
System.out.println("DND init for: " + text);
return new StringSelection(text);
}
@Override
public void exportDone(JComponent comp, Transferable trans, int action) {
System.out.println("exportDone");
if (action == MOVE) {
MyTable table = (MyTable)comp;
table.getModel().removeAt(selectedRow, selectedCol);
}
}
@Override
public boolean canImport(TransferSupport support) {
//System.out.println("canImport");
return support.isDrop();
}
@Override
public boolean importData(TransferSupport support) {
System.out.println("importData");
if(canImport(support)) { //to prevent from paste's
DropLocation dl = support.getDropLocation();
Point dropPoint = dl.getDropPoint();
String data;
try {
data = (String)support.getTransferable().getTransferData(DataFlavor.stringFlavor);
System.out.println("DND received: " + data);
} catch (UnsupportedFlavorException | IOException e) {
return false;
}
MyTable table = (MyTable)support.getComponent();
int row = table.rowAtPoint(dropPoint);
int col = table.columnAtPoint(dropPoint);
MyTableModel model = table.getModel();
Object currentValue = model.getValueAt(row, col);
if(currentValue == null) { //if there's currently no value on that cell
model.setValueAt(data, row, col);
model.fireTableCellUpdated(row, col);
return true;
}
}
return false;
}
}
我做错了什么?它不应该显示JPanel
而不是此文字。
答案 0 :(得分:2)
我试图将JLabel放入我的Swing Table中,但我得到的只是对象的名称。
JLabel
是JTable
中单元格的默认组件。我假设您在默认值中添加了JLabel
,这导致其toString
方法被调用并显示在默认标签中。
在这里,我创建了一个新的JTable
并将您的String
放在所有单元格中:
public class Test extends JFrame {
static String data = "<html>Text color: <font color='red'>red</font></html>";
static int size = 4;
public Test() {
JTable table = new JTable(size, size);
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
table.getModel().setValueAt(data, i, j);
}
}
add(table);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String[] args) {
new Test();
}
}