1

The below is on the way to providing the results I am looking for, but unfortunately when this displays there is a large gap between tables on each page. It is so that I can only fit two of them on the screen per tab (with just two rows per table).

private void display() {
    JFrame f = new JFrame("Test");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new JLabel("Test"));
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

class ResultsPanel extends JPanel implements ActionListener {

    private ArrayList<Venue> venueList;
    private Timer timer;
    QueueItem[] queue;
    Reporter reporter;
    private JPanel resultsPane1;
    private JPanel resultsPane2;
    private JTable[] table1;
    private JTable[] table2;
    private int totalItems;
    public String[] page1ColumnNames = {
        "Name",
        "Wins",
        "Losses",
        "Draws",
        "Win %",
        "Avg Turns"};
    public String[] page2ColumnNames = {
        "Name",
        "H%Total",
        "H%inRange",
        "KO/F rcvd",
        "KO/F Infl",
        "HChances",
        "HitsTotal",
        "Wounds Inflicted",
        "Wounds Received"};
    public String[][] page1Data = new String[2][6];
    public String[][] page2Data = new String[2][9];

    public void initResults() {

        timer = new Timer(500, this);
        timer.start();

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object src = e.getSource();

        try {
            Thread.sleep(1);
        } catch (InterruptedException d) {
            System.err.println("Caught : InterruptedException" + d.getMessage());
        }

        repaint();
    }

    public ResultsPanel(QueueItem[] queueItems) {
        super();
        totalItems = 0;
        queue = queueItems;


        for (QueueItem a : queue) {
            if (a != null) {
                totalItems++;
            }
        }

        JTabbedPane tabbedPane = new JTabbedPane();

        resultsPane1 = new JPanel();
        resultsPane2 = new JPanel();
        resultsPane1.setLayout(new BoxLayout(resultsPane1, BoxLayout.PAGE_AXIS));
        resultsPane2.setLayout(new BoxLayout(resultsPane2, BoxLayout.PAGE_AXIS));
        table1 = new JTable[totalItems];
        table2 = new JTable[totalItems];
        for (int b = 0; b < totalItems; b++) {
            reporter = queue[b].reporter;
            generateData();
            table1[b] = new JTable(page1Data, page1ColumnNames);
            table2[b] = new JTable(page2Data, page2ColumnNames);
            resultsPane1.add(new JScrollPane(table1[b]));
            resultsPane2.add(new JScrollPane(table2[b]));
        }



        JScrollPane scrollPane1 = new JScrollPane(resultsPane1);
        JScrollPane scrollPane2 = new JScrollPane(resultsPane2);
        tabbedPane.addTab("Win/Loss", scrollPane1);
        tabbedPane.addTab("Overall", scrollPane2);
        add(tabbedPane);

    }

    public void generateData() {
        double temp = 0;
        double avgTurns;
        double total;

        for (Integer a : reporter.numberOfTurns) {
            temp = temp + a;
        }
        avgTurns = 1.0 * temp / reporter.numberOfTurns.size();

        page1Data[0][0] = reporter.glad1.name;
        page1Data[0][1] = "" + reporter.gladTotalWins1;
        page1Data[0][2] = "" + reporter.gladTotalLosses1;
        page1Data[0][3] = "" + reporter.gladTotalDraws1;
        temp = (int) ((reporter.gladTotalWins1 * 100.0) / (reporter.gladTotalWins1 + reporter.gladTotalLosses1 + reporter.gladTotalDraws1));
        page1Data[0][4] = "" + temp + "%";
        page1Data[0][5] = "" + avgTurns;
        page1Data[1][0] = reporter.glad2.name;
        page1Data[1][1] = "" + reporter.gladTotalWins2;
        page1Data[1][2] = "" + reporter.gladTotalLosses2;
        page1Data[0][3] = "" + reporter.gladTotalDraws2;
        temp = (int) ((reporter.gladTotalWins2 * 100.0) / (reporter.gladTotalWins2 + reporter.gladTotalLosses2 + reporter.gladTotalDraws2));
        page1Data[1][4] = "" + temp + "%";
        page1Data[1][5] = "" + avgTurns;

        page2Data[0][0] = reporter.glad1.name;

        total = 0;
        for (double a : reporter.hitChancePercentOut1) {
            total = total + a;
        }
        temp = 1.0 * total / reporter.hitChancePercentOut1.size();
        page2Data[0][1] = "" + temp;

        total = 0;
        for (double a : reporter.hitChancePercentIn1) {
            total = total + a;
        }
        temp = 1.0 * total / reporter.hitChancePercentIn1.size();
        page2Data[0][2] = "" + temp;



        page2Data[1][0] = reporter.glad2.name;

        total = 0;
        for (double a : reporter.hitChancePercentOut2) {
            total = total + a;
        }
        temp = 1.0 * total / reporter.hitChancePercentOut2.size();
        page2Data[1][1] = "" + temp;

        total = 0;
        for (double a : reporter.hitChancePercentIn2) {
            total = total + a;
        }
        temp = 1.0 * total / reporter.hitChancePercentIn2.size();
        page2Data[1][2] = "" + temp;



        int deadCount2 = 0;
        int KOCount2 = 0;
        for (String a : reporter.endStatus2) {
            if (a.equals("DEAD")) {
                deadCount2++;
            } else if (a.equals("KO")) {
                KOCount2++;
            }

        }
        int deadCount1 = 0;
        int KOCount1 = 0;
        for (String a : reporter.endStatus1) {
            if (a.equals("DEAD")) {
                deadCount1++;
            } else if (a.equals("KO")) {
                KOCount1++;
            }

        }
        page2Data[0][3] = "" + KOCount1 + "/" + deadCount1;
        page2Data[1][3] = "" + KOCount2 + "/" + deadCount2;
        page2Data[0][4] = "" + KOCount2 + "/" + deadCount2;
        page2Data[1][4] = "" + KOCount1 + "/" + deadCount1;

        total = 0;
        for (Integer a : reporter.hitChances1) {
            total = total + a;
        }
        page2Data[0][5] = "" + total;
        total = 0;
        for (Integer a : reporter.hitTotals1) {
            total = total + a;
        }
        page2Data[0][6] = "" + total;
        total = 0;
        for (Integer a : reporter.woundsInflicted1) {
            total = total + a;
        }
        page2Data[0][7] = "" + total;
        total = 0;
        for (Integer a : reporter.woundsReceived1) {
            total = total + a;
        }
        page2Data[0][8] = "" + total;
        total = 0;

        total = 0;
        for (Integer a : reporter.hitChances2) {
            total = total + a;
        }
        page2Data[1][5] = "" + total;
        total = 0;
        for (Integer a : reporter.hitTotals2) {
            total = total + a;
        }
        page2Data[1][6] = "" + total;
        total = 0;
        for (Integer a : reporter.woundsInflicted2) {
            total = total + a;
        }
        page2Data[1][7] = "" + total;
        total = 0;
        for (Integer a : reporter.woundsReceived2) {
            total = total + a;
        }
        page2Data[1][8] = "" + total;
        total = 0;

    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
    }
}

How would I go about fixing this? Thanks

Edit: Edited to add full code per request... most of it is irrelevant to what is going on here. Also removed setPreferredSize, but still same result.

Edit 2:

enter image description here

FYI, the blank spot in "Draws" is unrelated.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Peter F
  • 435
  • 1
  • 8
  • 17
  • you should show a SSCCE (not a ton of code which even you evaluate as unrelated ;-) Anyway, @trashgod is spot on, it's the rather arbitrary size that JTable.getPreferredScrollableViewportSize returns: override and let it return some reasonable value depending on the rowCount – kleopatra Jul 02 '13 at 08:03
  • `Venue`? `QueueItem`? `Reporter`? – trashgod Jul 02 '13 at 11:14

1 Answers1

3

Absent a complete example, it's hard to be sure what you're seeing. Given that one should avoid setPreferredSize(), note that JTable implements Scrollable and can calculate it's own preferred size. Unfortunately, the arbitrary Dimension calculated may be causing the problem. Overriding getPreferredScrollableViewportSize() to return a multiple of getRowHeight(), for example, may be a useful alternative.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    +1 for hitting the nail spot on - the getPrefScrollable _is_ a nuisance, btw: JXTable does exactly that rowCount relative calculation. Note to myself: not yet optimal - should allow to optinally take actual rows up to a maximum into account) – kleopatra Jul 02 '13 at 08:06
  • It seems the consensus is that I need to do the getPreferredScrollableViewportSize() override. Marking as accepted. – Peter F Jul 02 '13 at 12:09
  • You may also want to look at the result returned by `BasicTableUI#getPreferredSize()`. – trashgod Jul 02 '13 at 14:53