Ive created a gui using windowbuilder, in it i have a button and label, my hope is that on click i can change the labels text, I am not sure how to do this and the currents configuration (below) is the one i was told to use but it seems to fail to work. I know its very basic but this is only my third day writing java, thnaks for the help
JButton button1 = new JButton(".............");
button1.setForeground(Color.RED);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JLabel clickit.SetText("you clicked it");
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 months ago.
Improve this question
I am trying just to get the button to display some text in the console, but whatever i do it isn't working here is the code for the Button class:
public class Button extends JButton implements ActionListener {
JButton button;
Button (){
button = new JButton();
this.setText("Click NOW");
button.addActionListener(this);
this.setForeground(Color.white);
button.setBounds(300, 100, 100, 50);
this.setBackground(Color.red);
this.setBorder(null);
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()== button) {
System.out.println("Display if you work");
}
}
}
There are no errors displayed and the code compiles correctly, it just isn't displaying the text in the terminal.
This code creates two JButtons, one the button field inside of the class, that you add the action listener to:
public class Button extends JButton implements ActionListener {
JButton button; // here!
Button (){
button = new JButton(); // here!
this.setText("Click NOW");
button.addActionListener(this); // and add the listener here
and the other which is the instance of this class that extends JButton:
// here !!!
public class Button extends JButton implements ActionListener {
// ....
and which is likely the one that is displayed as elsewhere you likely have this code:
Button button = new Button();
and then add this button to the GUI. Again, this "button" is from your Button class which extends JButton but doesn't have the action listener added to it.
You can solve this in one of two ways:
Don't create the new JButton button field inside of your new class and instead add the ActionListener to the this JButton, the instance of this class,
for example:
public class Button1 extends JButton implements ActionListener {
// JButton button;
Button1() {
// button = new JButton();
this.setText("Click NOW");
// button.addActionListener(this);
this.addActionListener(this);
this.setForeground(Color.white);
// button.setBounds(300, 100, 100, 50); // You really don't want to do
// this
this.setBackground(Color.red);
this.setBorder(null);
}
#Override
public void actionPerformed(ActionEvent e) {
// no need for the if block
// if (e.getSource() == button) {
System.out.println("Display if you work");
// }
}
}
Don't create a class that extends JButton but instead create code that creates a single JButton (not two) and add the ActionListener to the same object that is added to the GUI.
I'd go with number 2 myself and make it a method that returns a button with my properties of interest:
private JButton createMyButton(String text) {
JButton button = new JButton(text);
button.setForeground(Color.WHITE);
button.setBackground(Color.RED);
button.setBorder(null);
button.addActionListener(e -> {
System.out.println("Display if you work");
});
return button;
}
Side notes:
Avoid giving your class names that clash with core Java classes, such as class Button which clashes with the java.awt.Button class.
Avoid use of null layouts and setBounds. While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.
For that reason you're far better off learning about and using the layout managers. You can find the layout manager tutorial here: Layout Manager Tutorial, and you can find links to the Swing tutorials and to other Swing resources here: Swing Info.
In your actionPerformed method, use equals in the if statement, like this:
if (e.getSource().equals(button)) {
System.out.println("Display if you work");
}
It should work. == doesn't work in this case.
I am creating a GUI application using Java. There are different sections in the main frame, each having a particular functionality. The header panel contains the buttons Submit, Undo, Shuffle, Help and Quit. Here is the sample code:
JPanel header = new JPanel();
JButton submit = new JButton("Submit");
header.add(submit);
JButton undo= new JButton("Undo");
header.add(undo);
JButton shuffle= new JButton("Shuffle");
header.add(shuffle);
JButton help= new JButton("Help");
header.add(help);
JButton quit = new JButton("Quit");
header.add(quit);
Further down my code, I need to check that the button clicked is not in the header panel (there are buttons in other panels too).
public void actionPerformed(ActionEvent e)
{
String clicked = e.getActionCommand();
if(!clicked.equals("Submit") && !clicked.equals("Undo") && !clicked.equals("Help")&& !clicked.equals("Quit") && !clicked.equals("Shuffle")){
//some code here
Is there any alternate neater way to check that the button clicked is not in the header panel? I will need to do something similar in another panel which contains more buttons. Using IF statements to check for each button is inefficient and untidy, I believe.
Honestly, I'd just implement actions for every button. It may require more code but it's a better solution. But if you insist to go this way you can try this
public void actionPerformed(ActionEvent e) {
if (!Arrays.asList(PANEL.getComponents()).stream().filter(b -> b instanceof JButton)
.map(b -> (JButton) b).filter(b ->
b.getText().equals(e.getActionCommand())).findFirst().isPresent()) {
// execute code if button is not a child of PANEL
}
}
You can use addActionListener for every button separately.
I understand how to create a button and it's application in Java. Would anyone be able to show me the code to be able to make the button in the code below be able to print something as simple as hello world in the terminal. I am using bluej if that is of any matter. I am very sorry I am a beginner coder.
JButton button = new JButton();
button.setActionListener(e -> System.out.println("Clicked"));
This uses a lambda expression. Inside it, you can add as much code as you like, but add it between {} if it's more than a line.
More on buttons here
You need a listener for your button.
JButton button= new JButton("Button");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Hello World");
}
});
the button will 'listen' for the action and preform whatever task you define for it.
ActionListener is what you are looking for. There is a very nice guide on Oracle's website. You should look into this tutorial and understand different ways of creating ActionListeners. I will give you a simple example which doesn't involve Anonymous Classes because I am not sure of how much you know about them.
public class Frame extends JFrame implements ActionListener {
public Frame() {
super("Test"); // calling the superclass
setLayout(new FlowLayout()); // creating a layout for the frame
setDefaultCloseOperation(EXIT_ON_CLOSE);
// create the button
JButton jbTest = new JButton("Click me!");
/* 'this' refers to the instance of the class
because your class implements ActionListener
and you defined what to do in case a button gets pressed (see actionPerformed)
you can add it to the button
*/
jbTest.addActionListener(this);
add(jbTest);
pack();
}
// When a component gets clicked, do the following
#Override
public void actionPerformed(ActionEvent ae) {
System.out.println("Hello!");
}
}
Ok hello, What I want is when my button is pressed I want read the text from a URL and display it in the GUI,
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
//???????????????????
}
I am very confused on this. I saw it in a Open Source project and I can't get it to work :/ All I really want is it to open up to a raw github file (in the GUI) to display the contents of the github.
This is the github link
I want the text to display as if it where actually in the application.
Thanks for any help
What I would do, is go ahead and make a new JFrame, named something different than your previous one, but set it's visibility to false. Within that new JFrame, "ex", add a layout and then add you github info into the layout.
ex:
JFrame ex = new JFrame();
ex.setSize(50,50);
ex.setVisible(false);
ex.setResizable(falsE);
ex.setDefaultCloseOperation(JFrame.Dispose);
//Use "dispose" to exit the window but not terminate your program
and then, when you have a JButton clicked, or whatever you're using, use an actionevent.
ex:
jButton1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("JButton1 Button pressed");
ex.setVisible(true);
}
});
I am developing a calculator in Java language. The problem is that, i put ten buttons for digits(0,1,2..9) and i want that when i clicked one of them, all perform the same mouse clicked function. Is it possible? In netbeans, it does not let me do that, or i couldnt achieve. Thank you for helping.
Yes. Add the same listener to both buttons you are using.
For example, suppose you are using actionListener then:
public class ListenerClass implements Action{
#override
public void actionPerformed(ActionEvent e) {
//here retrieve information on which button has generated the event
}
}
ListenerClass listener = new ListenerClass();
JButton first = new JButton();
JButton second = new JButton();
first.addActionListener(listener);
second.addActionListener(listener);