0

Okay, so I am attempting to make a Flash cards program, and I want it to be that when the Add Set (or new deck of cards) is clicked, it will add a button to represent the set at the bottom of the frame.

What I have now will only show the button after the frame is resized. I don't really know what I'm doing with the panel, as I haven't figured out how to properly lay out all the parts yet.

I need the program to show the set icon in the bottom panel after the set it made.

import javax.imageio.ImageIO;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class GraphicsUI extends JPanel {

private DeckList setsList;
CardActions action = new CardActions();

JButton addSetButton, addCardButton;

private JLabel label;
private JPanel bottomPanel;

public GraphicsUI(){
    this.setPreferredSize(new Dimension(1000,825));
    this.setBackground(Color.LIGHT_GRAY);




    this.label = new JLabel();
    label.setOpaque(true);
    label.setBackground(Color.white);
    label.setPreferredSize(new Dimension(600, 400));



    ImageIcon img = new ImageIcon("setIcon.png");       
    //make that set image at bottom



this.addSetButton = new JButton("Add Set");
//      this.addSetButton.setBackground(Color.white);
//      this.addSetButton.setPreferredSize(new Dimension(240, 180));
        this.add(addSetButton, BorderLayout.WEST);
        this.addSetButton.addActionListener(new ButtonListener());


    this.addCardButton = new JButton("Add Card");
//  this.addCardButton

    this.add(label);

    JLabel blah = new JLabel();
    blah.setPreferredSize(new Dimension(1000,30));
    this.add(blah);



    this.bottomPanel = new JPanel();

    this.bottomPanel.setPreferredSize(new Dimension(1000, 400));
    this.add(bottomPanel);





}


public class ButtonListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        action.setCommand(CardActions.Command.ADDSET);

        action.setList(getSetInfo());


    }

}

private CardList getSetInfo(){
    CardList cl = new CardList();
    String setName = JOptionPane.showInputDialog("Enter the name of your set.");

    if(setName.isEmpty()){
        JOptionPane.showMessageDialog(this, "Cannot have an empty set.");
    }

        cl.setSetName(setName);
        ImageIcon img = new ImageIcon("setIcon.png");
        bottomPanel.add(new JButton(img));
            return cl;

}

}
user3113376
  • 161
  • 2
  • 14
  • 1) For better help sooner, post a [MCTaRE](http://stackoverflow.com/help/mcve) (Minimal Complete Tested and Readable Example). 2) One way to get image(s) for an example is to hot-link to the images seen in [this answer](http://stackoverflow.com/a/19209651/418556). – Andrew Thompson Feb 24 '14 at 01:54

2 Answers2

1

Once you've added the button, try calling revalidate on the container to update the container hierarchy.

You may also find that the use of setPreferredSize may not be leaving enough room for the new button to appear

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

you should make a new jpanel that contains only buttons that will bring you to the specific set that the user has created. to update the jpanel just call repaint()

Louis B
  • 342
  • 1
  • 5
  • 21
  • I have a new Jpanel where I want to buttons to go, and I've tried doing the repaint, but they don't work. – user3113376 Feb 24 '14 at 01:57
  • *"..just call repaint()"* There is more to it than that. See [this answer](http://stackoverflow.com/a/5630271/418556) for a working example of adding new labels. – Andrew Thompson Feb 24 '14 at 01:57