调整 Jtable 的大小以填充 JFrame 并且仍然滚动
我正在尝试调整选项卡视图中的scrollPanel 中的两个JTable 的大小。我已设法将框架的大小设置为 12000、5000,但 Jtable/scrollPanel 无法正常工作。
我找到了一些代码来设置有效的表格列的大小,但它没有填充 Jframe,而是在表格/scrollPanel 的底部添加了一个滚动条,
任何人都可以想出一种方法来让表格填充框架并且仍然有滚动?
表文件为:
public class ViewInTablePET extends JPanel {
private String[][] rowData = new String[0][];
private String columnNames[] = {"Shop", "Type", "price", "dateAcquired", "notes"};
private DefaultTableModel tableMod = new DefaultTableModel(rowData, columnNames);
private JTable table = new JTable(tableMod);
public ViewInTablePET() {
String bob;
String[] dan;
for (Integer i = 0; i < AppModel.getArrayListPet().size(); i++) {
bob = AppModel.getArrayListPet().get(i).toString();
dan = AppModel.getStringAsArray(bob, "\t");
tableMod.insertRow(i, new Object[]{dan[0], dan[1], dan[2], dan[3], dan[4]});
}
JScrollPane scrollPane = new JScrollPane(table);
this.add(scrollPane, BorderLayout.CENTER);
}
}
Fram 文件为:
public class ViewInTabbs extends JFrame {
JLabel Average = new JLabel("Click a table row to get the average price");
public ViewInTabbs() {
JTabbedPane tabs = new JTabbedPane();
ViewInTablePetShope petShop = new ViewInTablePetShope();
ViewInTablePET pet = new ViewInTablePET();
tabs.add("Pet Shope", (JPanel) petShop);
tabs.add("Pets in the shop", (JPanel) pet);
this.setTitle("Tabb View of Data");
this.add(tabs, BorderLayout.CENTER);
this.setSize(12000, 5000);
this.setVisible(true);
this.add(Average, BorderLayout.SOUTH);
}
public void setAverage(String ave) {
this.Average.setText(ave);
}
}
I am trying to resize two JTable that are in scrollPanels that are in a Tab view. I have managed to set the size of the frame to 12000, 5000 but the Jtable/scrollPanel are not playing ball.
I found some code to set the size of the columns of the table that worked BUT instead of filling the Jframe it just added a scroll bar at the bottom of the table/scrollPanel
can anyone come up with a way of getting the table to fill the frame and still have scrolling?
The Table File is:
public class ViewInTablePET extends JPanel {
private String[][] rowData = new String[0][];
private String columnNames[] = {"Shop", "Type", "price", "dateAcquired", "notes"};
private DefaultTableModel tableMod = new DefaultTableModel(rowData, columnNames);
private JTable table = new JTable(tableMod);
public ViewInTablePET() {
String bob;
String[] dan;
for (Integer i = 0; i < AppModel.getArrayListPet().size(); i++) {
bob = AppModel.getArrayListPet().get(i).toString();
dan = AppModel.getStringAsArray(bob, "\t");
tableMod.insertRow(i, new Object[]{dan[0], dan[1], dan[2], dan[3], dan[4]});
}
JScrollPane scrollPane = new JScrollPane(table);
this.add(scrollPane, BorderLayout.CENTER);
}
}
The Fram file is:
public class ViewInTabbs extends JFrame {
JLabel Average = new JLabel("Click a table row to get the average price");
public ViewInTabbs() {
JTabbedPane tabs = new JTabbedPane();
ViewInTablePetShope petShop = new ViewInTablePetShope();
ViewInTablePET pet = new ViewInTablePET();
tabs.add("Pet Shope", (JPanel) petShop);
tabs.add("Pets in the shop", (JPanel) pet);
this.setTitle("Tabb View of Data");
this.add(tabs, BorderLayout.CENTER);
this.setSize(12000, 5000);
this.setVisible(true);
this.add(Average, BorderLayout.SOUTH);
}
public void setAverage(String ave) {
this.Average.setText(ave);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
啊..第二读:罪魁祸首是 ViewInTablePET 的 LayoutManager,它默认为 FlowLayout。更改边框布局
ahh .. on second reading: the culprit is the LayoutManager of ViewInTablePET which defaults to FlowLayout. Change to BorderLayout
在您的
ViewInTablePET
类中,您添加带有约束BorderLayout.CENTER
的JScrollPane
,但我没有看到setLayout( new BorderLayout( ) )
在该类中调用。由于JPanel
默认情况下具有FlowLayout
,因此不会达到预期的效果。但是,当您首先将布局设置为BorderLayout
时,它应该可以工作In your
ViewInTablePET
class you add theJScrollPane
with the constrainsBorderLayout.CENTER
, but I do not see thesetLayout( new BorderLayout() )
call in that class. Since aJPanel
by default has aFlowLayout
this will not have the desired effect. It should however work when you set the layout toBorderLayout
first