我的系统有jtable,我需要把jbuttons放在桌子上。我尝试在互联网上搜索,但这是我理解的唯一代码一点点。这是我找到代码JButton in JTable Cell: How to add, assign or fill up JTable’s Cell with JButton and then add ActionListener to enable Click event for that JButtons in JTable cell.
的网站这是代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.TableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.*;
class ButtonInJTable extends JFrame{
private JPanel topPanel,topPanel1;
private JTable table;
private JScrollPane scrollPane,scrollPane1;
private String[] columnNames= new String[3];
private String[][] dataValues=new String[3][3] ;
JButton button = new JButton();
public ButtonInJTable(){
setTitle("Button in JTable Cell");
setSize(300,300);
topPanel= new JPanel();
topPanel.setLayout(new BorderLayout());
getContentPane().add(topPanel);
columnNames=new String[] {"Column 1" , "Column 2" , "Column 3"};
dataValues = new String[][] {{"1","2",}, {"4","5",}, {"7","8",}};
TableModel model=new myTableModel("owntable");
table =new JTable( );
table.setModel(model);
table.getColumn("Column 3").setCellRenderer(new ButtonRenderer());
table.getColumn("Column 3").setCellEditor(new ButtonEditor(new JCheckBox()));
scrollPane=new JScrollPane(table);
topPanel.add(scrollPane,BorderLayout.CENTER);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
JOptionPane.showMessageDialog(null,"Button Clicked in JTable Cell");
}
});
}
class ButtonRenderer extends JButton implements TableCellRenderer {
public ButtonRenderer() {
//setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setText((value == null) ? "" : value.toString());
return this;
}
}
class ButtonEditor extends DefaultCellEditor {
private String label;
public ButtonEditor(JCheckBox checkBox) {
super(checkBox);
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
label = (value == null) ? "" : value.toString();
button.setText(label);
return button;
}
public Object getCellEditorValue() {
return new String(label);
}
}
public class myTableModel extends DefaultTableModel {
String dat;
JButton button=new JButton("");
myTableModel(String tname){
super(dataValues,columnNames);
dat=tname;
}
public boolean isCellEditable(int row,int cols){
if( dat=="owntable"){
if(cols==0){return false;}
}
return true;
}
}
public static void main(String args[]){
ButtonInJTable mainFrame=new ButtonInJTable();
mainFrame.setVisible(true);
}
}
我仍然完全不理解这段代码所以我只想问几个问题:
这是什么意思?
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setText((value == null) ? "" : value.toString());
return this;
}
和此:
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
label = (value == null) ? "" : value.toString();
button.setText(label);
return button;
}
为什么要有checkbox
?
public ButtonEditor(JCheckBox checkBox) {
super(checkBox);
}
根据上面的代码,我如何为不同的按钮单独actionlistener
?
谢谢你的任何答案:)