import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;
public class test extends JFrame {
private JPanel jContentPane = null;
private JScrollPane jScrollPane = null;
private JTable viewtable = null;
private JButton jButton = null;
//建立一个模型,之后的表将在建立在模型下,或者说是以模型为一个基本模板;
DefaultTableModel dtm = null;
//表中表头名;
String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years",
"Vegetarian" };
//表中数据;
Object[][] data = {
{ "Mary", "Campione", "Snowboarding", new Integer(5),
new Boolean(false) },
{ "Alison", "Huml", "Rowing", new Integer(3), new Boolean(true) },
{ "Kathy", "Walrath", "Knitting", new Integer(2),
new Boolean(false) },
{ "Sharon", "Zakhour", "Speed reading", new Integer(20),
new Boolean(true) },
{ "Philip", "Milne", "Pool", new Integer(10), new Boolean(false) } };
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
//使用JTABLE ON JSCROLLPANE控件;
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new java.awt.Rectangle(40,40,650,200));
jScrollPane.setViewportView(getViewtable());
}
return jScrollPane;
}
/**
* This method initializes viewtable
*
* @return javax.swing.JTable
*/
//初始化表;
private JTable getViewtable() {
if (viewtable == null) {
viewtable = new JTable();
//将表头和数据放入模型中;
dtm=new DefaultTableModel(data,columnNames);
//用模型建立表;
viewtable=new JTable(dtm);
}
return viewtable;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("del");
jButton.setBounds(new java.awt.Rectangle(289,316,165,28));
//为删除按钮添加监听;
jButton.addActionListener(new ActionListener(){
//实现其中方法;
public void actionPerformed (ActionEvent e){
//取得所选行数组长度;
int numrow=viewtable.getSelectedRows().length;
for (int i=0;i<numrow;i++){
//删除所选行;
dtm.removeRow(viewtable.getSelectedRow());
}
}
});
}
return jButton;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
/**
* This is the default constructor
*/
public test() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(772, 500);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJScrollPane(), null);
jContentPane.add(getJButton(), null);
}
return jContentPane;
}
} // @jve:decl-index=0:visual-constraint="10,10"
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;
public class test extends JFrame {
private JPanel jContentPane = null;
private JScrollPane jScrollPane = null;
private JTable viewtable = null;
private JButton jButton = null;
//建立一个模型,之后的表将在建立在模型下,或者说是以模型为一个基本模板;
DefaultTableModel dtm = null;
//表中表头名;
String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years",
"Vegetarian" };
//表中数据;
Object[][] data = {
{ "Mary", "Campione", "Snowboarding", new Integer(5),
new Boolean(false) },
{ "Alison", "Huml", "Rowing", new Integer(3), new Boolean(true) },
{ "Kathy", "Walrath", "Knitting", new Integer(2),
new Boolean(false) },
{ "Sharon", "Zakhour", "Speed reading", new Integer(20),
new Boolean(true) },
{ "Philip", "Milne", "Pool", new Integer(10), new Boolean(false) } };
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
//使用JTABLE ON JSCROLLPANE控件;
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new java.awt.Rectangle(40,40,650,200));
jScrollPane.setViewportView(getViewtable());
}
return jScrollPane;
}
/**
* This method initializes viewtable
*
* @return javax.swing.JTable
*/
//初始化表;
private JTable getViewtable() {
if (viewtable == null) {
viewtable = new JTable();
//将表头和数据放入模型中;
dtm=new DefaultTableModel(data,columnNames);
//用模型建立表;
viewtable=new JTable(dtm);
}
return viewtable;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("del");
jButton.setBounds(new java.awt.Rectangle(289,316,165,28));
//为删除按钮添加监听;
jButton.addActionListener(new ActionListener(){
//实现其中方法;
public void actionPerformed (ActionEvent e){
//取得所选行数组长度;
int numrow=viewtable.getSelectedRows().length;
for (int i=0;i<numrow;i++){
//删除所选行;
dtm.removeRow(viewtable.getSelectedRow());
}
}
});
}
return jButton;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
/**
* This is the default constructor
*/
public test() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(772, 500);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJScrollPane(), null);
jContentPane.add(getJButton(), null);
}
return jContentPane;
}
} // @jve:decl-index=0:visual-constraint="10,10"