当前位置: 技术问答>java相关
焦急救援 Table 的 滚动条
来源: 互联网 发布时间:2014-12-27
本文导语: 请高手指点 Table 的 滚动条 问题: 请问怎样设置使Table出现滚动条?? 为什么Table只有直接放在Frame(JApplet)上时出现滚动条。 如果先把Table放在Panel上,再将Pane放在Frame(JApplet) 上滚动设置无效。 程序如下: import ...
请高手指点 Table 的 滚动条 问题:
请问怎样设置使Table出现滚动条??
为什么Table只有直接放在Frame(JApplet)上时出现滚动条。
如果先把Table放在Panel上,再将Pane放在Frame(JApplet)
上滚动设置无效。
程序如下:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
public class Table1 extends JPanel {
private Object[][] rowData;
Object[] columnData;
private DefaultTableModel model=new DefaultTableModel();
private JTable table=new JTable(model);
JScrollPane jp = new JScrollPane(table);
public Table1() {
Object[][] rowData = {
{"Mary", "Campione",
"Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath",
"Chasing toddlers", new Integer(2), new Boolean(false)},
{"Mark", "Andrews",
"Speed reading", new Integer(20), new Boolean(true)},
{"Angela", "Lih",
"Teaching high school", new Integer(4), new Boolean(false)}
};
Object[] columnData={"a","dsf","sdf","df","df"};
model.setDataVector(rowData,columnData);
table.setAutoResizeMode(3); //???????
table.setAutoscrolls(true); //???????
jp.setAutoscrolls(true); //???????
this.add(jp);
}
public static void main(String args[]) {
Frame mainFrame=new Frame("test table");
Table1 tTable=new Table1();
mainFrame.add(tTable);
mainFrame.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
mainFrame.setSize(400,100);
mainFrame.setVisible(true);
}
}
另外,好多example使用AbstractTableModel,不使用DefaultTableModel。前者不更麻烦吗?
请问怎样设置使Table出现滚动条??
为什么Table只有直接放在Frame(JApplet)上时出现滚动条。
如果先把Table放在Panel上,再将Pane放在Frame(JApplet)
上滚动设置无效。
程序如下:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
public class Table1 extends JPanel {
private Object[][] rowData;
Object[] columnData;
private DefaultTableModel model=new DefaultTableModel();
private JTable table=new JTable(model);
JScrollPane jp = new JScrollPane(table);
public Table1() {
Object[][] rowData = {
{"Mary", "Campione",
"Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath",
"Chasing toddlers", new Integer(2), new Boolean(false)},
{"Mark", "Andrews",
"Speed reading", new Integer(20), new Boolean(true)},
{"Angela", "Lih",
"Teaching high school", new Integer(4), new Boolean(false)}
};
Object[] columnData={"a","dsf","sdf","df","df"};
model.setDataVector(rowData,columnData);
table.setAutoResizeMode(3); //???????
table.setAutoscrolls(true); //???????
jp.setAutoscrolls(true); //???????
this.add(jp);
}
public static void main(String args[]) {
Frame mainFrame=new Frame("test table");
Table1 tTable=new Table1();
mainFrame.add(tTable);
mainFrame.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
mainFrame.setSize(400,100);
mainFrame.setVisible(true);
}
}
另外,好多example使用AbstractTableModel,不使用DefaultTableModel。前者不更麻烦吗?
|
>>
The JTable has a method, setAutoResizeMode(int), that defines the resize policy based on 5 constants, also defined in JTable :
AUTO_RESIZE_OFF, AUTO_RESIZE_NEXT_COLUMN, AUTO_RESIZE_SUBSEQUENT_COLUMNS,
AUTO_RESIZE_LAST_COLUMN, AUTO_RESIZE_ALL_COLUMNS
AUTO_RESIZE_OFF will disable all resizing and enable the horizontal scroll bar of the parent JScrollPane.
The JTable has a method, setAutoResizeMode(int), that defines the resize policy based on 5 constants, also defined in JTable :
AUTO_RESIZE_OFF, AUTO_RESIZE_NEXT_COLUMN, AUTO_RESIZE_SUBSEQUENT_COLUMNS,
AUTO_RESIZE_LAST_COLUMN, AUTO_RESIZE_ALL_COLUMNS
AUTO_RESIZE_OFF will disable all resizing and enable the horizontal scroll bar of the parent JScrollPane.
|
(参考)
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.*;
public class DataFileTable extends JPanel {
public DataFileTable(String dataFilePath) {
JTable table;
DataFileTableModel model;
Font f;
f = new Font("SanSerif",Font.PLAIN,24);
setFont(f);
setLayout(new BorderLayout());
model = new DataFileTableModel(dataFilePath);
table = new JTable();
table.setModel(model);
table.createDefaultColumnsFromModel();
JScrollPane scrollpane = new JScrollPane(table);
add(scrollpane);
}
public Dimension getPreferredSize(){
return new Dimension(400, 300);
}
public static void main(String s[]) {
JFrame frame = new JFrame("Data File Table");
DataFileTable panel;
panel = new DataFileTable("customers.dat");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setForeground(Color.black);
frame.setBackground(Color.lightGray);
frame.getContentPane().add(panel,"Center");
frame.setSize(panel.getPreferredSize());
frame.setVisible(true);
frame.addWindowListener(new WindowCloser());
}
}
class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent e) {
Window win = e.getWindow();
win.setVisible(false);
System.exit(0);
}
}
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.*;
public class DataFileTable extends JPanel {
public DataFileTable(String dataFilePath) {
JTable table;
DataFileTableModel model;
Font f;
f = new Font("SanSerif",Font.PLAIN,24);
setFont(f);
setLayout(new BorderLayout());
model = new DataFileTableModel(dataFilePath);
table = new JTable();
table.setModel(model);
table.createDefaultColumnsFromModel();
JScrollPane scrollpane = new JScrollPane(table);
add(scrollpane);
}
public Dimension getPreferredSize(){
return new Dimension(400, 300);
}
public static void main(String s[]) {
JFrame frame = new JFrame("Data File Table");
DataFileTable panel;
panel = new DataFileTable("customers.dat");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setForeground(Color.black);
frame.setBackground(Color.lightGray);
frame.getContentPane().add(panel,"Center");
frame.setSize(panel.getPreferredSize());
frame.setVisible(true);
frame.addWindowListener(new WindowCloser());
}
}
class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent e) {
Window win = e.getWindow();
win.setVisible(false);
System.exit(0);
}
}
|
用AbstractTableModel是因为需要定制表格,这样才能够灵活地使用。defaultTableModel不够灵活
|
不好意思,没看清你的题目就回答了。
你可以试一试table类的方法:
table.setShowVerticalLines(true);
table.setShowHorizontalLines(true);
你可以试一试table类的方法:
table.setShowVerticalLines(true);
table.setShowHorizontalLines(true);
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。