0
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class GUI extends JFrame {

private JButton reg;
private JButton custom;
 private JButton custom2;
public GUI(){
    super("Heartstone Arena Alpha 0.01");
    setLayout(new FlowLayout());

    reg = new JButton("Click Me");
    add(reg);

    Icon ACn = new ImageIcon(getClass().getResource("463.png"));
    Icon ACg = new ImageIcon(getClass().getResource("463 (1).png"));
    custom = new JButton("Custom", ACn);
    custom.setRolloverIcon(ACg);
    add(custom);

    HandlerClass handler = new HandlerClass();
    reg.addActionListener(handler);
    custom.addActionListener(handler);


}

private class HandlerClass implements ActionListener {
    public void actionPerformed(ActionEvent event) {




        Icon An = new ImageIcon(getClass().getResource("Alexstrasza(303).png"));
        custom2 = new JButton(" ", An);
        custom2.setIcon(An);
        custom2.setRolloverIcon(An);





    }
}

Here is the code, what I want to do is have custom2 replace custom when it's clicked. How would I go on about this? I tried using custom = null and then add(custom2); but it doesn't show up PS: Ignore the reg button

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Nether
  • 1,081
  • 10
  • 14
  • 4
    You might use a single button with different listeners and text. But if that doesn't suit, look to [`CardLayout`](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) as seen in this [short example](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Jan 21 '14 at 10:04
  • remove custom/clean frame -> add custom 2 -> repaint frame – lebryant Jan 21 '14 at 10:07
  • @lebryant Don't forget to validate and or pack the frame or better still, just don't mention that hackish approach unless necessary (and with card layout - it is *not necessary*). – Andrew Thompson Jan 21 '14 at 16:59

1 Answers1

1

You need to add an ActionListener to your button, which should make the first button invisible and the second button visible. So it does not really get destroyed, you just dont show it.
Like this:

public class YourClassName implements ActionListener {

  private JButton button1;
  private JButton button2;

  public YourClassName() {

    // Code Snippet ----------------

    button1 = new JButton("Click to replace");
    button1.addActionListener(this);
    // implement code for resizing and positioning here

    button2 = new JButton("I am new here");
    button2.setVisible(false);
    // implement code for resizing and positioning here

    // ...

  }

  @Override
  public void actionPerformed(ActionEvent e) {
    if(e.getSource() == button1) {
      button1.setVisible(false);
      button2.setvisible(true);
    }
  }
}

Note: The code is made up out of my head, there may be a mistakes in it. Also, it is just a snippet, feel free to comment for full code

chvolkmann
  • 524
  • 2
  • 9