如下圖所示,在表格單元格中添加標簽和按鈕:
?
?
代碼如下:
?
?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
?
import javax.swing.AbstractCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
?
import com.customcomponent.basicClass.BasicTableColumnModel;
?
import syspublicpanel.table.SysLoadTable;
import syspublicpanel.table.SysLoadTableModel;
import uiframework.tools.UIProperties;
?
public class TestTableRenderer extends JFrame {
??? private static final long serialVersionUID = 3941331829886452257L;
??? private SysLoadTable testTable = null ; // 表格
???
??? private static final String[] colNames = new String[] { " 第一列 " , " 第二列 " , " 第三列 " }; // 表頭
???
??? public TestTableRenderer() {
?????? initialize();
??? }
???
??? /**
??? ? * 初始化組件
??? ? */
??? private void initialize() {
?????? this .setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE );
?????? this .setSize(400, 300);
?????? this .setLocation(200, 100);
?????? this .setLayout( new BorderLayout());
?????? this .add( new JScrollPane(getTestTable()), BorderLayout. CENTER );
??????
?????? this .setVisible( true );
??? }
???
??? private SysLoadTable getTestTable() {
?????? if ( testTable == null ) {
?????????? SysLoadTableModel tableModel = new SysLoadTableModel( colNames );
?????????? tableModel.setAllEditableColumn( false ); // 設置所有列不可編輯
?????????? tableModel.setColumnEditable( " 第三列 " , true ); // 設置第三列可編輯
??????????
?????????? /**
?????????? ? * 添加表格內容
?????????? ? */
?????????? tableModel.addRow( new Object[] { "1" , "2" , null });
?????????? tableModel.addRow( new Object[] { "11" , "22" , null });
?????????? tableModel.addRow( new Object[] { "111" , "222" , null });
??????????
?????????? testTable = new SysLoadTable(tableModel);
??????????
?????????? BasicTableColumnModel columnModel = (BasicTableColumnModel) testTable .getColumnModel();
?????????? columnModel.getColumn(2).setCellRenderer( new CustomTableRenderer());
?????????? columnModel.getColumn(2).setCellEditor( new CustomTableRenderer());
?????? }
?????? return testTable ;
??? }
?
??? public static void main(String[] args) {
?????? try {
?????????? UIManager. setLookAndFeel (UIProperties. getInstance ().getLookAndFeel());
?????? } catch (ClassNotFoundException e) {
?????????? e.printStackTrace();
?????? } catch (InstantiationException e) {
?????????? e.printStackTrace();
?????? } catch (IllegalAccessException e) {
?????????? e.printStackTrace();
?????? } catch (UnsupportedLookAndFeelException e) {
?????????? e.printStackTrace();
?????? }
??????
?????? new TestTableRenderer();
??? }
}
?
/**
? * 自定義表格渲染器
? */
class CustomTableRenderer extends AbstractCellEditor implements TableCellRenderer, TableCellEditor {
??? private static final long serialVersionUID = 7380564532198049597L;
??? private JPanel panel = null ; // 面板
??? private JLabel delLabel = null ; // 刪除標簽
??? private JButton modifyButton = null ; // 修改按鈕
???
??? private static final String imagePath = "/sysimages/" ; // 圖片路徑
???
??? public CustomTableRenderer() {
?????? delLabel = new JLabel( new ImageIcon(getClass().getResource( imagePath + "delete.gif" )));
?????? delLabel .setCursor( new Cursor(Cursor. HAND_CURSOR ));
?????? delLabel .setToolTipText( " 刪除 " );
??????
?????? modifyButton = new JButton( " 修改 " );
?????? modifyButton .setPreferredSize( new Dimension(60, 20));
??????
?????? panel = new JPanel();
?????? panel .setBackground(Color. WHITE );
?????? panel .setLayout( new FlowLayout(FlowLayout. CENTER , 20, 0));
?????? panel .add( delLabel );
?????? panel .add( modifyButton );
??? }
???
??? public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
?????? if (row == table.getSelectedRow()) {
?????????? panel .setBackground(table.getSelectionBackground());
?????? } else {
?????????? panel .setBackground(Color. WHITE );
?????? }
?????? return panel ;
??? }
??? ???
?
??? public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
?????????????? if (row == table.getSelectedRow()) {
? ?????????? panel .setBackground(table.getSelectionBackground());
?????? } else {
?????????? panel .setBackground(Color. WHITE );
?????? }
?????? return panel ;
??? }
?
??? public Object getCellEditorValue() {
?????? return null ;
??? }
}
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
