⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tableshow.java

📁 JTable jdk 1.4.2 kai fa de
💻 JAVA
字号:
package table;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumnModel;

public class TableShow extends JPanel implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = -4591232811482593809L;
	JTable tbTab;
	JButton btAdd;
	JButton btRemove;
	JButton btAllCheck;
	JButton btAllUnCheck;
	JButton btUp;
	JButton btDown;
	SortManager sm;
	Dimension btsize=new Dimension(50,50);
	MyTableModel tableModel=new MyTableModel();
	public TableShow()
	{
		loadData();
		createUI();
	}
	public void loadData()
	{
		Member m=new Member();
		m.setName("amos");
		m.setAge("20");
		m.setSex("men");
		m.setIsmember(true);
		m.setCurrentColor(Color.red);
		tableModel.addARow(m);
	}
	public void createUI()
	{
		GridBagLayout gbPanel0 = new GridBagLayout();
		GridBagConstraints gbcPanel0 = new GridBagConstraints();
		setLayout( gbPanel0 );
		tbTab = new JTable( tableModel );
		
		
		
        String []sex={"men","women","dog"};
        JComboBox combobox=new JComboBox(sex);
        JCheckBox checkbox=new JCheckBox();
        TableColumnModel cmodel=tbTab.getColumnModel();
		cmodel.getColumn(0).setPreferredWidth(100);
		cmodel.getColumn(1).setCellEditor(new DefaultCellEditor(combobox)); 
		cmodel.getColumn(2).setCellEditor(new DefaultCellEditor(checkbox));//if edit this cell ,show checkbox!it should be Render
		cmodel.getColumn(2).setCellRenderer(new CheckBoxCellRenderer());
		cmodel.getColumn(3).setPreferredWidth(50);
		cmodel.getColumn(4).setCellEditor(new ColorEditor());//when edit to use celleditor and show celleditor.
		cmodel.getColumn(4).setCellRenderer(new ColorRenderer(true));//when not editor to use Renderer and show Renderer.
		sm=new SortManager(tbTab);
		JScrollPane scpTab = new JScrollPane( tbTab );
		gbcPanel0.gridx = 0;
		gbcPanel0.gridy = 0;
		gbcPanel0.gridwidth = 19;
		gbcPanel0.gridheight = 20;
		gbcPanel0.fill = GridBagConstraints.BOTH;
		gbcPanel0.weightx = 1;
		gbcPanel0.weighty = 1;
		gbcPanel0.anchor = GridBagConstraints.WEST;
		gbPanel0.setConstraints( scpTab, gbcPanel0 );
		add( scpTab );

		btAdd = new JButton( ""  );
		btAdd.setSize(btsize);
		btAdd.addActionListener(this);
		btAdd.setIcon( createImageIcon("/images/add.gif"));
		gbcPanel0.gridx = 19;
		gbcPanel0.gridy = 0;
		gbcPanel0.gridwidth = 1;
		gbcPanel0.gridheight = 1;
		gbcPanel0.fill = GridBagConstraints.NONE;
		gbcPanel0.weightx = 1;
		gbcPanel0.weighty = 0;
		gbcPanel0.anchor = GridBagConstraints.WEST;
		gbPanel0.setConstraints( btAdd, gbcPanel0 );
		add( btAdd );

		btRemove = new JButton( ""  );
		btRemove.setSize(btsize);
		btRemove.addActionListener(this);
		btRemove.setIcon( createImageIcon("/images/remove.gif"));
		gbcPanel0.gridx = 19;
		gbcPanel0.gridy = 1;
		gbcPanel0.gridwidth = 1;
		gbcPanel0.gridheight = 1;
		gbcPanel0.fill = GridBagConstraints.NONE;
		gbcPanel0.weightx = 1;
		gbcPanel0.weighty = 0;
		gbcPanel0.anchor = GridBagConstraints.WEST;
		gbPanel0.setConstraints( btRemove, gbcPanel0 );
		add( btRemove );

		btAllCheck = new JButton( ""  );
		btAllCheck.setSize(btsize);
		btAllCheck.addActionListener(this);
		btAllCheck.setIcon( createImageIcon("/images/check.gif"));
		gbcPanel0.gridx = 19;
		gbcPanel0.gridy = 2;
		gbcPanel0.gridwidth = 1;
		gbcPanel0.gridheight = 1;
		gbcPanel0.fill = GridBagConstraints.NONE;
		gbcPanel0.weightx = 1;
		gbcPanel0.weighty = 0;
		gbcPanel0.anchor = GridBagConstraints.WEST;
		gbPanel0.setConstraints( btAllCheck, gbcPanel0 );
		add( btAllCheck );

		btAllUnCheck = new JButton( ""  );
		btAllUnCheck.setSize(btsize);
		btAllUnCheck.addActionListener(this);
		btAllUnCheck.setIcon( createImageIcon("/images/unCheck.gif") );
		gbcPanel0.gridx = 19;
		gbcPanel0.gridy = 3;
		gbcPanel0.gridwidth = 1;
		gbcPanel0.gridheight = 1;
		gbcPanel0.fill = GridBagConstraints.NONE;
		gbcPanel0.weightx = 1;
		gbcPanel0.weighty = 0;
		gbcPanel0.anchor = GridBagConstraints.WEST;
		gbPanel0.setConstraints( btAllUnCheck, gbcPanel0 );
		add( btAllUnCheck );

		btUp = new JButton( ""  );
		btUp.setSize(btsize);
		btUp.addActionListener(this);
		btUp.setIcon( createImageIcon("/images/up.gif"));
		gbcPanel0.gridx = 19;
		gbcPanel0.gridy = 4;
		gbcPanel0.gridwidth = 1;
		gbcPanel0.gridheight = 1;
		gbcPanel0.fill = GridBagConstraints.NONE;
		gbcPanel0.weightx = 1;
		gbcPanel0.weighty = 0;
		gbcPanel0.anchor = GridBagConstraints.WEST;
		gbPanel0.setConstraints( btUp, gbcPanel0 );
		add( btUp );

		btDown = new JButton( ""  );
		btDown.setSize(btsize);
		btDown.addActionListener(this);
		btDown.setIcon( createImageIcon("/images/down.gif"));
		gbcPanel0.gridx = 19;
		gbcPanel0.gridy = 5;
		gbcPanel0.gridwidth = 1;
		gbcPanel0.gridheight = 1;
		gbcPanel0.fill = GridBagConstraints.NONE;
		gbcPanel0.weightx = 1;
		gbcPanel0.weighty = 0;
		gbcPanel0.anchor = GridBagConstraints.WEST;
		gbPanel0.setConstraints( btDown, gbcPanel0 );
		add( btDown );
	}
	protected static ImageIcon createImageIcon(String path) {
		java.net.URL imgURL = TableShow.class.getResource(path);
		if (imgURL != null) {
			return new ImageIcon(imgURL);
		} else {
			System.err.println("Couldn't find file: " + path);
			return null;
		}
      }
	public static void main(String []args)
	{
		JFrame f=new JFrame("Table");
		f.getContentPane().setLayout(new BorderLayout());
		TableShow ts=new TableShow();
		f.getContentPane().add(ts);
		f.setSize(new Dimension(100,100));
		f.pack();
		f.setVisible(true);
	}
	public void actionPerformed(ActionEvent event) {
	       
	       if(event.getSource()==btAdd)
	       {
	   		Member m=new Member();
			m.setName("amos");
			m.setAge("20");
			m.setSex("men");
			m.setIsmember(false);
	        m.setCurrentColor(Color.red);
			tableModel.addARow(m);
	       }
	       if(event.getSource()==btRemove)
	       {
	    	   tableModel.removeARow(tbTab.getSelectedRow());
	       }
	       if(event.getSource()==btAllCheck)
	       {
	    	   System.out.println("btAllCheck");
	    	   tableModel.isMemberAll();
	       }
	       if(event.getSource()==btAllUnCheck)
	       {
	    	   System.out.println("btAllUnCheck");
	    	   tableModel.isNotMemberAll();
	       }
	       if(event.getSource()==btUp)
	       {
	    	   tableModel.up(tbTab.getSelectedRow());
	       }
	       if(event.getSource()==btDown)
	       {
	    	   tableModel.down(tbTab.getSelectedRow());
	       }
		}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -