JTable(JScrollPane)
1.怎么设置标题栏的高度和宽度
2.怎么设置奇偶行的背景色
3.怎么设置可以实现以下功能:不用拉动下滑条就可以显示最后一条数据
4.怎么设置可以实现以下功能:列不可以被拖动
1.怎么设置标题栏的高度和宽度
2.怎么设置奇偶行的背景色
3.怎么设置可以实现以下功能:不用拉动下滑条就可以显示最后一条数据
4.怎么设置可以实现以下功能:列不可以被拖动
//利用JTable中的getColumnModel()方法取得TableColumnModel对象;再利用TableColumnModel界面所定义的getColumn()方法取
//TableColumn对象,利用此对象的setPreferredWidth()方法就可以控制字段的宽度.
column=table.getColumnModel().getColumn(i);
if (i==1) column.setPreferredWidth(150);
if (i==2) column.setPreferredWidth(100);
}
JTableHeader有height,width屬性
4.怎么设置可以实现以下功能:列不可以被拖动
JTableHeader的屬性TresizingAllowed
现在可以做到滚动条可以自动下移,
现在的问题是不能下移到最后一条数据处(仅仅可以下移到倒数第二条数据处).
/*
*功能:设置滚动条到最后一条数据
*/
public void moveEnd()
{
int rowIndex = table.getRowCount();
int vColIndex = 5; if (!(table.getParent() instanceof JViewport)) {
return;
} JViewport viewport = (JViewport)table.getParent(); // This rectangle is relative to the table where the northwest corner of cell (0,0) is always (0,0).
Rectangle rect = table.getCellRect(rowIndex, vColIndex, true); // The location of the view relative to the table
Rectangle viewRect = viewport.getViewRect(); // Translate the cell location so that it is relative
// to the view, assuming the northwest corner of the view is (0,0).
rect.setLocation(rect.x - viewRect.x, rect.y - viewRect.y); // Calculate location of rect if it were at the end of view
rect.translate(viewRect.width - rect.width, viewRect.height - rect.height); // Scroll the area into view.
viewport.scrollRectToVisible(rect);
}
import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.Point;import java.awt.event.MouseMotionAdapter;import java.awt.Toolkit;import java.awt.Dimension;
import javax.swing.JTable.*;import javax.swing.JScrollPane.*;
import java.awt.*;import javax.swing.*;import java.util.* ;import java.util.Vector;import javax.swing.table.DefaultTableModel;import java.lang.Thread.*;import Lib.ShowSystemTime.*;
import java.awt.Color.*;import javax.swing.table.DefaultTableCellRenderer;import javax.swing.table.TableColumn;import javax.swing.table.*;
public class ConsumeMonitor extends JFrame
{
private int iPreEndPosX = 653;
// 标题
private String[] columnNames = {"时间", "信 用 卡 号", "功 能", "金 额", "TID", "返回结果"}; //
Vector data = new Vector();
private Point pressedPt;
public ConsumeMonitor()
{
setDefaultCloseOperation(EXIT_ON_CLOSE); this.setUndecorated(true); this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
pressedPt = e.getPoint(); }
});
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
JFrame frame = (JFrame) e.getSource(); frame.setLocation(0, 0); }
});
this.setLocation(0, 0);
init(); } public void Show()
{
loadTitle();
initFrame(); } public void init()
{
Container container = this.getContentPane(); container.setLayout(new GridBagLayout());
GridBagConstraints gridBagConstraints = new GridBagConstraints();
int anchor = gridBagConstraints.CENTER; int iFill = gridBagConstraints.BOTH;
container.add(scrollPane,new GridBagConstraints(0,0,1,1,1.0,1.0,anchor,iFill,new Insets(0,0,0,0),-22,-138));
scrollPane.getViewport().add(table); table.setBounds(0,0,422,0);
//setSize(1020,760); setSize(800,360); show(); } /*
*功能:填充数据
*显示格式:
* 时间 信 用 卡 号 功 能 金 额 TID 返回结果
*/
public void insertRecord(Hashtable hashtable)
{
Vector rowdata = new Vector(5); // 时间
rowdata.add(hashtable.get("Time")); // 信 用 卡 号
rowdata.add(hashtable.get("BankCard")); // 功 能
rowdata.add(hashtable.get("BusinessType")); // 金 额
rowdata.add(hashtable.get("Sum")); // TID
rowdata.add(hashtable.get("TID")); // 返回结果
rowdata.add(hashtable.get("BusinessResult"));
//将数据加入到容器,也就是加入到JTable 中
data.add(rowdata);
setRowBackground();
moveEnd();
//最后刷新JTable 控件
this.table.updateUI(); } /*
*功能:设置滚动条到最后一条数据
*/
public void moveEnd()
{
int rowIndex = table.getRowCount(); if(rowIndex <= 14)
{
//return; } int vColIndex = 5;
if (!(table.getParent() instanceof JViewport)) {
return; } JViewport viewport = (JViewport)table.getParent();
// This rectangle is relative to the table where the northwest corner of cell (0,0) is always (0,0).
Rectangle rect = table.getCellRect(rowIndex, vColIndex, true); if(rect.y == 0)
{
rect.y = 21; }
// The location of the view relative to the table
Rectangle viewRect = viewport.getViewRect(); if(viewRect.y == 16)
{
viewRect.y = 21; } // Translate the cell location so that it is relative
// to the view, assuming the northwest corner of the view is (0,0).
rect.setLocation(rect.x - viewRect.x, rect.y - viewRect.y);
// Calculate location of rect if it were at the end of view rect.translate(viewRect.width - rect.width, viewRect.height - rect.height);
// Scroll the area into view.
viewport.scrollRectToVisible(rect); } /*
*功能:加载标题
*/
public void loadTitle()
{
//设置列标题
Vector cname = new Vector(6); for(int i = 0; i < 6; i++)
{
cname.add(columnNames[i]); }
DefaultTableModel model=new DefaultTableModel(data,cname); table.setModel(model);
// 设置标题栏的宽度
setColumnWidth();
//最后刷新JTable 控件
table.updateUI(); } /*
*功能:实时显示交易状态
*/
public void initFrame()
{
int iLine = 0; try
{
Hashtable dataRecord = new Hashtable(); loadTitle();
for(int i = 0; i < 18; i++)
{
dataRecord = initHashtable(); insertRecord(dataRecord); Thread.sleep(1000); }
} catch (InterruptedException e){
System.out.println(e.getMessage()); }
} /*
*功能:测试时,加载数据
*/
private Hashtable initHashtable()
{
Hashtable hashtable = new Hashtable();
// 读取系统时间
ShowSystemTime systemTime = new ShowSystemTime(); String sSystemTime = systemTime.showTime();
// 时间
hashtable.put("Time", sSystemTime); // 信 用 卡 号
hashtable.put("BankCard", "1234567890-1234567890-12"); // 功 能
hashtable.put("BusinessType", "消费"); // 金 额
hashtable.put("Sum", "123456789.12"); // TID
hashtable.put("TID", "TID"); // 返回结果
hashtable.put("BusinessResult", "成功");
return hashtable; } public static void main(String[] args)
{
ConsumeMonitor app = new ConsumeMonitor(); app.Show(); } javax.swing.JScrollPane scrollPane = new javax.swing.JScrollPane(); javax.swing.JTable table = new javax.swing.JTable(){
public boolean isCellEditable(int rowIndex, int vColIndex) {
return false; }
};}