java 制作可滚动的表格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14789884/
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
Make scrollable table
提问by Jesus Rodriguez
I'm making this program in Java in which I sell stuff to those connected via socket. I'm loading the products and prices via JDBC just fine, but I want to display the product with the price next to it in a scrollable table. Right now I just have this JList in which I load the names of the products:
我正在用 Java 制作这个程序,在其中我向通过套接字连接的人出售东西。我正在通过 JDBC 加载产品和价格就好了,但我想在可滚动表中显示产品旁边的价格。现在我只有这个 JList,我在其中加载了产品的名称:
Please, click the link below to understand the question.
请单击下面的链接以了解问题。
What elements should I use and how to accomplish what I'm needing?
我应该使用哪些元素以及如何完成我所需要的?
Thanks for reading.
谢谢阅读。
回答by exexzian
You need to use JTable
with JScrollPane
您需要使用JTable
与JScrollPane
Updatedreference example links:
更新了参考示例链接:
this reference exampleand and thismight help you
回答by Gnanasekaran Palanisamy
You need to use JTableand JScrollPane
您需要使用JTable和JScrollPane
Sample Code:
示例代码:
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class ProductTableExample {
public static void main( String[] str ) {
String[] colName = new String[] { "Product Name" ,"Price" };
Object[][] products = new Object[][] {
{ "Galleta" ,"" },
{ "Malta" ,"" },
{ "Nestea" ,"0" },
{ "Tolta" ,"0" }
};
JTable table = new JTable( products, colName );
JFrame frame = new JFrame( "Simple Table Example" );
// create scroll pane for wrapping the table and add
// it to the frame
frame.add( new JScrollPane( table ) );
frame.pack();
frame.setVisible( true );
}
}
回答by aleroot
回答by Abhishekkumar
Use JTable with JScrollPane in it for scrolling from top to down or from left to right.
使用带有 JScrollPane 的 JTable 从上到下或从左到右滚动。
回答by shuangwhywhy
You need a JTable in a JScrollPane.
您需要 JScrollPane 中的 JTable。