Replacing a button with another button - java

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

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

Related

JButton won't show text

This class represents the button panel of a UI I have created, the second JButton named 'btnNext' doesn't display text however the first JButton does, why is this?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
public class ButtonPanel extends JPanel {
private MainPanel mainPanel;
// Buttons
private JButton btnRunTheAlgorithm = new JButton("Run the Algorithm");
public static JButton btnNext = new JButton("Next Step");
public ButtonPanel(MainPanel mainPanel) {
this.mainPanel = mainPanel;
this.setLayout(new FlowLayout());
this.add(btnRunTheAlgorithm);
this.add(btnNext);
this.btnRunTheAlgorithm.addActionListener(e -> {
Algorithm main = new Algorithm(mainPanel);
main.Run();
});
this.btnNext.setAction(new AbstractAction() {
public void actionPerformed(ActionEvent ae) {
synchronized (btnNext) {
btnNext.notify();
}
}
});
}
}
The buttons displays to the panel as on the image below:
Buttons:
I'm also not sure the AbstractAction works, so I suppose that could be the cause of the text not displaying but I have no idea why if that is the case.

How to make a TextField visible when a button is clicked in Java?

I'm trying to create a GUI program which provides a user with a button. When this button is clicked, it displays a textField and prompts user to enter a regular expression.
Here are the steps I've taken so far to implement this:
Created a JFrame and added a button, label and text field.
Set the text field visibility to "false" initially. Label and button
visibility are set to "true"
Implemented the ActionListener interface and overrode the ActionPerformed method to change the textField visibility to "true" when the button is clicked.
Registered the ActionListener instance to the button.
When I run it this way, the text field does NOT become visible after clicking the button (it compiles fine, but nothing happens in the GUI)
However, if I change the label visibility to "false" initially, then add an action to set to "true" in the ActionListener, it works and BOTH the label and the textfield become visible on clicking the button.
What I want to know is why the textfield only becomes visible when there is a label included in the ActionListener?
In other words, why does this code NOT work?
import java.awt.FlowLayout;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class RegularExpressionGui extends JFrame {
private final JLabel label1; //label
private final JTextField textField1;
private JTextField textField2; //text field
private final JButton button1; //button
private final JTextArea textArea1; //textArea
public RegularExpressionGui() {
super("Regular Expression Lookup");
setLayout(new FlowLayout()); //GUI Layout
//add the components
label1 = new JLabel("Regular Expression");
label1.setVisible(true);
add(label1);
textField1 = new JTextField("Enter Regular Expression here");
textField1.setVisible(false);
add(textField1);
button1 = new JButton("Lookup Expression");
add(button1);
textArea1 = new JTextArea("Results");
add(new JScrollPane(textArea1)); //makes the output area scrollable
LookupHandler lookup = new LookupHandler();
button1.addActionListener(lookup);
textField1.addActionListener(lookup);
} //end constructor
//inner class containing ActionListner
private class LookupHandler implements ActionListener {
#Override
public void actionPerformed(ActionEvent event) {
if (event.getSource() == button1) {
textField1.setVisible(true);
}
}
}
}
And why does this one work instead?
import java.awt.FlowLayout;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class RegularExpressionGui extends JFrame {
private final JLabel label1; //label
private final JTextField textField1;
private JTextField textField2; //text field
private final JButton button1; //button
private final JTextArea textArea1; //textArea
public RegularExpressionGui() {
super("Regular Expression Lookup");
setLayout(new FlowLayout()); //GUI Layout
//add the components
label1 = new JLabel("Regular Expression");
label1.setVisible(false);
add(label1);
textField1 = new JTextField("Enter Regular Expression here");
textField1.setVisible(false);
add(textField1);
button1 = new JButton("Lookup Expression");
add(button1);
textArea1 = new JTextArea("Results");
add(new JScrollPane(textArea1)); //makes the output area scrollable
LookupHandler lookup = new LookupHandler();
button1.addActionListener(lookup);
textField1.addActionListener(lookup);
} //end constructor
//inner class containing ActionListner
private class LookupHandler implements ActionListener {
#Override
public void actionPerformed(ActionEvent event) {
if (event.getSource() == button1) {
textField1.setVisible(true);
label1.setVisible(true);
}
}
}
}
I'm not 100% sure but I think you don't need the inner class.
For your problem, if you remove the inner class and instead you implement ActionListener on your RegularExpressionGui class, then add pack() inside the actionPerformed it should work, because pack() should be calling to the repaint method. If you call your 1st code, and resize the window, you should now see it, because while resizing it, the Layout Manager is calling the repaint method.
So, your class should look like this one right now:
import java.awt.FlowLayout;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class RegularExpressionGui extends JFrame implements ActionListener {
private final JLabel label1; //label
private final JTextField textField1;
private JTextField textField2; //text field
private final JButton button1; //button
private final JTextArea textArea1; //textArea
public RegularExpressionGui() {
super("Regular Expression Lookup");
setLayout(new FlowLayout()); //GUI Layout
//add the components
label1 = new JLabel("Regular Expression");
label1.setVisible(true);
add(label1);
textField1 = new JTextField("Enter Regular Expression here");
textField1.setVisible(false);
add(textField1);
button1 = new JButton("Lookup Expression");
add(button1);
textArea1 = new JTextArea("Results");
add(new JScrollPane(textArea1)); //makes the output area scrollable
//LookupHandler lookup = new LookupHandler();
button1.addActionListener(this);
textField1.addActionListener(this);
pack();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
} //end constructor
//inner class containing ActionListner
#Override
public void actionPerformed(ActionEvent event) {
if (event.getSource() == button1) {
textField1.setVisible(true);
//label1.setVisible(true);
pack();
}
}
public static void main (String args[]) {
new RegularExpressionGui();
}
}

Java applet layout and action handler issue

In my application ,When the user clicks the JButton, display a JLabel that prompts the user to enter an integer, a JTextField into which the user can type the integer, and a secondJButton that contains the text Double me. When the user clicks the second button, the integer is doubled and the answer is displayed in the JTextField.
I am not able to display second button and text fields, when i click first button...please help
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class JDouble extends JApplet implements ActionListener {
private Container container = getContentPane();
/**
* The JButton.
*/
private JButton button = new JButton("Click me");
private JButton button2 = new JButton("Double me");
/**
* The JLabel.
*/
private JLabel label = new JLabel("Enter Integer");
private JTextField textfield = new JTextField(4);
private JTextField textfield2 = new JTextField(4);
public void init() {
// set the layout to FlowLayout
container.setLayout(new FlowLayout());
// register the 'this' action listener for the button
button.addActionListener(this);
container.add(button);
}
public void init1(){
container.setLayout(new FlowLayout());
container.add(textfield);
container.add(button2);
container.add(textfield2);
button2.addActionListener(this);
}
public void actionPerformed(ActionEvent actionEvent) {
container.add(label);
}
public void actionPerformed1(ActionEvent actionEvent) {
String me = textfield.getText();
int computation = Integer.parseInt(me);
computation = computation*2;
String changecomputation = Integer.toString(computation);
textfield2.setText(changecomputation);
container.remove(button);
container.add(label);
repaint();
validate();
}
}
Your init() method:
public void init() {
// set the layout to FlowLayout
container.setLayout(new FlowLayout());
// register the 'this' action listener for the button
button.addActionListener(this);
container.add(button);
}
We'll use this function to show the other fields when 'Click Me' is clicked...
private showInputFields(){
container.add(textfield);
button2.addActionListener(this);
container.add(button2);
container.add(textfield2);
}
Let's fix your action listener. If you want 'Click Me" button displayed on startup, init() takes care of that. When user clicks 'Click Me', we invoke showInputFields() to display the other components; we handle 'Double me' click using the same listener, we just check event source to handle appropriately...
private boolean inputFieldsDisplayed;
public void actionPerformed(ActionEvent actionEvent) {
if( actionEvent.getSource() == button && !inputFieldsDisplayed){
showInputFields();
inputFieldsDisplayed = true;
} else if ( actionEvent.getSource() == button2){
String me = textfield.getText();
int computation = Integer.parseInt(me);
computation = computation*2;
String changecomputation = Integer.toString(computation);
textfield2.setText(changecomputation);
}
validate();
repaint();
}

Java swing add Components from another Class

Im learning about Java Swing components and I want to do that when I push button, Java Swing would add label from another class into JFrame screen. Its just simple example for start.
I want to learn how to use and add swing components from another class.
There can be some stupid mistakes, but dont judge me, im new ^^
Frame class add button
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Frame extends JFrame{
private JButton btn;
private boolean regCompl = false;
public Frame(){
super("The title Macas");
setLayout(new FlowLayout());
btn = new JButton("Push for Registration");
btn.addActionListener(
new ActionListener(){
#Override
public void actionPerformed(ActionEvent event) {
regCompl = true;
}
}
);
add(btn);
if(regCompl == true){
RegComplete regObj = new RegComplete(this);
}
}// end of constructor
}
RegComplete Class add label to screen after button are pressed.
import javax.swing.JButton;
import javax.swing.JLabel;
public class RegComplete {
Frame frame;
private JLabel label;
public RegComplete(Frame fm){
this.frame = fm;
label = new JLabel("Hello world Mac4s");
fm.add(label);
}
}
You have to create Object inside the action Listener
btn = new JButton("Push for Registration");
btn.addActionListener(
new ActionListener(){
#Override
public void actionPerformed(ActionEvent event) {
RegComplete regObj = new RegComplete(Frame.this);
}
}
);

Java how to add application from the other class into gui class

I've got my Keypad class separate and I want to run it from the other class (gui), so I can have whatever I want in my gui class (some btn etc.) and then in the bottom my Keypad.
When I try Keypad kp = new Keypad(); I get almost what I want but they're displayed in separate windows I want them to be in the same window.
That's the Keypad class:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class KeypadWork extends JFrame implements ActionListener {
private JButton buttonR = new JButton("Reset");
private JButton button0 = new JButton("0");
private JButton buttonE = new JButton("Enter");
public KeypadWork() {
setTitle("Keypad");
setLayout(new GridLayout(4, 3, 2, 2));
for (int i = 1; i < 10; i++) {
addButton(new JButton(String.valueOf(i)));
}
addButton(buttonR);
addButton(button0);
addButton(buttonE);
this.pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
private void addButton(JButton button) {
button.addActionListener(this);
add(button);
}
#Override
public void actionPerformed(ActionEvent e) {
}
}
That's the solution, thank you #Alderath
If you don't want the KeyPadWork instance to be in a separate window, then you shouldn't make it a JFrame. If you want it inside another window, extend JPanel instead and add the KeyPadWork instance to some other JFrame using the normal AWT Container.add(Component) method.
Thanks a lot!

Categories