Composition over Inheritance - java

i saw an example of ActionListener use in code by implementing ActionListener.
but here i wanna use functionality of ActionListener by using Ref.Var. of ActionListener.
JButton createButton(){
ActionListener al;
JButton button = new JButton();
button.setBounds(130, 100, 100, 40);
button.setText("aaa");
button.setSize(100, 40);
button.setLayout(null);
frame.add(button);
return button;
}
look at ActionListener reference here . how to use this ref.var on button to listen event on button

JButton createButton(){
ActionListener al = new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
// run code;
}
};
JButton button = new JButton();
button.setBounds(130, 100, 100, 40);
button.setText("aaa");
button.setSize(100, 40);
button.addActionListener(al);
frame.add(button);
return button;
}
OR
jButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//run code;
}
} );
It's basically exactly the same as having the containing class implement ActionListener: you provide an implementation, and you configure your button to listen to it.

Related

jbutton as method actionlistener doesnt work

so I created JButton as a method like this
private JButton loginButton() {
JButton button = new JButton("Login");
button.setFocusable(false);
button.addActionListener(this);
button.setFont(new Font("MV Boli", Font.PLAIN, 25));
return button;
}
when I try to test if the action listener works, it does nothing. Is this the wrong way how to create JButton and that's why it doesn't wrong or there is something wrong with my code?
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton()) {
System.out.println("Test");
}
}
this.setContentPane(mainPanel());
}
private JPanel mainPanel() {
JPanel panel = new JPanel();
panel.add(loginButton(), BorderLayout.SOUTH);
panel.setVisible(true);
return panel;
}
Your loginButton() is creating a new button each time it is called. So, when you have
panel.add(loginButton(), BorderLayout.SOUTH);
in your mainPanel() method, you create the first button. But when you have
if (e.getSource() == loginButton())
in your actionPerformed() method you create a second button, which is different from the first one you created earlier. That's why your code will not enter the if() block.

Change BG Color of JButton On Press JAVA

I am very new to GUI and am wondering how to change the BG color of a JButton when that respective button is pressed. I am not sure how to even properly structure GUI for this my first time.
public static void createWindow(int x)
{
JFrame frame = new JFrame("WINDOW");
frame.setSize(40*x, 40*x);
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
for(int i = 0; i < x * x; i++)
{
JButton button = new JButton();
button.setPreferredSize(new Dimension(40, 40));
panel.add(button);
button.addActionListener(new Action());
}
frame.add(panel);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
static class Action implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//changes color of button
}
}
Your ActionListener might look something like:
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();
button.setBackground( Color.RED );
}
And you only need one ActionListener because the code is generic since the button clicked will come from the event.
Also, Don't use Action for the Class name. There is an interface by that name so it gets confusing. Use a more descriptive name.

Disabling a Jbutton in java when another Jbutton is pressed

My code is:
public FactoryWindow()
{
getPreferredSize();
setTitle("Bounce");
JPanel buttonPanel = new JPanel();
add(comp, BorderLayout.CENTER);
addButton(buttonPanel, "Circle", new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
comp.addShape();
}
});
addButton(buttonPanel, "Machine", new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
comp.addMachine();
}
});
addButton(buttonPanel, "Close", new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
add(buttonPanel, BorderLayout.SOUTH);
pack();
}
This is a constructor. the class extends JFrame
public void addButton(Container c, String title, ActionListener listener)
{
JButton button = new JButton(title);
c.add(button);
button.addActionListener(listener);
}
I want to be able to disable the Shape button when I press the machine button
How would I go about doing that?
I know there is Something like buttonName.setEnabled(false); but I cannot figure out how to use it in this context.
You will need a reference to the button you are trying to disable, this will require you to change your code slightly...
First, you need your addButton method to return the button it created...
public JButton addButton(Container c, String title, ActionListener listener) {
JButton button = new JButton(title);
c.add(button);
button.addActionListener(listener);
return button;
}
Then you need to assign the result to a variable...
JButton cirlce = null;
JButton machine = null;
cirlce = addButton(buttonPanel, "Circle", new ActionListener() {
public void actionPerformed(ActionEvent event) {
comp.addShape();
}
});
Then you can access it from your ActionListener...
machine = addButton(buttonPanel, "Machine", new ActionListener() {
public void actionPerformed(ActionEvent event) {
comp.addMachine();
circle.setEnabled(false);
}
});
Now, if you're using Java 6 (and I think Java 7), it will complain that the button should be final, but this won't work based on the way you have your code set up. Instead, you will need to make circle and machine instance fields in order to be able to access them from within the ActionListener context

addActionListener not working

I followed a tutorial on how to do this - Here's the code I used:
package soundboard;
import javax.swing.*;
import java.awt.event.*;
public class Soundboard {
JButton Button1;
public void windowCreate() {
JFrame frame = new JFrame();
mainsPanel = new JPanel();
Button1 = new JButton("1");
Button1.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(Button1);
frame.add(mainsPanel);
frame.setSize(183,245);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent event){
}
public static void main(String[] args){
Soundboard window = new Soundboard();
window.windowCreate();
}
}
The code seems to not be working. Could anyone explain why?
The JPanel is used as a background. The problem is in Button1.addActionListener(this); , as it says that "this" is not convertible to ActionListener or something like so.
If you want to add your class as an Onclicklistener:
Button1.addActionListener(this);
then your class must implement the appropriate interface ActionListener like this:
public class Soundboard implements ActionListener{
//...
#Override
public void actionPerformed(ActionEvent e){
//...
}
}
EDIT
If you have multiple buttons, that need separated implementation, you could f.e. use anonymous classes:
mybutton.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
//does something, that probably interests only mybutton
//declare mybutton as **final** if you must use it
}
});
You need to implement the ActionListener interface if you want to override the actionPerformed method:
public class Soundboard implements ActionListener {
You can only add ActionListeners to a Component with addActionListener().
Your class has to implement ActionListener e.g.
public class Soundboard implements ActionListener {
I got it working. Here's how I implemented it, the other buttons don't do a thing quite yet.
All the code is in a class called Soundboard, which implements ActionListener, while javax.swing* and
java.awt.event* are also imported.
JButton loadButton;
JButton clearButton;
JButton Button1;
JButton Button2;
JButton Button3;
JButton Button4;
JPanel mainsPanel;
int times;
public void windowCreate() {
JFrame frame = new JFrame();
mainsPanel = new JPanel();
loadButton = new JButton("Load...");
loadButton.setSize(80, 30);
loadButton.setLocation(4, 4);
clearButton = new JButton("Clear");
clearButton.setSize(80, 30);
clearButton.setLocation(92, 4);
Button1 = new JButton("1");
Button1.setSize(80, 80);
Button1.setLocation(4, 45);
Button2 = new JButton("2");
Button2.setSize(80, 80);
Button2.setLocation(92, 45);
Button3 = new JButton("3");
Button3.setSize(80, 80);
Button3.setLocation(4, 133);
Button4 = new JButton("4");
Button4.setSize(80, 80);
Button4.setLocation(92, 133);
loadButton.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(loadButton);
frame.add(clearButton);
frame.add(Button1);
frame.add(Button2);
frame.add(Button3);
frame.add(Button4);
frame.add(mainsPanel);
frame.setSize(183,245);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
#Override
public void actionPerformed(ActionEvent event){
times += 1;
System.out.println("Test successful - this was the #"
+ times + " press");
}
public static void main(String[] args){
Soundboard window = new Soundboard();
window.windowCreate();
}

Setting functions to individual buttons

I have a soundboard program I am designing for school. I'm actually permitted to write whatever program that I want. I have a code written for it, as shown here:
package soundboard;
import javax.swing.*;
import java.awt.event.*;
public class Soundboard implements ActionListener{
JButton loadButton;
JButton clearButton;
JButton Button1;
JButton Button2;
JButton Button3;
JButton Button4;
JPanel mainsPanel;
int load;
public void windowCreate() {
JFrame frame = new JFrame();
mainsPanel = new JPanel();
loadButton = new JButton("Load...");
loadButton.setSize(80, 30);
loadButton.setLocation(4, 4);
loadButton.addActionListener(this);
clearButton = new JButton("Clear");
clearButton.setSize(80, 30);
clearButton.setLocation(92, 4);
clearButton.addActionListener(this);
Button1 = new JButton("1");
Button1.setSize(80, 80);
Button1.setLocation(4, 45);
Button1.addActionListener(this);
Button2 = new JButton("2");
Button2.setSize(80, 80);
Button2.setLocation(92, 45);
Button2.addActionListener(this);
Button3 = new JButton("3");
Button3.setSize(80, 80);
Button3.setLocation(4, 133);
Button3.addActionListener(this);
Button4 = new JButton("4");
Button4.setSize(80, 80);
Button4.setLocation(92, 133);
Button4.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(loadButton);
frame.add(clearButton);
frame.add(Button1);
frame.add(Button2);
frame.add(Button3);
frame.add(Button4);
frame.add(mainsPanel);
frame.setSize(183,245);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
#Override
public void actionPerformed(ActionEvent event){
load += 1;
System.out.println(load);
}
public static void main(String[] args){
Soundboard window = new Soundboard();
window.windowCreate();
}
}
In this example, every single button does the exact same thing. How, using this base code, ca I set it so the buttons do their own individual thing? I plan on designing it so hitting the "load" button and then a number-button loads a sound to that said button. Hitting a number-button without hitting load first plays the previously designated sound. Hitting "clear" unloads all buttons.
Instead of
ButtonX.addActionListener(this);
write
ButtonX.addActionListener(e -> {
//do stuff here
});
The -> signifies that this is a lambda expression, which basically creates an anonymous class from a functional interface and passes it as an argument. For more on lambda expressions, you can read my guide here, or the official (but long) tutorial here.
After you make all the lambda expressions, you can remove the
#Override
public void actionPerformed(ActionEvent event){
load += 1;
System.out.println(load);
}
and
implements ActionListener
from your class.
You need to attach different action performed to seperate button an e.g. on how to do it is below for load and clear button
import javax.swing.*;
import java.awt.event.*;
public class Soundboard implements ActionListener{
JButton loadButton;
JButton clearButton;
JButton Button1;
JButton Button2;
JButton Button3;
JButton Button4;
JPanel mainsPanel;
int load;
public void windowCreate() {
JFrame frame = new JFrame();
mainsPanel = new JPanel();
loadButton = new JButton("Load...");
loadButton.setSize(80, 30);
loadButton.setLocation(4, 4);
loadButton.addActionListener(e -> System.out.println("load action"));
clearButton = new JButton("Clear");
clearButton.setSize(80, 30);
clearButton.setLocation(92, 4);
clearButton.addActionListener(e -> System.out.println("Clear action"));
Button1 = new JButton("1");
Button1.setSize(80, 80);
Button1.setLocation(4, 45);
Button1.addActionListener(this);
Button2 = new JButton("2");
Button2.setSize(80, 80);
Button2.setLocation(92, 45);
Button2.addActionListener(this);
Button3 = new JButton("3");
Button3.setSize(80, 80);
Button3.setLocation(4, 133);
Button3.addActionListener(this);
Button4 = new JButton("4");
Button4.setSize(80, 80);
Button4.setLocation(92, 133);
Button4.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(loadButton);
frame.add(clearButton);
frame.add(Button1);
frame.add(Button2);
frame.add(Button3);
frame.add(Button4);
frame.add(mainsPanel);
frame.setSize(183,245);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
#Override
public void actionPerformed(ActionEvent event){
load += 1;
System.out.println(load);
}
public static void main(String[] args){
Soundboard window = new Soundboard();
window.windowCreate();
}
}

Categories