私は、コンクリートの列にイメージを追加し、jTableでrawを追加するというタスクがあります。私はsetCellRenderedで作成しようとしましたが、正しく動作しません。多分誰かが同じ仕事をしていました。 私はDefaultTableCellRendererを介して作成しようとしましたが、現在、このレンダラーを具体的なセルではなく、列ではなく、どのように呼び出すことができないのですか?[3] [4] public Frame1(){ initialize(); }JTable、MousClickによって具体的なセルに動的にlableを追加します。
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 364, 385);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
table = new JTable();
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent event) {
Point point = event.getPoint();
JTable target = (JTable)event.getSource();
int column = table.columnAtPoint(point);
int row = target.getSelectedRow();
table.getColumnModel().getColumn(column).setCellRenderer(new ImageRenderer());
//JOptionPane.showMessageDialog(table, "Column header #" + column + " is clicked");
}
});
table.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, Color.DARK_GRAY, Color.LIGHT_GRAY, SystemColor.menu, null));
table.setModel(new DefaultTableModel(
new Object[][] {
{null, "", null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
},
new String[] {
"New column", "New column", "New column", "New column", "New column", "New column"
}
));
table.getColumnModel().getColumn(0).setMinWidth(50);
table.setBounds(0, 0, 350, 350);
table.setRowHeight(50);
frame.getContentPane().add(table);
}
}
class ImageRenderer extends DefaultTableCellRenderer {
JLabel lbl = new JLabel();
ImageIcon imageIcon = new ImageIcon(new ImageIcon("green-glossy-ball.jpg").getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT));
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
lbl.setIcon(imageIcon);
return lbl;
}
}
私は、このセルは画像を表示する必要がありますが、私は理解していたが、別のセルを選択した後に画像を表示する必要がありますか? –
はい、私はシンプルプレイを "Connect Four"にする必要があります。私が列をクリックすると、ボールは最初の空きセルまで落ちなければなりません。今、私はテーブルとイベントを扱う方法を理解しようとしています。 – Lena