Java 如何在 jtable 中添加表头和滚动条。爪哇
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28128035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Add Table header and Scrollbar in jtable. java
提问by Adnan Ali
I am trying to add a Table headerand Scrollbarin JTABLE
, but I'm not getting what I want. I have tried with 3 different procedures, but did not come up with the right one. This code is for Scrollbar.
我想添加一个表头和滚动条的JTABLE
,但我没有得到我想要的东西。我尝试了 3 种不同的程序,但没有想出正确的程序。此代码用于Scrollbar。
Can any one tell me how to add header. I can't add it just as model.addRow()
because after that I have to get the values from the table and this method create problems with that. and How to add Scroll bar code too.
谁能告诉我如何添加标题。我不能添加它,model.addRow()
因为在那之后我必须从表中获取值,而这种方法会产生问题。以及如何添加滚动条码。
This is my Code
这是我的代码
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
table.setFont(new Font("Times New Roman", Font.PLAIN, 13));
//JScrollPane scrollPane = new JScrollPane(table); //Try one
//new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED ); // Try 2
model.addColumn("ID");
model.addColumn("NAME");
model.addColumn("MODEL");
model.addColumn("P_Price");
model.addColumn("S_Price");
model.addColumn("Quantity");
table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
table.setBounds(10, 0, 457, 103);
//panel_1.add(new JScrollPane(table)); Try 3
//panel_1.add(scrollPane); //link to Try one
//table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); // link 2 Try 2
panel_1.add(table);
采纳答案by silentprogrammer
To add headers do
添加标题做
String [] header={"name","age"};
String [][] data={{"akash","20"},{"pankaj","24"},{"pankaj","24"},{"pankaj","24"},{"pankaj","24"}};
DefaultTableModel model = new DefaultTableModel(data,header);
To add scrollbar do
添加滚动条做
JScrollPane js=new JScrollPane(table);
js.setVisible(true);
add(js);
Full Code
完整代码
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Table extends JPanel{
JTable jt;
public Table(){
String [] header={"name","age"};
String [][] data={{"akash","20"},{"pankaj","24"},{"pankaj","24"},{"pankaj","24"},{"pankaj","24"}};
DefaultTableModel model = new DefaultTableModel(data,header);
JTable table = new JTable(model);
table.setPreferredScrollableViewportSize(new Dimension(450,63));
table.setFillsViewportHeight(true);
JScrollPane js=new JScrollPane(table);
js.setVisible(true);
add(js);
}
public static void main(String [] a) {
JFrame jf=new JFrame();
Table tab= new Table();
jf.setTitle("Table");
jf.setSize(500, 500);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(tab);
}
}
Output
输出
回答by Hovercraft Full Of Eels
I fear that you may be messing yourself up by use of null
layouts and setBounds(...)
something that does not work well with most Swing GUI's and in particular JTables displayed in JScrollPanes.
我担心您可能会因为使用null
布局和setBounds(...)
与大多数 Swing GUI,特别是 JScrollPanes 中显示的 JTables 不兼容的东西而把自己搞砸了。
Suggestions:
建议:
- Don't use
null
layouts, don't callsetBounds(...)
especiallyon something that you want to display inside of scrollpanes. - Add the JTable to the JScrollPane via the JScrollPane's constructor.
- Add the JScrollPane thus created to a JPanel that uses
BorderLayout
in theBorderLayout.CENTER
position.
- 不要使用
null
布局,不要setBounds(...)
特别调用要在滚动窗格内显示的内容。 - 通过 JScrollPane 的构造函数将 JTable 添加到 JScrollPane。
- 将这样创建的 JScrollPane 添加到
BorderLayout
在该BorderLayout.CENTER
位置使用的 JPanel 。
For example:
例如:
import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.*;
import javax.swing.table.*;
public class Foo {
private JPanel panel_1 = new JPanel();
public Foo() {
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
table.setFont(new Font("Times New Roman", Font.PLAIN, 13));
JScrollPane scrollPane = new JScrollPane(table);
model.addColumn("ID");
model.addColumn("NAME");
model.addColumn("MODEL");
model.addColumn("P_Price");
model.addColumn("S_Price");
model.addColumn("Quantity");
panel_1.setLayout(new BorderLayout());
panel_1.add(scrollPane, BorderLayout.CENTER);
}
public JPanel getPanel1() {
return panel_1;
}
private static void createAndShowGui() {
Foo mainPanel = new Foo();
JFrame frame = new JFrame("Foo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel.getPanel1());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Don't even use setBounds(...)
on your JScrollPane. While null layouts and setBounds()
might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms different from the original one.
甚至不要setBounds(...)
在您的 JScrollPane 上使用。虽然空布局和setBounds()
Swing 新手似乎是创建复杂 GUI 的最简单和最好的方法,但您创建的 Swing GUI 越多,使用它们时遇到的困难就越严重。当 GUI 调整大小时,它们不会调整您的组件的大小,它们是增强或维护的皇家女巫,它们放置在滚动窗格中时完全失败,在与原始平台不同的所有平台上查看时,它们看起来很糟糕。
回答by Isthatso
Headers:
标题:
JTable table = new JTable(model);
String cols [] = {"ID","NAME","MODEL","P_Price","S_Price","Quantity"};
model.setColumnIdentifiers(cols );
Scrollpane:
滚动窗格:
JScrollPane jsp = new JScrollPane(table);
panel1.add(jsp);
//....
add(jsp);// add scroll to frame not the panel
Note :
笔记 :
But remember that null layout
is not considered as a good program design, especially when setting up non top level container with .setBounds( )
since it won't guarantee a consistency on other platform regarding to the position of the component.
但请记住,这null layout
不是一个好的程序设计,尤其是在设置非顶级容器时,.setBounds( )
因为它不能保证在其他平台上组件位置的一致性。
回答by Adnan Ali
The only reason for the stuff was that I wasn't adding setBounds(10, 0, 457, 103)
method in JScrollpane.
Thanks everyone for your answers. Credits to @singakash
这些东西的唯一原因是我没有setBounds(10, 0, 457, 103)
在JScrollpane.
感谢大家的回答中添加方法。归功于@singakash
Here is the complete working code.
这是完整的工作代码。
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
table.setFont(new Font("Times New Roman", Font.PLAIN, 13));
model.addColumn("ID");
model.addColumn("NAME");
model.addColumn("MODEL");
model.addColumn("P_Price");
model.addColumn("S_Price");
model.addColumn("Quantity");
table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
table.setBounds(10, 0, 457, 103);
JScrollPane jp=new JScrollPane(table);
jp.setBounds(10, 0, 457, 103);
jp.setVisible(true);
add(jp);
panel_1.add(jp);