我检查了有关在JTable上进行水平滚动的stackoverflow的问题。设置AUTO_RESIZE_OFF会使我获得水平滚动条,但同时表格不会使用滚动窗格的整个宽度。
我做了一些阅读,事实证明这是自1998年以来一直在运行bug(Oracle会解决这个问题吗?)
我看到了一些关于骑行方法的建议,但没有一个对我有用。那么有人有答案吗?非常感谢。基本上我需要表自动调整大小,但在任何列名称被压缩之前启用水平滚动条。
感谢。
答案 0 :(得分:6)
对其他答案进行咆哮后 - JXTable(在SwingX project中)有一个额外的列布局属性
需要进行一些内部调整,因此(偏见我)建议使用JXTable。或者看看它的代码和c& p - 所有允许的,所有开源: - )
答案 1 :(得分:3)
检查此链接并尝试解决此问题
http://www.daniweb.com/software-development/java/threads/29263
JTable with horizontal scrollbar
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
public class ScrollableJTable {
public static void main(String[] args) {
new ScrollableJTable();
}
public ScrollableJTable() {
JFrame frame = new JFrame("Creating a Scrollable JTable!");
JPanel panel = new JPanel();
String data[][] = {
{"001", "vinod", "Bihar", "India", "Biology", "65", "First"},
{"002", "Raju", "ABC", "Kanada", "Geography", "58", "second"},
{"003", "Aman", "Delhi", "India", "computer", "98", "Dictontion"},
{"004", "Ranjan", "Bangloor", "India", "chemestry", "90", "Dictontion"},
{"001", "vinod", "Bihar", "India", "Biology", "65", "First"},
{"002", "Raju", "ABC", "Kanada", "Geography", "58", "second"},
{"003", "Aman", "Delhi", "India", "computer", "98", "Dictontion"},
{"004", "Ranjan", "Bangloor", "India", "chemestry", "90", "Dictontion"},
{"001", "vinod", "Bihar", "India", "Biology", "65", "First"},
{"002", "Raju", "ABC", "Kanada", "Geography", "58", "second"},
{"003", "Aman", "Delhi", "India", "computer", "98", "Dictontion"},
{"004", "Ranjan", "Bangloor", "India", "chemestry", "90", "Dictontion"},
{"001", "vinod", "Bihar", "India", "Biology", "65", "First"},
{"002", "Raju", "ABC", "Kanada", "Geography", "58", "second"},
{"003", "Aman", "Delhi", "India", "computer", "98", "Dictontion"},
{"004", "Ranjan", "Bangloor", "India", "chemestry", "90", "Dictontion"},
{"001", "vinod", "Bihar", "India", "Biology", "65", "First"},
{"002", "Raju", "ABC", "Kanada", "Geography", "58", "second"},
{"003", "Aman", "Delhi", "India", "computer", "98", "Dictontion"},
{"004", "Ranjan", "Bangloor", "India", "chemestry", "90", "Dictontion"},
{"001", "vinod", "Bihar", "India", "Biology", "65", "First"},
{"002", "Raju", "ABC", "Kanada", "Geography", "58", "second"},
{"003", "Aman", "Delhi", "India", "computer", "98", "Dictontion"},
{"004", "Ranjan", "Bangloor", "India", "chemestry", "90", "Dictontion"},
{"001", "vinod", "Bihar", "India", "Biology", "65", "First"},
{"002", "Raju", "ABC", "Kanada", "Geography", "58", "second"},
{"003", "Aman", "Delhi", "India", "computer", "98", "Dictontion"},
{"004", "Ranjan", "Bangloor", "India", "chemestry", "90", "Dictontion"},
{"001", "vinod", "Bihar", "India", "Biology", "65", "First"},
{"002", "Raju", "ABC", "Kanada", "Geography", "58", "second"},
{"003", "Aman", "Delhi", "India", "computer", "98", "Dictontion"},
{"004", "Ranjan", "Bangloor", "India", "chemestry", "90", "Dictontion"}
};
String col[] = {"Roll", "Name", "State", "country", "Math", "Marks", "Grade"};
JTable table = new JTable(data, col);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JTableHeader header = table.getTableHeader();
header.setBackground(Color.yellow);
JScrollPane pane = new JScrollPane(table);
panel.add(pane);
frame.add(panel);
frame.pack();
// frame.setSize(500, 200);
// frame.setUndecorated(true);
// frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
答案 2 :(得分:2)
答案 3 :(得分:2)
这里有一些链接,我希望它们不会破坏。 swinglabs下载页面在这里:
http://swinglabs.org/downloads.jsp
但是在我访问该网站时,这些下载被破坏了(这可能现在已经解决了),经过一番调查后,我发现他们的服务器上有无聊的访问权限:
http://java.net/downloads/swingx/releases/1.6.2/
这就是我遇到的所有麻烦,只需找到库。这只是将.jar文件解压缩并添加到我的项目然后导入JXTable(org.jdesktop.swingx.JXTable)的问题。
JXTable扩展了常规JTable(参见文档:http://download.java.net/javadesktop/swinglabs/releases/0.8/docs/api/org/jdesktop/swingx/JXTable.html)因此所有常规JTable方法仍然可用。
导入库后,只需告诉表格水平滚动
JXTable table = new JXTable(yourTableModel)
table.setHorizontalScrollEnabled(true)
Swing labs提供了一个可爱的教程,解释了他们在JTable上所做的所有改进。请参阅此处的教程:
http://swinglabs.org/docs/components/JXTable/tutorial.jsp?step=0
希望这有帮助,真的帮助了我。感谢Kleopatra指出了正确的道路。