Swing中表格(JTable)的详细使用

我们在使用表格的时候一般都是希望用它来进行数据的展示的,但是表格不仅仅可以用来进行显示数据,还可以在表格中对数据进行操作,或者在表格中添加一些其他的控件来进行一些操作

基本使用:

 
public static void main(String[] args) {
		/*
		 * 表格的数据是存储在DefaultTableModel里面的
		 */
		Object[] columns={"字段一","字段二","字段三","字段四"};//字段
		Object[][] data=null;//需要展示的数据,一般是二维数组
		DefaultTableModel model=new DefaultTableModel(data, columns);
		JTable table=new JTable(model);
	}

进阶使用:(对表格进行渲染,进行编辑,表格中添加控件)

表格中的显示和编辑是由DefaultTableCellRenderer和TableCellEditor这两个类分别来控制,根据需要可以重写这两个类,下面这个例子实现了基本表格中常用的需求。

1:重写DefaultTableCellRenderer类

package JTableDemo;
import java.awt.Color;
import java.awt.Component;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

public class MyTableReander extends DefaultTableCellRenderer{
	
	private static final long serialVersionUID = 1L;

	/*对表格进行渲染的时候单元格默认返回的是JLabel,可以根据需要返回不同的控件*/
	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean arg3, int row, int column) {
		// TODO Auto-generated method stub
		if (column==0) {
			JCheckBox box=new JCheckBox();
			box.setOpaque(true);
			setHorizontalAlignment(box.CENTER);
			box.setHorizontalAlignment(JCheckBox.CENTER);
			if (isSelected) {//点击表格的时候改变点击的行的背景色
				box.setBackground(new Color(135, 206, 250));
			} else {
				if (row % 2 == 0) {
					box.setBackground(new Color(240, 250, 250));
					box.setForeground(table.getForeground());
				} else {
					box.setBackground(table.getBackground());
				}
			}
			boolean valu=(Boolean) value;
			box.setSelected(valu);
			return box;
		}
		
		
		JLabel label = new JLabel();
		label.setOpaque(true);
		if (isSelected) {//点击表格的时候改变点击的行的背景色
			label.setBackground(new Color(135, 206, 250));
		} else {
			if (row % 2 == 0) {
				label.setBackground(new Color(240, 250, 250));
				label.setForeground(table.getForeground());
			} else {
				label.setBackground(table.getBackground());
			}
		}
		label.setText(value != null ? value.toString() : "");
		return label;
	}
}

2:重写TableCellEditor类(工具类,可以添加JTextField,JCheckBox,JComboBox等控件到单元格中)

package JTableDemo;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.io.Serializable;
import java.util.EventObject;
import javax.swing.AbstractCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.table.TableCellEditor;

public class MyTableCellEditor  extends AbstractCellEditor implements TableCellEditor{

	protected JComponent editorComponent;
	protected EditorDelegate delegate;
	protected int clickCountToStart = 1;
	
	public MyTableCellEditor(final JTextField textField) {
		editorComponent = textField;
		this.clickCountToStart = 2;
		delegate = new EditorDelegate() {
			public void setValue(Object value) {
				textField.setText((value != null) ? value.toString().trim() : "");
			}

			public Object getCellEditorValue() {
				return textField.getText();
			}
		};
		textField.addActionListener(delegate);
	}
	
	public MyTableCellEditor(final JCheckBox checkBox) {
		editorComponent = checkBox;
		checkBox.setSelected(false);
		checkBox.setHorizontalAlignment(JCheckBox.CENTER);
		delegate = new EditorDelegate() {
			public void setValue(Object value) { 
				boolean selected = false; 
				if (value instanceof Boolean) {
					selected = ((Boolean)value).booleanValue();
				}
				else if (value instanceof String) {
					selected = value.equals("true");
				}
				checkBox.setSelected(selected);
			}

			public Object getCellEditorValue() {
				return Boolean.valueOf(checkBox.isSelected());
			}
		};
		checkBox.addActionListener(delegate);
		checkBox.setRequestFocusEnabled(false);
	}

	public MyTableCellEditor(final JComboBox comboBox) {
		editorComponent = comboBox;
		comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
		delegate = new EditorDelegate() {
			public void setValue(Object value) {
				comboBox.setSelectedItem(value);
			}

			public Object getCellEditorValue() {
				return comboBox.getSelectedItem();
			}

			public boolean shouldSelectCell(EventObject anEvent) { 
				if (anEvent instanceof MouseEvent) { 
					MouseEvent e = (MouseEvent)anEvent;
					return e.getID() != MouseEvent.MOUSE_DRAGGED;
				}
				return true;
			}
			public boolean stopCellEditing() {
				if (comboBox.isEditable()) {
					// Commit edited value.
					comboBox.actionPerformed(new ActionEvent(
							MyTableCellEditor.this, 0, ""));
				}
				return super.stopCellEditing();
			}
		};
		comboBox.addActionListener(delegate);
	}

	public Component getComponent() {
		return editorComponent;
	}

	public void setClickCountToStart(int count) {
		clickCountToStart = count;
	}
	public int getClickCountToStart() {
		return clickCountToStart;
	}
	public Object getCellEditorValue() {
		return delegate.getCellEditorValue();
	}

	public boolean isCellEditable(EventObject anEvent) { 
		if(anEvent != null && delegate != null){
			return delegate.isCellEditable(anEvent); 
		}else{
			return false;
		}
		
	}

	public boolean shouldSelectCell(EventObject anEvent) { 
		return delegate.shouldSelectCell(anEvent); 
	}

	public boolean stopCellEditing() {
		return delegate.stopCellEditing();
	}

	public void cancelCellEditing() {
		delegate.cancelCellEditing();
	}

	public Component getTreeCellEditorComponent(JTree tree, Object value,
			boolean isSelected,
			boolean expanded,
			boolean leaf, int row) {
			String  stringValue = tree.convertValueToText(value, isSelected,
					expanded, leaf, row, false);

		delegate.setValue(stringValue);
		return editorComponent;
	}
	public Component getTableCellEditorComponent(JTable table, Object value,
			boolean isSelected,
			int row, int column) {
		delegate.setValue(value);
		return editorComponent;
	}


	protected class EditorDelegate implements ActionListener, ItemListener, Serializable {

		/**  The value of this cell. */
		protected Object value;

		public Object getCellEditorValue() {
			return value;
		}

		public void setValue(Object value) { 
			this.value = value; 
		}

		public boolean isCellEditable(EventObject anEvent) {
			if (anEvent instanceof MouseEvent) { 
				return ((MouseEvent)anEvent).getClickCount() >= clickCountToStart;
			}
			return true;
		}

		public boolean shouldSelectCell(EventObject anEvent) { 
			return true; 
		}
		public boolean startCellEditing(EventObject anEvent) {
			return true;
		}
		public boolean stopCellEditing() { 
			fireEditingStopped(); 
			return true;
		}
		public void cancelCellEditing() { 
			fireEditingCanceled(); 
		}
		public void actionPerformed(ActionEvent e) {
			MyTableCellEditor.this.stopCellEditing();
		}
		public void itemStateChanged(ItemEvent e) {
			MyTableCellEditor.this.stopCellEditing();
		}
	}
}

3:重写DefaultTableModel类

package JTableDemo;

import javax.swing.table.DefaultTableModel;

public class MyTableModel extends DefaultTableModel{
	
	private static final long serialVersionUID = 1L;
	private static final String[] columns={"字段一","字段二","字段三","字段四"};//所有的列字段
	
	public MyTableModel(Object[][] data){
		super(data, columns);
	}
	
	@Override
	public boolean isCellEditable(int row, int column) {
		// TODO Auto-generated method stub
		//重写isCellEditable方法,设置是否可以对表格进行编辑,也可以设置某行或者列,可以编辑或者不可以编辑
		return super.isCellEditable(row, column);
	}
	
	@Override
	public void setValueAt(Object arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		super.setValueAt(arg0, arg1, arg2);
	}

}

最后效果:

Swing中表格(JTable)的详细使用_第1张图片

 

源码下载链接:https://download.csdn.net/download/qq_36880602/10472256

 

 

 

   

 

你可能感兴趣的:(Swing常用控件)