java 表格中带按钮的方法

 

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

import NETSOURCE.SearchPanel;
import NETSOURCE.SearchPanel.ButtonEditor;
import NETSOURCE.SearchPanel.ButtonRenderer;

public class ManageStoredData extends JPanel{
 private DefaultTableModel tableModel;
 private JTable table;
 private final int columnWithoutButton = 5;
 public ManageStoredData() {
  setLayout(null); 
  this.setBackground(Color.pink);
  final JScrollPane scrollPane = new JScrollPane();
  scrollPane.setAutoscrolls(true);
  tableModel = new DefaultTableModel() {
   // 设置列能否编辑
   public boolean isCellEditable(int row, int column) {
    if (column == 4 || column == 5 )
     return true;
    return false;
   }
  };
  String headName[] = { "序号", "文件名", "文件大小", "修改日期",
    "导出", "删除"};
  tableModel.setColumnIdentifiers(headName);
  table = new JTable(tableModel);
  table.getColumn("导出").setCellRenderer(new ButtonRenderer());
  table.getColumn("导出").setCellEditor(new ButtonEditor(new JCheckBox()));
  table.getColumn("删除").setCellRenderer(new ButtonRenderer());
  table.getColumn("删除").setCellEditor(new ButtonEditor(new JCheckBox()));
  // 设置列标题不能移动
//  table.getTableHeader().setReorderingAllowed(false);
  table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

  scrollPane.setViewportView(table);

  // 设置列宽,并且当调整Panel时列宽也能相应的变化
  table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
//  table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);  //若设置它,列宽永远固定
  table.getColumnModel().getColumn(0).setPreferredWidth(30);
  table.getColumnModel().getColumn(1).setPreferredWidth(120);
  table.getColumnModel().getColumn(2).setPreferredWidth(80);
  table.getColumnModel().getColumn(3).setPreferredWidth(80);
  table.getColumnModel().getColumn(4).setPreferredWidth(70);
  table.getColumnModel().getColumn(5).setPreferredWidth(70);
  // 设置行高
  table.setRowHeight(25);  
  scrollPane.setBounds(80, 20, 500, 125);
  this.add(scrollPane);
  setVisible(true);  
  InitValue();
 }

 private void InitValue() {
  for(int i=0;i<5;i++){
   Vector vector=new Vector();
   vector.addElement("1");
   vector.addElement("Open Close Msg");
   vector.addElement("do");
   vector.addElement("2");
   vector.addElement("导出"); 
   vector.addElement("删除"); 
   tableModel.getDataVector().add(vector);
  }
 }

 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(table.getSelectionBackground());
   } else {
      setForeground(table.getForeground());
    setBackground(UIManager.getColor("Button.background"));
   }
   setText((value == null) ? "" : value.toString());
   return this;
  }

 }

 public class ButtonEditor extends DefaultCellEditor {
  protected JButton button;

  public ButtonEditor(JCheckBox checkBox) {
   super(checkBox);
   button = new JButton();
   button.setOpaque(true);
   button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent arg0) {
     ButtonClick();
    }
   });
  }

  @Override
  public Component getTableCellEditorComponent(JTable table1,
    Object value, boolean isSelected, int row, int column) {
   if (isSelected) {
    System.out.println("change column");
    button.setForeground(table.getSelectionForeground());
    button.setBackground(table.getSelectionBackground());
   } else {
    System.out.println("change row");
    button.setForeground(table.getForeground());
    button.setBackground(table.getBackground());
   }

   button.setText(table1.getValueAt(row, column).toString());
   return button;

  }

  //若缺少此方法,点击按钮后将获取不到Button的Text值,将会显示false.
  @Override
  public Object getCellEditorValue(){
   return button.getText();
  }
  
  protected void ButtonClick() {
   // TODO Auto-generated method stub
   System.out.println("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");

   System.out.println(table.getSelectedColumn() + " and  "
     + table.getSelectedRow());
  }
 }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 AbstractTableModel 构建Table表格添加JButton按钮,之前在网上找了2天没有找到好用的程序,最终终于找到一个好用的例子。 不要使,我退你们分。。 sing the Swing JTable class can quickly become a sticky business when you want to customize it to your specific needs. First you must become familiar with how the JTable class is organized. Individual cells are rendered by TableCellRenderer implementations. The table contents are represented by an implementation of the TableModel interface. By default, JTable uses DefaultTableCellRenderer to draw its cells. DefaultTableCellRenderer recognizes a few primitive types, rendering them as strings, and can even display Boolean types as checkboxes. But it defaults to displaying the value returned by toString() for types it does not specifically handle. You have to provide your own TableCellRenderer implementation if you want to display buttons in a JTable. The TableCellRenderer interface contains only one method, getTableCellRendererComponent(...), which returns a java.awt.Component that knows how to draw the contents of a specific cell. Usually, getTableCellRendererComponent() will return the same component for every cell of a column, to avoid the unnecessary use of extra memory. But when the contents of a cell is itself a component, it is all right to return that component as the renderer. Therefore, the first step towards having JButtons display correctly in a JTable is to create a TableCellRenderer implementation that returns the JButton contained in the cell being rendered. In the accompanying code listing, JTableButtonRenderer demonstrates how to do this. Even after creating a custom TableCellRenderer, you're still not done. The TableModel associated with a given JTable does not only keep track of the contents of each cell, but it also keeps track of the class of data stored in each column. DefaultTableModel is designed to work with DefaultTableCellRenderer and will return java.lang.String.class for columns containing data types that it does not specifically handle. The exact method that does this is getColumnClass(int column). Your second step is to create a TableModel implementation that returns JButton.class for cells that contain JButtons. JTableButtonModel shows one way to do this. It just returns the result of getClass() for each piece of cell data. At this point, you're almost done, but not quite. What's the use of putting a JButton in a JTable if you can't press the darn thing? By default, JTable will not forward mouse events to components contained in its cells. If you want to be able to press the buttons you add to JTable, you have to create your own MouseListener that forwards events to the JButton cells. JTableButtonMouseListener demonstrates how you could do this.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值