Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

java - Why is my table not visible when it's in a JScrollPane?

I've been trying for hours, different things and searching everywhere for a solution, but I can not get my table in addScoreCardUpper() to show up. I get an horizontal scrollbar, but no content, just 2 pixels worth of border.

import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

public class YahtzeeGUI extends JFrame{
    /**
     * 
     */
    private static final long serialVersionUID = 3255683022699487295L;
    private JFrame frame;
    private JPanel panel;
    private JPanel dicePanel;
    private JScrollPane scrollPane;
    private JButton btnRoll;
    private JButton[] btnDice = new JButton[5];
    private JTable table;

    private Yahtzee y = new Yahtzee();

    public YahtzeeGUI(){
        createWindow();
        addButtonRoll();
        addButtonDice();
        addScoreCardUpper();
        //addScoreCardLower();


        frame.add(panel);
        frame.setVisible(true);
    }

    public void createWindow(){
        frame = new JFrame();
        frame.setTitle("Yahtzee");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1000,700);
        frame.setVisible(true);
        panel = new JPanel(new BorderLayout());
        dicePanel = new JPanel();
        scrollPane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    }

    public void addButtonRoll(){
        btnRoll = new JButton ("Roll the Dice");
        btnRoll.addActionListener(new RollHandler());

        dicePanel.add (btnRoll);
        panel.add(dicePanel, BorderLayout.SOUTH);
    }

    public void addButtonDice(){

        for (int i = 0; i < btnDice.length; i++){
            btnDice[i] = new JButton(String.valueOf(y.dice[i].getFaceValue()));
            btnDice[i].addActionListener(new HoldHandler());
            dicePanel.add (btnDice[i]);
        }
        panel.add(dicePanel, BorderLayout.SOUTH);
    }

    public void addScoreCardUpper(){
        String tableHeader[] = {"Upper Section", "How to score", "Score" };
        String tableValues[][] = {
                {"ONES", "Total of all Ones",""},
                {"TWOS", "Total of all Twos",""},
                {"THREES", "Total of all Threes",""},
                {"FOURS", "Total of all Fours",""},
                {"FIVES", "Total of all Fives",""},
                {"SIXES", "Total of all Sixes",""},
                {"TOTAL SCORE", "",""},
                {"BONUS", "",""},
                {"TOTAL + BONUS", "",""}
        };

        table = new JTable(tableValues, tableHeader);
        table.setEnabled(false);
        setColumnWidths();
        scrollPane.add(table); // Here is the offender
        panel.add(scrollPane, BorderLayout.EAST);
    }

    /*public void addScoreCardLower(){
        String tableHeader[] = {"Lower S", "How to score", "Score" };
        String tableValues[][] = {
                {"ONES", "Total of all Ones",""},
                {"TWOS", "Total of all Twos",""},
                {"THREES", "Total of all Threes",""},
                {"FOURS", "Total of all Fours",""},
                {"FIVES", "Total of all Fives",""},
                {"SIXES", "Total of all Sixes",""},
                {"TOTAL SCORE", "",""},
                {"BONUS", "",""},
                {"TOTAL + BONUS", "",""}
        };

        table = new JTable(tableValues, tableHeader);
        table.setEnabled(false);
        setColumnWidths();
        scoreSubPanel.add(table);
        panel.add(scoreSubPanel, BorderLayout.WEST);
    }*/

    public void setColumnWidths(){

        TableColumn a = table.getColumnModel().getColumn(0);
        TableColumn b = table.getColumnModel().getColumn(1);
        a.setPreferredWidth(100);
        b.setPreferredWidth(100);

    }

    class RollHandler implements ActionListener {

        public void actionPerformed(ActionEvent event) {

            for(int i = 0; i < y.dice.length; i++){
                if (y.dice[i].getHoldState() != true){
                    y.dice[i].roll();
                    btnDice[i].setText(String.valueOf(y.dice[i].getFaceValue()));
                }
            }
        }
    }

    class HoldHandler implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            for (int i = 0; i < btnDice.length; i++){
                if (event.getSource()== btnDice[i]){ // Picks the pressed button after running through them all.
                    if (y.dice[i].getHoldState() == false){
                        y.dice[i].setHoldState(true);
                    } else if (y.dice[i].getHoldState() == true){
                        y.dice[i].setHoldState(false);
                    }
                }
            }
        }
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is your problem:

scrollPane.add(table);

This does not add the JTable to the JScrollPane's viewport's view which is where you want it, but rather completely replaces the JScrollPane's viewport with the JTable, making the JScrollPane completely nonfunctional. Instead set the table as the JScrollPane's viewportview:

scrollPane.setViewportView(table);

Do either this or pass the table into the JScrollPane's constructor which does pretty much the same thing:

JScrollPane scrollPane = new JScrollPane(table,
          ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
          ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

This is all well explained in the JScrollPane API, and you will want to give it a look for the important details.


Edit
Your code also calls setVisible(true) on the JFrame before adding all components which can lead to trouble. You'll want to avoid doing this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...