0

I'm not sure what title to give this question, so hopefully no one is too confused...

I have a button that, when clicked, adds attributes of each "Player" object from an ArrayList of Player objects to the JTable. At the moment, there are 500 "Players" in my ArrayList and am wanting to show 100 rows (since there appears to be a limit of 100 rows - if I knew how to do buttons for next/previous 10 rows, I'd do that). When go to the appropriate tab in the JTabbedPane and click on the button, I only see 10 rows of data - I would like to see 100 rows of data and scroll down to see the rest of them. I have put the JTable inside a JScrolledPane.

This is the code in my button's 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);

            }
    } 

EDIT: Here is some extra code which may be helpful. Most of this code was automatically created by NetBeans - I have attempted to make some custom edits.

The code for my 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

Code for the 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);

The button is called getAllPlayersBtn, and the players.size() is equal to 500.

I have seen similar questions to this on StackOverFlow, but can't seem to get adjustments working the way I want. I am using the NetBeans GUI Builder to build the GUI for the program - since I am new to this GUI builder, I am not sure which code to show everyone on here (apart from the code fragment posted earlier). If more code is needed, please tell me what exactly is needed.

I have my project on GitHub at https://github.com/rattfieldnz/Java_Projects/tree/master/PCricketStats for anyone that wants to run it and provide me with some help. The main class of the application is "AppInterfac.java", and the JTable is in the "Stats Involving Multiple Players" tab.


This is most likely unrelated to this particular problem, but I thought I would briefly mention it here... Ideally I would want to have a "Next" button that shows rows in lots of 10, and a "Previous" button that would go back in lots of 10. I've seen so many different ways to do this I am confused, so I would like someone to have a look at my particular case and guide me on how to implement these buttons.

Rob
  • 1,272
  • 6
  • 24
  • 35
  • please for better help sooner post an [SSCCE](http://sscce.org/), demonstrations of a.m. issue, short, runnable, compilable, just about JFrame with JTable and JButton caused a.m. .... EDIT and to put JTable to JScrollPane, otherwise everything here will be (probalby) shots to the dark – mKorbel May 23 '13 at 10:35
  • 1
    Code is very confusing .. post a runnable code – Amarnath May 23 '13 at 11:35
  • again, please post an SSCCE, [could be based](http://stackoverflow.com/a/16664124/714968), on code, because your post is very similair to my song from 1st part of – mKorbel May 23 '13 at 11:51
  • `am wanting to show 100 rows (since there appears to be a limit of 100 rows` - there is not a limit of 100 rows. That is the result of terrible code generated by your IDE. Use a `DefaultTableModel` as the model for your table. Then you can just use the `addRow(...)` method to add as many rows as you want. No need for Next/Previous buttons either. – camickr May 23 '13 at 14:55

1 Answers1

1

Instead of setting the cell values via the table, you should add the rows via the model.

You have two choices. You could create a new model, add all the new rows to it and apply it to the table when you're done.

You could get the model from the table, clear it and add all the new rows.

This will depend on the underlying table model

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366