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