如何在JTable的一个column里添加button?

chalsy 2002-05-15 10:51:00
我想在JTable 的一个column里添加button,以提供点击事件而弹出一个popup画面,
但是好像在column李不能添加button.
请问有没有什么替换的方法?
...全文
505 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Doctor11971 2002-05-27
  • 打赏
  • 举报
回复
首先告诉你,在column里可以加button,但是我看你的目的是弹出一个popup画面.
所以根本用不着那么麻烦.这样就可以了.
public class t extends JTable{
public t(){
addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e)
{
if(!(e.getModifiers()==e.META_MASK)){//clicked mouse left
JPopupMenu l= new JPopupMenu();
l.show(参数);
}
if (e.getModifiers()==e.META_MASK){//clicked mouse right
JPopupMenu r= new JPopupMenu();
r.show(参数);
}
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
});
}
}
}
alanfan 2002-05-27
  • 打赏
  • 举报
回复
you should only add a new column with setting its picklist property
turbochen 2002-05-15
  • 打赏
  • 举报
回复
参考:
http://www2.gol.com/users/tame/
soldier1002 2002-05-15
  • 打赏
  • 举报
回复
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
/** * @version 1.0 11/09/98 */
public 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) {
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(new Color(184, 208, 200));
setBackground(table.getSelectionBackground());
} else{
setForeground(table.getForeground());
setBackground(new Color(217, 236, 210));
//setBackground(UIManager.getColor("Button.background"));
}
setText( (value ==null) ? "" : value.toString() );
this.setToolTipText("Perform forecasting for this period only!");
return this;
}
}
soldier1002 2002-05-15
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
/** * @version 1.0 11/09/98 */
public class ButtonEditor extends DefaultCellEditor {
protected JButton button;
private String label;
private boolean isPushed;
private int rowcount;

public ButtonEditor(JCheckBox checkBox) {
super(checkBox);
button = new JButton();
button.setOpaque(true);

button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fireEditingStopped();}});
}

public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
rowcount = row;
if (isSelected) {
button.setForeground(table.getSelectionForeground());
button.setBackground(new Color(182, 208, 200));
//button.setBackground(table.getSelectionBackground());
} else{
button.setForeground(table.getForeground());
//button.setBackground(new Color(184, 208, 200));
//button.setBackground(new Color(212, 208, 200));
button.setBackground(table.getBackground());
}
label = (value ==null) ? "" : value.toString();
button.setText( label );
isPushed = true;
return button;
}

public Object getCellEditorValue() {
if (isPushed) {
//
//JOptionPane.showMessageDialog(button ,label + ": tOuch!");
// System.out.println(label + ": Ouch!");
}
isPushed = false;
return new String( label ) ;
}

public boolean stopCellEditing() {
isPushed = false;
return super.stopCellEditing();
}

protected void fireEditingStopped() {
super.fireEditingStopped();
}
}
soldier1002 2002-05-15
  • 打赏
  • 举报
回复
jTable1.getColumn("Con1").setCellRenderer(new ButtonRenderer());
jTable1.getColumn("Con1").setCellEditor(new ButtonEditor(new JCheckBox()));

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧