Austin
Austin

Reputation: 396

JTextArea trouble

I am making a poker game and I have hit an issue, almost everything works up to the deal button's actionListener. It is supposed to remove the deal button and add a new JTextArea (this text is just a placeholder). Almost everything works until that point, but in the submit button actionListener it won't remove the inputText JTextArea (instead I just setVisible to false). How can I add another textArea and have it show up on the screen?

private ArrayList<String> deck;
private Button submit;
private Button deal;
private Font buttonFont;
private Font textFont;
private JFrame framePoker;
private JPanel masterPanel;
private JPanel cardPanel;
private JPanel textPanel;
private JPanel buttonPanel;
private Checkbox chip100;
private Checkbox chip500;
private Checkbox chip900;
private Checkbox op1;
private Checkbox op2;
private Checkbox op3;
private CheckboxGroup opponentInput;
private CheckboxGroup chipInput;
private Container cont;
private SpringLayout layout;
private JTextArea inputText;
private JTextArea test;

// initialize frame contents
private void initialize() throws IOException 
{
    // set defaults
    layout = new SpringLayout();
    masterPanel = new JPanel(layout);
    textPanel = new JPanel(layout);
    buttonPanel = new JPanel(layout);
    cardPanel = new JPanel(layout);
    opponentInput = new CheckboxGroup();
    chipInput = new CheckboxGroup();
    chip100 = new Checkbox("100", chipInput, false);
    chip500 = new Checkbox("500", chipInput, true);
    chip900 = new Checkbox("900", chipInput, false);
    op1 = new Checkbox("1", opponentInput, true);
    op2 = new Checkbox("2", opponentInput, false);
    op3 = new Checkbox("3", opponentInput, false);
    buttonFont = new Font("TimesRoman", Font.BOLD, 46);
    textFont = new Font("TimesRoman", Font.PLAIN, 32);
    submit = new Button("SUBMIT");
    inputText = new JTextArea("How many chips?       How many opponents?");
    deal = new Button("DEAL");
    deck = new ArrayList<>(52);

    fillDeck();

    // set up frame (window)
    framePoker = new JFrame();
    framePoker.setTitle("SWING Poker - Texas Hold 'Em");
    framePoker.setBounds(0, 0, 1200, 650);
    framePoker.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    framePoker.setBackground(Color.GREEN);


    try {
    framePoker.setContentPane(new JLabel(
            new ImageIcon(ImageIO.read(new File(
            "C:/Users/Austin/Documents/custom made codes/random programs/Poker/src/background.png")))));
    } catch (IOException e) {
        e.printStackTrace();
    }

    framePoker.setResizable(false);
    framePoker.pack();
    framePoker.setLayout(layout);

    cont = framePoker.getContentPane();

    // setup and add master panel
    masterPanel.setPreferredSize(framePoker.getPreferredSize());
    masterPanel.setOpaque(false);
    cardPanel.setPreferredSize(framePoker.getPreferredSize());
    cardPanel.setOpaque(false);
    textPanel.setPreferredSize(framePoker.getPreferredSize());
    textPanel.setOpaque(false);
    buttonPanel.setPreferredSize(framePoker.getPreferredSize());
    buttonPanel.setOpaque(false);

    framePoker.add(masterPanel);

    // set font for buttons
    chip100.setFont(buttonFont);
    chip500.setFont(buttonFont);
    chip900.setFont(buttonFont);
    op1.setFont(buttonFont);
    op2.setFont(buttonFont);
    op3.setFont(buttonFont);
    submit.setFont(buttonFont);
    deal.setFont(buttonFont);
    inputText.setFont(textFont);
    inputText.setOpaque(false);

    // display beginning buttons
    buttonPanel.add(submit);

    buttonPanel.add(chip100);
    buttonPanel.add(chip500);
    buttonPanel.add(chip900);

    buttonPanel.add(op1);
    buttonPanel.add(op2);
    buttonPanel.add(op3);

    masterPanel.add(buttonPanel);

    textPanel.add(inputText);
    masterPanel.add(textPanel);

    layout.putConstraint(SpringLayout.WEST, submit, (framePoker.getWidth() / 2) - 100, SpringLayout.WEST, cont);
    layout.putConstraint(SpringLayout.NORTH, submit, 500, SpringLayout.NORTH, cont);

    layout.putConstraint(SpringLayout.WEST, chip100, framePoker.getWidth() / 3, SpringLayout.WEST, cont);
    layout.putConstraint(SpringLayout.NORTH, chip100, (framePoker.getHeight() / 2) - 150, SpringLayout.NORTH, cont);
    layout.putConstraint(SpringLayout.WEST, chip500, framePoker.getWidth() / 3, SpringLayout.WEST, cont);
    layout.putConstraint(SpringLayout.NORTH, chip500, (framePoker.getHeight() / 2) - 50, SpringLayout.NORTH, cont);
    layout.putConstraint(SpringLayout.WEST, chip900, framePoker.getWidth() / 3, SpringLayout.WEST, cont);
    layout.putConstraint(SpringLayout.NORTH, chip900, (framePoker.getHeight() / 2) + 50, SpringLayout.NORTH, cont);

    layout.putConstraint(SpringLayout.WEST, op1, ((framePoker.getWidth() / 3) * 2) - 75, SpringLayout.WEST, cont);
    layout.putConstraint(SpringLayout.NORTH, op1, (framePoker.getHeight() / 2) - 150, SpringLayout.NORTH, cont);
    layout.putConstraint(SpringLayout.WEST, op2, ((framePoker.getWidth() / 3) * 2) - 75, SpringLayout.WEST, cont);
    layout.putConstraint(SpringLayout.NORTH, op2, (framePoker.getHeight() / 2) - 50, SpringLayout.NORTH, cont);
    layout.putConstraint(SpringLayout.WEST, op3, ((framePoker.getWidth() / 3) * 2) - 75, SpringLayout.WEST, cont);
    layout.putConstraint(SpringLayout.NORTH, op3, (framePoker.getHeight() / 2) + 50, SpringLayout.NORTH, cont);

    layout.putConstraint(SpringLayout.WEST, inputText, 325, SpringLayout.WEST, cont);
    layout.putConstraint(SpringLayout.NORTH, inputText, 140, SpringLayout.NORTH, cont);

    // action listener for the submit button
    submit.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) 
        {
            startChips = Integer.parseInt(chipInput.getSelectedCheckbox().getLabel());
            numPlayers = Integer.parseInt(opponentInput.getSelectedCheckbox().getLabel()) + 1;
            chipText = new JTextArea[numPlayers * 2];

            // remove beginning buttons
            buttonPanel.remove(submit);
            buttonPanel.remove(chip100);
            buttonPanel.remove(chip500);
            buttonPanel.remove(chip900);
            buttonPanel.remove(op1);
            buttonPanel.remove(op2);
            buttonPanel.remove(op3);
            inputText.setVisible(false);    // still refuses to remove

            // display deal button
            framePoker.add(deal);

            layout.putConstraint(SpringLayout.WEST, deal, (framePoker.getWidth() / 2) - 75, SpringLayout.WEST, cont);
            layout.putConstraint(SpringLayout.NORTH, deal, 415, SpringLayout.NORTH, cont);
        }
    });

    // action listener for deal button
    deal.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent arg0) 
        {
            framePoker.remove(deal);

            test = new JTextArea("tessting");
            test.setVisible(true);

            textPanel.add(test);
            masterPanel.add(textPanel);
        }
    });
}

Upvotes: 0

Views: 66

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

Layouts are lazy, when you add or remove components, the container is not immediately validated (which would cause the layout hierarchy to be updated), this is done for performance reasons.

So once you're satisfied that the UI is ready (you've finished adding/removing stuff), you should call revalidate and repaint on the container(s) you modified

Don't mix heavyweight (AWT) components with light weight (Swing) components, this is going to cause no end of issues. If you're not aware, Button and CheckBox are heavyweight components. See How to Use Buttons, Check Boxes, and Radio Buttons for alternatives

Upvotes: 1

Related Questions