1

the mission here is to create JButtons from a String in ActionListener, but we need a way to refresh the GUI panel so it know that there is now variable for the button creater in the GUI. i have a feeling that the button creater must be in ActionListener and there is a command like repaint() or removeAll that is missing inside the ActionListener.

public JButton[] turneringer = null;
JButton AntallTurneringer = new JButton("number of buttons");

JMenuBar meny = new JMenuBar();
JMenu fil = new JMenu("somthing");
JMenuItem angre = new JMenuItem("deleate on button");
JMenuItem angre2 = new JMenuItem("deleate all buttons");

int d;
int i;

public GUI(){
    this.setTitle("somthing");
    this.setSize(500,500);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout());

    this.setJMenuBar(meny);
    meny.add(fil);
    fil.add(angre2);
    fil.add(angre);
    angre2.addActionListener(this);
    angre.addActionListener(this);

    AntallTurneringer.addActionListener(this);
    this.add(AntallTurneringer);
    AntallTurneringer.setVisible(true);

    if(d > 0){
    turneringer = new JButton[d];
    for(i = 0; i < d; i++){
        turneringer[d] = new JButton();
        turneringer[d].addActionListener(this);
        turneringer[d].setText("Turnering "+(i+1));
        turneringer[d].setVisible(true);
        this.add(turneringer[d]);
    }}
    this.setVisible(true);

}

@Override
public void actionPerformed(ActionEvent arg0) {
    if(arg0.getSource().equals(AntallTurneringer)){
        String tu = JOptionPane.showInputDialog(null, "number of buttons");
        d = Integer.parseInt(tu);

    }
}
elektronet
  • 108
  • 13

2 Answers2

1

You could use a separate panel for the buttons. would simplify the whole thing.

private JPanel buttonPnl;

public void actionPerformed(ActionEvent e){
    buttonPnl.invalidate();

    buttonPnl.clear();

    //create the new buttons

    buttonPnl.validate();
    buttonPnl.repaint();
}    
  • i added some Jpanel. but when i used the command panel2.add(turneringer[d]); the program start crashing. – elektronet Apr 21 '15 at 21:02
  • have you removed all buttons before? adding a button twice might be the reason for this issue. btw. `buttonPnl.clear()` should actually be `buttonPnl.removeAll()`. –  Apr 21 '15 at 21:04
0

I have solved it, it were no need for repainting or clearing anything. the first problem in the code were that i had used [d] inside the array, and not[i]. the second problem were the placement of the for loop. this is the working code under.

public JButton[] turneringer = null;
JButton AntallTurneringer = new JButton("Velg antall turneringer");

JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();

int d;

public GUI(){
    this.setTitle("Squash Turnering");
    this.setLayout(new GridLayout());
    this.setSize(500,500);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    panel1.setBackground(Color.BLACK);
    panel2.setBackground(Color.RED);

    AntallTurneringer.addActionListener(this);
    AntallTurneringer.setVisible(true);

    panel1.add(AntallTurneringer);
    add(panel1);

    add(panel2);
    panel2.setVisible(false);

    this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
    if(arg0.getSource().equals(AntallTurneringer)){
        String tu = JOptionPane.showInputDialog(null, "number of buttons you want to add");
        d = Integer.parseInt(tu);

        turneringer = new JButton[d];
        for(int i = 0; i < d; i++){
            turneringer[i] = new JButton();
            turneringer[i].addActionListener(this);
            turneringer[i].setText("Turnering "+(i+1));
            turneringer[i].setVisible(true);
            turneringer[i].setSize(100, 100);
            panel2.add(turneringer[i]);
        }
        panel1.setVisible(false);
        panel2.setVisible(true);
    }
}}
elektronet
  • 108
  • 13