当前位置: 技术问答>java相关
怎样使用JTable Class ..........
来源: 互联网 发布时间:2015-04-03
本文导语: 我在做程序时,要生成一个JTable,左边的一栏要放一个JCheckbox 如下例: 口 第一栏 第二栏 第三栏 请问如何实现? | import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*;...
我在做程序时,要生成一个JTable,左边的一栏要放一个JCheckbox
如下例:
口 第一栏 第二栏 第三栏
请问如何实现?
如下例:
口 第一栏 第二栏 第三栏
请问如何实现?
|
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class JTest extends JFrame
{
protected JTable m_table;
protected TableData m_data;
public JTest() {
super("Expense Report");
setSize(200, 200);
m_data = new TableData();
m_table = new JTable();
m_table.setAutoCreateColumnsFromModel(false);
m_table.setModel(m_data);
m_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
TableCellRenderer renderer = new CheckCellRenderer();
TableCellEditor editor = new DefaultCellEditor(new JCheckBox());
TableColumn column = new TableColumn(0,30,renderer, editor);
m_table.addColumn(column);
renderer = new DefaultTableCellRenderer();
editor = new DefaultCellEditor(new JTextField());
column = new TableColumn(1,180,renderer, editor);
m_table.addColumn(column);
JTableHeader header = m_table.getTableHeader();
header.setUpdateTableInRealTime(false);
JScrollPane ps = new JScrollPane();
ps.setSize(220, 220);
ps.getViewport().add(m_table);
getContentPane().add(ps, BorderLayout.CENTER);
WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(wndCloser);
setVisible(true);
}
public static void main(String argv[]) {
new JTest();
}
}
class CheckCellRenderer extends JCheckBox implements TableCellRenderer
{
protected static Border m_noFocusBorder;
public CheckCellRenderer() {
super();
m_noFocusBorder = new EmptyBorder(1, 2, 1, 2);
setOpaque(true);
setBorder(m_noFocusBorder);
}
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column)
{
if (value instanceof Boolean) {
Boolean b = (Boolean)value;
setSelected(b.booleanValue());
}
setBackground(isSelected && !hasFocus ?
table.getSelectionBackground() : table.getBackground());
setForeground(isSelected && !hasFocus ?
table.getSelectionForeground() : table.getForeground());
setFont(table.getFont());
setBorder(hasFocus ? UIManager.getBorder(
"Table.focusCellHighlightBorder") : m_noFocusBorder);
return this;
}
}
class TableData extends AbstractTableModel
{
protected Vector m_vector;
public TableData() {
m_vector = new Vector();
setDefaultData();
}
public void setDefaultData() {
m_vector.removeAllElements();
for(int i=0;i
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class JTest extends JFrame
{
protected JTable m_table;
protected TableData m_data;
public JTest() {
super("Expense Report");
setSize(200, 200);
m_data = new TableData();
m_table = new JTable();
m_table.setAutoCreateColumnsFromModel(false);
m_table.setModel(m_data);
m_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
TableCellRenderer renderer = new CheckCellRenderer();
TableCellEditor editor = new DefaultCellEditor(new JCheckBox());
TableColumn column = new TableColumn(0,30,renderer, editor);
m_table.addColumn(column);
renderer = new DefaultTableCellRenderer();
editor = new DefaultCellEditor(new JTextField());
column = new TableColumn(1,180,renderer, editor);
m_table.addColumn(column);
JTableHeader header = m_table.getTableHeader();
header.setUpdateTableInRealTime(false);
JScrollPane ps = new JScrollPane();
ps.setSize(220, 220);
ps.getViewport().add(m_table);
getContentPane().add(ps, BorderLayout.CENTER);
WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(wndCloser);
setVisible(true);
}
public static void main(String argv[]) {
new JTest();
}
}
class CheckCellRenderer extends JCheckBox implements TableCellRenderer
{
protected static Border m_noFocusBorder;
public CheckCellRenderer() {
super();
m_noFocusBorder = new EmptyBorder(1, 2, 1, 2);
setOpaque(true);
setBorder(m_noFocusBorder);
}
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column)
{
if (value instanceof Boolean) {
Boolean b = (Boolean)value;
setSelected(b.booleanValue());
}
setBackground(isSelected && !hasFocus ?
table.getSelectionBackground() : table.getBackground());
setForeground(isSelected && !hasFocus ?
table.getSelectionForeground() : table.getForeground());
setFont(table.getFont());
setBorder(hasFocus ? UIManager.getBorder(
"Table.focusCellHighlightBorder") : m_noFocusBorder);
return this;
}
}
class TableData extends AbstractTableModel
{
protected Vector m_vector;
public TableData() {
m_vector = new Vector();
setDefaultData();
}
public void setDefaultData() {
m_vector.removeAllElements();
for(int i=0;i