Large gap between JTables

↘锁芯ラ 提交于 2019-11-26 23:42:19

问题


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:

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


回答1:


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.



来源:https://stackoverflow.com/questions/17416020/large-gap-between-jtables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!