JavaSwing创建表格

一、Applet

package com.jy.applet.day1206;

import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jy.applet.day1206.swing.pane.MyTabPane;



/**
 * 用来实现TAB结构的Applet
 * 
 * @author Administrator
 * 
 */
@SuppressWarnings("serial")
public class AppletTest2 extends JApplet {
	private static final Log log = LogFactory.getLog(AppletTest2.class);// 日志记录
	private static AppletTest2 instance;

	public void init() {
		log.info("Applet初始化......");
		instance = this;
		SwingUtilities.invokeLater(new Runnable() {

			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager
							.getSystemLookAndFeelClassName());
					new MyTabPane();//实例化Tab面板
				} catch (Exception e) {
					e.printStackTrace();
				}

			}

		});
	}

	public static AppletTest2 getInstance() {
		return instance;
	}

	public void destroy() {
		log.info("程序正在关闭.....");
	}

}

二、主面板

package com.jy.applet.day1206.swing.pane;

import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JTabbedPane;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jy.applet.day1206.AppletTest2;


public class MyTabPane extends JTabbedPane{

	private static final long serialVersionUID = -3545783299455340236L;
	private static final Log log = LogFactory.getLog(MyTabPane.class);// 日志记录
	
	public MyTabPane(){
		init();
	}

	private void init() {
		log.info("开始初始化tab面板......");
		JTabbedPane myTabPane = new JTabbedPane(JTabbedPane.TOP);//TAB面板,tab标题位于顶部
		myTabPane.setBorder(BorderFactory.createLineBorder(Color.GREEN, 1));//设置边框
		myTabPane.add("位置导航", null);
		myTabPane.add("详细信息", new MyDetailPane());//这里面放的是一个表格
		myTabPane.add("已选择地点", null);
		myTabPane.add("其他", null);
		myTabPane.setToolTipTextAt(0, "位置导航");
		myTabPane.setToolTipTextAt(1, "详细信息");
		myTabPane.setToolTipTextAt(2, "已选择地点");
		myTabPane.setToolTipTextAt(3, "其他");
		myTabPane.setSelectedIndex(0);//默认选择第0个,即“位置导航”
		myTabPane.setVisible(true);
		AppletTest2.getInstance().getContentPane().add(myTabPane);//将myTabPane添加到Applet的内容面板上
	}
}

三、其中的“详细信息Tab面板”

package com.jy.applet.day1206.swing.pane;

import javax.swing.JScrollPane;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jy.applet.day1206.swing.table.DetailInfoTable;


public class MyDetailPane extends JScrollPane{

	private static final long serialVersionUID = -5258637918997915428L;
	private static final Log log = LogFactory.getLog(MyDetailPane.class);// 日志记录
	
    public MyDetailPane(){
    	init();
    }

	private void init() {
		log.info("开始初始化详细信息面板......");
		setAutoscrolls(true);
		setViewportView(new DetailInfoTable());//表格
		setVisible(true);
	}
}

四、表格类

package com.jy.applet.day1206.swing.table;

import java.awt.Color;
import java.util.List;

import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import com.jy.applet.entity.DetailInfo;
import com.jy.applet.util.DataTestDao;

@SuppressWarnings( { "serial", "unused" })
public class DetailInfoTable extends JTable {

	private static final long serialVersionUID = -8389977798357867875L;
	private DefaultTableModel defaultTableModel;
	private final Object[] columnNames = { "序号", "国家", "省份", "省会城市" };

	public DetailInfoTable() {

		defaultTableModel = new DefaultTableModel(columnNames, 0) {
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		this.setModel(defaultTableModel);
		this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.setRowSelectionAllowed(true);
		this.setGridColor(Color.BLACK);
		this.setShowGrid(true);
		this.setShowHorizontalLines(true);
		this.setShowVerticalLines(true);
		this.setFillsViewportHeight(true);
		this.setDefaultEditor(Object.class, cellEditor);
		this.setColumnWidth(0, 50);
		this.doLayout();
		initRowsData();
	}

	public void initRowsData() {
		this.removeRowsData();

		List<DetailInfo> list = DataTestDao.getDetailInfoList();
		Object[] rowData = new Object[columnNames.length];
		int index = 1;// 用来显示序号的

		if (list != null && !list.isEmpty()) {
			for (DetailInfo info : list) {
				rowData[0] = index++;// 用来显示序号的
				rowData[1] = info.getCountry();// 国家
				rowData[2] = info.getProvince();// 省份
				rowData[3] = info.getCity();// 城市
				defaultTableModel.addRow(rowData);
			}
		}
		this.revalidate();
	}

	private void removeRowsData() {
		int count = defaultTableModel.getRowCount();
		for (count -= 1; count > -1; count--) {
			defaultTableModel.removeRow(count);
		}
	}

	private void setColumnWidth(int columnIndex, int width) {
		TableColumn column = this.getColumnModel().getColumn(columnIndex);
		column.setPreferredWidth(width);
		column.setMaxWidth(width);
		column.setMinWidth(width);
	}
}

五、模拟数据库的DAO

package com.jy.applet.util;

import java.util.ArrayList;
import java.util.List;

import com.jy.applet.entity.DetailInfo;

/**
 * 用户模拟数据的DAO
 * @author Administrator
 *
 */
@SuppressWarnings("unchecked")
public class DataTestDao {
	/**
	 * 获取详细信息
	 * @return
	 */
	public static List<DetailInfo> getDetailInfoList() {
		List<DetailInfo> details = new ArrayList<DetailInfo>();
		details.add(new DetailInfo("1","中国","北京","北京"));
		details.add(new DetailInfo("2","中国","天津","天津"));
		details.add(new DetailInfo("3","中国","山东","济南"));
		details.add(new DetailInfo("4","中国","浙江","杭州"));
		details.add(new DetailInfo("5","中国","江苏","南京"));
		details.add(new DetailInfo("6","中国","广东","广州"));
		details.add(new DetailInfo("7","美国","纽约","纽约"));
		details.add(new DetailInfo("8","英国","伦敦","伦敦"));
		details.add(new DetailInfo("9","法国","巴黎","巴黎"));
		details.add(new DetailInfo("10","俄罗斯","莫斯科","莫斯科"));
		return details;
	}

}

六、运行结果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值