【发布时间】:2013-05-23 10:31:11
【问题描述】:
我不知道给这个问题取什么标题,所以希望没有人太困惑......
我有一个按钮,当单击该按钮时,会将 Player 对象的 ArrayList 中的每个“Player”对象的属性添加到 JTable。目前,我的 ArrayList 中有 500 个“玩家”,我想显示 100 行(因为似乎有 100 行的限制 - 如果我知道如何为下一个/前 10 行做按钮,我会做那)。当转到 JTabbedPane 中的相应选项卡并单击按钮时,我只看到 10 行数据 - 我希望看到 100 行数据并向下滚动以查看其余数据。我已将 JTable 放在 JScrolledPane 中。
这是我按钮的 actionListener 中的代码:
private void getAllPlayersBtnActionPerformed(java.awt.event.ActionEvent evt) {
for(int i = 0; i < players.size(); i++)
{
playersDataTable.setValueAt(players.get(i).getPlayerID(), i, 0);
playersDataTable.setValueAt(players.get(i).getPlayerName(), i, 1);
playersDataTable.setValueAt(players.get(i).getCountryName(), i, 2);
playersDataTable.setValueAt(players.get(i).getCareerSpan(), i, 3);
playersDataTable.setValueAt(players.get(i).getMatchesPlayed(), i, 4);
playersDataTable.setValueAt(players.get(i).getInningsPlayed(), i, 5);
playersDataTable.setValueAt(players.get(i).getBallsBowled(), i, 6);
playersDataTable.setValueAt(players.get(i).getRunsConceded(), i, 7);
playersDataTable.setValueAt(players.get(i).getWicketsTaken(), i, 8);
playersDataTable.setValueAt(players.get(i).getBowlingAverage(), i, 9);
playersDataTable.setValueAt(players.get(i).getEconomyRate(), i, 10);
playersDataTable.setValueAt(players.get(i).getStrikeRate(), i, 11);
playersDataTable.setValueAt(players.get(i).getFiveWicketsInnings(), i, 12);
}
}
编辑:这里有一些额外的代码可能会有所帮助。大部分代码是由 NetBeans 自动创建的 - 我已尝试进行一些自定义编辑。
scrolledPane 的代码
tableScrollPane = new javax.swing.JScrollPane();
tableScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
tableScrollPane.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
// Code of sub-components and layout - not shown here
// Code adding the component to the parent container - not shown here
JTable 的代码:
playersDataTable = new javax.swing.JTable();
playersDataTable.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(153, 204, 255), 2));
playersDataTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
//NOTE: There were 100 of these, but deleted some to truncate this code
{null, null, null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null, null, null}
},
new String [] {
"ID", "Player Name", "Country", "Career Span", "Matches Played", "Innings Played", "Balls Bowled", "Runs Conceded", "Wickets Taken", "Bowling Average", "Economy Rate", "Strike Rate", "5 Wickets/Innings"
}
) {
Class[] types = new Class [] {
java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Integer.class
};
boolean[] canEdit = new boolean [] {
false, false, false, false, false, false, false, false, false, false, false, false, false
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
playersDataTable.setPreferredSize(new java.awt.Dimension(700, 160));
playersDataTable.getTableHeader().setResizingAllowed(false);
playersDataTable.getTableHeader().setReorderingAllowed(false);
playersDataTable.setUpdateSelectionOnSort(false);
tableScrollPane.setViewportView(playersDataTable);
playersDataTable.getColumnModel().getColumn(0).setResizable(false);
playersDataTable.getColumnModel().getColumn(0).setPreferredWidth(20);
playersDataTable.getColumnModel().getColumn(1).setResizable(false);
playersDataTable.getColumnModel().getColumn(2).setResizable(false);
playersDataTable.getColumnModel().getColumn(3).setResizable(false);
playersDataTable.getColumnModel().getColumn(3).setPreferredWidth(50);
playersDataTable.getColumnModel().getColumn(4).setResizable(false);
playersDataTable.getColumnModel().getColumn(4).setPreferredWidth(60);
playersDataTable.getColumnModel().getColumn(5).setResizable(false);
playersDataTable.getColumnModel().getColumn(5).setPreferredWidth(60);
playersDataTable.getColumnModel().getColumn(6).setResizable(false);
playersDataTable.getColumnModel().getColumn(6).setPreferredWidth(50);
playersDataTable.getColumnModel().getColumn(7).setResizable(false);
playersDataTable.getColumnModel().getColumn(7).setPreferredWidth(60);
playersDataTable.getColumnModel().getColumn(8).setResizable(false);
playersDataTable.getColumnModel().getColumn(8).setPreferredWidth(50);
playersDataTable.getColumnModel().getColumn(9).setResizable(false);
playersDataTable.getColumnModel().getColumn(9).setPreferredWidth(55);
playersDataTable.getColumnModel().getColumn(10).setResizable(false);
playersDataTable.getColumnModel().getColumn(10).setPreferredWidth(50);
playersDataTable.getColumnModel().getColumn(11).setResizable(false);
playersDataTable.getColumnModel().getColumn(11).setPreferredWidth(35);
playersDataTable.getColumnModel().getColumn(12).setResizable(false);
按钮调用getAllPlayersBtn,players.size()等于500。
我在 StackOverFlow 上看到过与此类似的问题,但似乎无法按照我想要的方式进行调整。我正在使用 NetBeans GUI Builder 来构建程序的 GUI - 因为我是这个 GUI 构建器的新手,所以我不确定要在此处显示每个人的哪些代码(除了前面发布的代码片段)。如果需要更多代码,请告诉我具体需要什么。
我在 GitHub 上有我的项目 https://github.com/rattfieldnz/Java_Projects/tree/master/PCricketStats 供任何想要运行它并为我提供帮助的人使用。应用程序的主类是“AppInterfac.java”,JTable 在“Stats Involving Multiple Players”选项卡中。
这很可能与这个特定问题无关,但我想我会在这里简单地提及它......理想情况下,我希望有一个“下一个”按钮,显示 10 行,以及一个“上一个”按钮那会回到很多 10。我已经看到了很多不同的方法来做到这一点我很困惑,所以我希望有人看看我的特殊情况并指导我如何实现这些按钮。
【问题讨论】:
-
请尽快发布SSCCE 以获得更好的帮助,上午问题的演示,简短,可运行,可编译,只是 JFrame 与 JTable 和 JButton 导致上午 .... 编辑并将 JTable 放入 JScrollPane ,否则这里的一切都将(很可能)拍摄到黑暗
-
代码很混乱..贴一个可运行的代码
-
再次,请在代码上发布 SSCCE could be based,因为您的帖子与我在第一部分的歌曲非常相似
-
am wanting to show 100 rows (since there appears to be a limit of 100 rows- 没有 100 行的限制。那是您的 IDE 生成的糟糕代码的结果。使用DefaultTableModel作为表的模型。然后,您可以使用addRow(...)方法添加任意数量的行。也不需要下一个/上一个按钮。
标签: java swing jtable jbutton netbeans-7