Swing开发中在JTable单元格放JPanel组件
我想做一个单列表格。每一个单元格内容为一个JPanel对象。JPanel对象里面有按钮之类的组件。需要可以对这个单元格中的JPanel进行操作!希望高手能给予帮助。内容符...
我想做一个单列表格。每一个单元格内容为一个JPanel对象。JPanel对象里面有按钮之类的组件。需要可以对这个单元格中的JPanel进行操作!
希望高手能给予帮助。内容符合必定高分相赠!
谢谢!若这里不方便 可加本人QQ:339726288。加时注明~谢谢!!!
类似的图片。 展开
希望高手能给予帮助。内容符合必定高分相赠!
谢谢!若这里不方便 可加本人QQ:339726288。加时注明~谢谢!!!
类似的图片。 展开
展开全部
//首先给QQ:7706189
//编辑器,我在旦困JPanel上 了JTextField 和JButton
public class TextCellEditor extends javax.swing.AbstractCellEditor implements
javax.swing.table.TableCellEditor {
private JDialog parent;
private JPanel editPanel;
private JTextField editText;
private JButton editButton;
public TextCellEditor(JDialog d){
this.parent=d;
editPanel = new javax.swing.JPanel();
editButton = new javax.swing.JButton();
editText = new javax.swing.JTextField();
editButton.setText("*");
editButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new GlossarySortDialog(parent,editText).setVisible(true);
}
});
editPanel.setLayout(new BorderLayout());
editPanel.add(editText,BorderLayout.CENTER);
editPanel.add(editButton,BorderLayout.EAST);
}
public Object getCellEditorValue() {
return editText.getText();
}
//重点:这个方法返回JPanel编辑器(包括JPanel上的其他控件)
public java.awt.Component getTableCellEditorComponent(
javax.swing.JTable table, Object value, boolean isSelected,
int row, int column) {
if (value == null)
editText.setText("");
else
editText.setText(value.toString());
//返回JPanel编辑器模睁念
return editPanel;
}
}
//=============
//表格的单元格用到了
public class TempColligateTicketDialog extends javax.swing.JDialog {
private javax.swing.JTable tableColligate;
private DefaultTableModel dTableModel;
private TextCellEditor textCell; //早悉你要用的JPanel编辑器
....
....
/**
* 初始化表格
* @param strSql
*/
private void initTable() {
Object[][] body = null;
String[] head = null;
head = new String[]{"序号", "注意事项"};
dTableModel = new DefaultTableModel(body, head) {
boolean[] canEdit = new boolean[]{
false, true
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit[columnIndex];
}
};
List list = SystemConstants.getOperationCardContent();
CardOperationStep cos = null;
if (list != null) {
for (int i = 0; i < list.size(); i++) {
cos = (CardOperationStep) list.get(i);
Object[] rowData = {i+1, cos.operationCont};
dTableModel.addRow(rowData);
}
}
tableColligate.setModel(dTableModel);
tableColligate.getColumnModel().getColumn(0).setMaxWidth(50);
//在表格的第2列加JPanel编辑器
tableColligate.getColumnModel().getColumn(1).setCellEditor(textCell);
}
....
.....
}
//编辑器,我在旦困JPanel上 了JTextField 和JButton
public class TextCellEditor extends javax.swing.AbstractCellEditor implements
javax.swing.table.TableCellEditor {
private JDialog parent;
private JPanel editPanel;
private JTextField editText;
private JButton editButton;
public TextCellEditor(JDialog d){
this.parent=d;
editPanel = new javax.swing.JPanel();
editButton = new javax.swing.JButton();
editText = new javax.swing.JTextField();
editButton.setText("*");
editButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new GlossarySortDialog(parent,editText).setVisible(true);
}
});
editPanel.setLayout(new BorderLayout());
editPanel.add(editText,BorderLayout.CENTER);
editPanel.add(editButton,BorderLayout.EAST);
}
public Object getCellEditorValue() {
return editText.getText();
}
//重点:这个方法返回JPanel编辑器(包括JPanel上的其他控件)
public java.awt.Component getTableCellEditorComponent(
javax.swing.JTable table, Object value, boolean isSelected,
int row, int column) {
if (value == null)
editText.setText("");
else
editText.setText(value.toString());
//返回JPanel编辑器模睁念
return editPanel;
}
}
//=============
//表格的单元格用到了
public class TempColligateTicketDialog extends javax.swing.JDialog {
private javax.swing.JTable tableColligate;
private DefaultTableModel dTableModel;
private TextCellEditor textCell; //早悉你要用的JPanel编辑器
....
....
/**
* 初始化表格
* @param strSql
*/
private void initTable() {
Object[][] body = null;
String[] head = null;
head = new String[]{"序号", "注意事项"};
dTableModel = new DefaultTableModel(body, head) {
boolean[] canEdit = new boolean[]{
false, true
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit[columnIndex];
}
};
List list = SystemConstants.getOperationCardContent();
CardOperationStep cos = null;
if (list != null) {
for (int i = 0; i < list.size(); i++) {
cos = (CardOperationStep) list.get(i);
Object[] rowData = {i+1, cos.operationCont};
dTableModel.addRow(rowData);
}
}
tableColligate.setModel(dTableModel);
tableColligate.getColumnModel().getColumn(0).setMaxWidth(50);
//在表格的第2列加JPanel编辑器
tableColligate.getColumnModel().getColumn(1).setCellEditor(textCell);
}
....
.....
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询