I am working on a project but the program seems to have a bug that I can not find.
Here is an MCVE which reproduces the problem:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class SO{
JLabel label;
JButton button;
JPanel panel;
JFrame frame;
public static void main(String[] args){
new SO().start();
}
public void start()
{
label = new JLabel("Button not pressed");
button = new JButton("Press me");
frame = new JFrame();
panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
panel.add(label);
panel.add(button);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("Button was pressed");
label = new JLabel("Button is pressed"); //Doesn't work
frame.repaint();
}
});
}
}
The above program has a JLabel with some text and a JButton both of which are added into a JPanel which in turn is added to a JFrame.
When the button is pressed, I want the text in the JLabel to change. But the text doesn't get changed despite the println executing every time I press the button.
What is the problem here?
You are creating a new object of the JLabel on clicking on the button but not adding it to the JPanel or JFrame after that.
In spite of creating new object i.e.
label = new JLabel("Button is pressed")
do something like,
label.setText("Button is pressed");
More Info
change
label = new JLabel("Button is pressed");
to
label.setText("Button is pressed");
you don't need to create and assign new lable each time.just change the text
You can change that line to label.setText("Button is pressed"); to make this work.
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class SO{
JLabel label;
JButton button;
JPanel panel;
JFrame frame;
public static void main(String[] args){
new SO().start();
}
public void start()
{
label = new JLabel("Button not pressed");
button = new JButton("Press me");
frame = new JFrame();
panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
panel.add(label);
panel.add(button);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("Button was pressed");
label.setText("Button is pressed"); //Doesn't work
frame.repaint();
}
});
}
}
Related
I'm working in java using Jpanel and my work is compiling fine however is showing no output. hopefully, someone could tell me why this is. I'm using jscrollpane and I'm calling it at the end idk if it's something to do with the listener or what.
FileDemoPanel.java
package Tutoiral03Task01;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FileDemoPanel extends JPanel implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton openBtn, saveBtn;
JTextArea workTa;
openBtn = new JButton ("Open");
openBtn.setEnabled (false);
openBtn.setMnemonic('g');
openBtn.setToolTipText("open button");
setLayout(new BorderLayout());
saveBtn = new JButton ("Save");
saveBtn.setEnabled (false);
saveBtn.setMnemonic('f');
saveBtn.setToolTipText("Save button");
JTextArea logTA = new JTextArea (5, 100);
logTA.setEditable(false);
logTA.setBackground(Color.lightGray);
logTA.setMargin(new Insets(5,5,5,5));
JScrollPane logScrollPane = new JScrollPane(logTA);
add(logScrollPane);
}
}
FileDemo.java
package Tutoiral03Task01;
import javax.swing.*;
public class FileDemo {
public static void main (String[] args){
JFrame frame = new JFrame("Working with files");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new FileDemoPanel());
frame.pack();
frame.setVisible(true);
}
}
The problem is that you create all the buttons and others in a actionperformed method.
This is wrong, because that is used as a ButtonListener, so if you dont press a button nothing will happened. We use to write the GUI frame in the constructor of the class.Then we create an object type of the GUI class. So i think i fixed it and i did some extra changes to make the program more simple. The step i didnt do is to add a ButtonListener, so Buttons do nothing.
i wish it will helps you.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FileDemoPanel extends JFrame {
private JPanel panel = new JPanel();
private JButton openBtn = new JButton("Open");
private JButton saveBtn = new JButton ("Save");
private JTextArea workTa;
public FileDemoPanel(){
openBtn.setEnabled (false);
openBtn.setMnemonic('g');
openBtn.setToolTipText("open button");
setLayout(new BorderLayout());
saveBtn.setEnabled (false);
saveBtn.setMnemonic('f');
saveBtn.setToolTipText("Save button");
JTextArea logTA = new JTextArea (5, 100);
logTA.setEditable(false);
logTA.setBackground(Color.lightGray);
logTA.setMargin(new Insets(5,5,5,5));
JScrollPane logScrollPane = new JScrollPane(logTA);
panel.add(openBtn);
panel.add(saveBtn);
panel.add(logTA);
panel.add(logScrollPane);
this.setContentPane(panel);
this.setVisible(true);
this.setResizable(true);
this.setSize(350,150);
this.setTitle("Κεντρική Σελίδα");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
And the mainclass.As you can see is too small.
public class FileDemo {
public static void main (String[] args){
new FileDemoPanel();
}
}
So i have two simple jframes, one is the main frame and the other is visible only when a button is pressed.
What I'm trying to do now is to display which button is being pressed in the second jframe, whether its toy or food in the jlabel in the first jframe.
The button launch selection in the first jframe will link to the second jframe, then the user clicks one of the two button and the button that was clicked will be displayed in the jlabel such as "Toy button was clicked"
I implemented how the two jframes linked by:
class SelectionListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
Object_Selection object_select = new Object_Selection(); //launch the second jframe
object_select.setVisible(true);
}
}
But I'm having issue on displaying which button was pressed in the second jframe in the jlabel of the first jframe.
Here an one-file mcve (copy paste the entire code into one file OpenDialogWindow.java, and run) demonstrating what you want to achieve:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class OpenDialogWindow {
public static void main(String[] args) {
JFrame frame = new JFrame("Main Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(400,250);
frame.add(new Pane());
frame.pack();
frame.setVisible(true);
}
}
class Pane extends JPanel{
private static int WIDTH = 300, HEIGHT = 100, GAP = 5;
private final JLabel label;
Pane() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setLayout(new BorderLayout(GAP,GAP));
label = new JLabel("", JLabel.CENTER);
add(label, BorderLayout.PAGE_START);
JButton show = new JButton("Show Dialog");
show.addActionListener(e-> new Diag(new DiagButtonListener()));
add(show, BorderLayout.PAGE_END);
}
class DiagButtonListener implements ActionListener{
#Override
public void actionPerformed(ActionEvent e) {
label.setText("Diag button clicked !");
}
}
}
class Diag extends JDialog {
public Diag(ActionListener listener) {
setTitle("Dialog window");
setSize(300, 150);
setLocation(450,400);
JButton btn = new JButton("Click");
btn.addActionListener(listener);
add(btn, BorderLayout.NORTH);
JLabel help = new JLabel("Click button and see parent frame updted", JLabel.CENTER);
add(help, BorderLayout.SOUTH);
setVisible(true);
}
}
I am trying to create a Java GUI application that contains a label and button. When the button is clicked the background color of the first panel is changed. I've got the label and button but getting errors whenever I click the button. Also, I wanted the first panel to originally have a yellow background then switch to whatever color. Here's my code:
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
public class ChangeDemo extends JFrame implements ActionListener
{
public static final int WIDTH = 300;
public static final int HEIGHT= 200;
private JPanel biggerPanel;
public static void main(String[] args)
{
ChangeDemo gui = new ChangeDemo();
gui.setVisible(true);
}
public ChangeDemo()
{
super ("ChangeBackgroundDemo");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(2,3));
JPanel biggerPanel = new JPanel();
biggerPanel.setLayout(new BorderLayout());
biggerPanel.setBackground(Color.YELLOW);
JLabel namePanel = new JLabel("Click the button to change the background color");
biggerPanel.add(namePanel, BorderLayout.NORTH);
add(namePanel);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.setBackground(Color.LIGHT_GRAY);
JButton changeButton = new JButton("Change Color");
changeButton.addActionListener(this);
buttonPanel.add(changeButton);
add(buttonPanel);
}
public void actionPerformed(ActionEvent e)
{
String buttonString = e.getActionCommand();
if(buttonString.equals("Change Color"))
biggerPanel.setBackground(Color.RED);
else
System.out.println("Unexpected Error!");
}
}
I made a few changes to your code.
First, you must start a Swing application with a call to SwingUtilities.invokeLater.
public static void main(String[] args) {
SwingUtilities.invokeLater(new ChangeDemo());
}
Second, you use Swing components. You only extend a Swing component when you want to override a method of the Swing component.
Third, I made a action listener specifically for your JButton. That way, you don't have to check for a particular JButton string. You can create as many action listeners as you need for your GUI.
JButton changeButton = new JButton("Change Color");
changeButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
isYellow = !isYellow;
if (isYellow) buttonPanel.setBackground(Color.YELLOW);
else buttonPanel.setBackground(Color.RED);
}
});
Finally, I changed the background color of the JButton panel.
Here's the entire ChangeDemo class.
package com.ggl.testing;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ChangeDemo implements Runnable {
private boolean isYellow;
private JFrame frame;
public static void main(String[] args) {
SwingUtilities.invokeLater(new ChangeDemo());
}
#Override
public void run() {
frame = new JFrame("Change Background Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
JPanel namePanel = new JPanel();
JLabel nameLabel = new JLabel(
"Click the button to change the background color");
nameLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
namePanel.add(nameLabel);
mainPanel.add(namePanel);
final JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.setBackground(Color.YELLOW);
isYellow = true;
JButton changeButton = new JButton("Change Color");
changeButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
isYellow = !isYellow;
if (isYellow) buttonPanel.setBackground(Color.YELLOW);
else buttonPanel.setBackground(Color.RED);
}
});
buttonPanel.add(changeButton);
mainPanel.add(buttonPanel);
frame.add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}
Here is working demo based on amendments to your code, haven't had time to tidy it up but hopefully you'll get the gist of it. Problem was you hand't added Panels to the borders (north, south etc.) in order to color them. Hopefully this helps.
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
public class ChangeDemo extends JFrame implements ActionListener
{
public static final int WIDTH = 300;
public static final int HEIGHT= 200;
private JPanel biggerPanel = new JPanel();
private JPanel namePanel = new JPanel();
public static void main(String[] args)
{
ChangeDemo gui = new ChangeDemo();
gui.setVisible(true);
}
public ChangeDemo()
{
super ("ChangeBackgroundDemo");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(2,3));
//JPanel biggerPanel = new JPanel();
this.biggerPanel.setLayout(new BorderLayout());
this.biggerPanel.setBackground(Color.YELLOW);
JLabel nameLabel = new JLabel("Click the button to change the background color");
namePanel.add(nameLabel);
namePanel.setBackground(Color.YELLOW);
//this.biggerPanel.add(namePanel, BorderLayout.NORTH);
add(namePanel);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.setBackground(Color.LIGHT_GRAY);
JButton changeButton = new JButton("Change Color");
changeButton.addActionListener(this);
changeButton.setActionCommand("Change Color");
buttonPanel.add(changeButton);
add(buttonPanel);
}
public void actionPerformed(ActionEvent e)
{
String buttonString = e.getActionCommand();
if(buttonString.equals("Change Color"))
this.namePanel.setBackground(Color.RED);
else
System.out.println("Unexpected Error!");
}
}
A while back I started a project that soon built up a shed load of code, most of the code was made up of components and their properties. All was going well until I hit an error. Off the top of head, the error was something on the line of exceeding the code limit of a constructor, roughly 65000 bytes.
This error has literally bought me and my project to halt. At the same time I have also found major problems in my understanding of SWING.
What I tried was to break my game code into logical sections, putting each section into a different class. For example, a jpanel which dealt with buying and selling would be its own .java file. Another jpanel that dealt with shipping would be in another .java file.
What I hoped to achieve was a JFrame that called each of these jpanels when the user requested it at the press of a jbutton. However, this didn't quite work as I wished, putting me in a position where I need help.
What I have done is simplified my problem by creating an example framework, hoping that somebody could point out what I need to be looking at, possibly even a solution.
I have created a JFrame which holds a panel called bg, which itself holds 2 JButtons (btn1 and btn2). In a different class file I have created a JPanel called panel1, and in another class again I have created another JPanel called panel2.
When the user opens the application they are presented with a frame and the option of two buttons, when any of these buttons are pressed I would like for either panel1 or
panel2 to open. How would this be done?
Any help would be greatly appreciated. Thanks in advance.
////////////// frame
package panel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Frame implements ActionListener {
public JPanel bg;
public static JButton btn1, btn2;
public Frame(){
JFrame f = new JFrame();
f.setSize(308, 205);
f.setLayout(null);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
bg = new JPanel();
bg.setSize(300, 200);
bg.setLocation(0, 0);
bg.setLayout(null);
bg.setBackground(Color.black);
bg.setVisible(true);
btn1 = new JButton("open 1");
btn1.setSize(135, 30);
btn1.setLocation(10, 10);
btn1.addActionListener(this);
btn2 = new JButton("open 2");
btn2.setSize(135, 30);
btn2.setLocation(155, 10);
btn2.addActionListener(this);
bg.add(btn1);
bg.add(btn2);
f.add(bg);
}
public static void main(String[] args) {
new Frame();
}
#Override
public void actionPerformed(ActionEvent a) {
if (a.getSource() == btn1){
}
if (a.getSource() == btn2){
}
}
}
////////////////////// panel1
package panel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class panel1 implements ActionListener {
public JButton btn3;
public panel1(){
JPanel a = new JPanel();
a.setSize(280, 110);
a.setLocation(155, 10);
a.setBackground(Color.red);
a.setVisible(true);
btn3 = new JButton("open bb");
btn3.setSize(100, 30);
btn3.setLocation(10, 10);
btn3.addActionListener(this);
a.add(btn3);
}
#Override
public void actionPerformed(ActionEvent a) {
if (a.getSource() == btn3){
}
}
}
//////////////////////////// panel2.java
package panel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class panel2 implements ActionListener {
public JButton btn4;
public panel2(){
JPanel b = new JPanel();
b.setSize(280, 110);
b.setLocation(155, 10);
b.setBackground(Color.blue);
b.setVisible(true);
btn4 = new JButton("open");
btn4.setSize(100, 30);
btn4.setLocation(10, 10);
btn4.addActionListener(this);
b.add(btn4);
}
#Override
public void actionPerformed(ActionEvent a) {
if (a.getSource() == btn4){
}
}
}
You don't need to split your panels into different classes for something this simple. Try keeping everything together:
package panel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Frame implements ActionListener {
public JPanel bg,panel1,panel2;
public static JButton btn1, btn2;
public Frame(){
JFrame f = new JFrame();
f.setSize(308, 205);
f.setLayout(null);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
bg = new JPanel();
bg.setSize(300, 200);
bg.setLocation(0, 0);
bg.setLayout(null);
bg.setBackground(Color.black);
bg.setVisible(true);
btn1 = new JButton("open 1");
btn1.setSize(135, 30);
btn1.setLocation(10, 10);
btn1.addActionListener(this);
btn2 = new JButton("open 2");
btn2.setSize(135, 30);
btn2.setLocation(155, 10);
btn2.addActionListener(this);
bg.add(btn1);
bg.add(btn2);
f.add(bg);
panel1 = new JPanel();
panel1.setSize(280, 110);
panel1.setLocation(155, 10);
panel1.setBackground(Color.red);
panel1.setVisible(false);
bg.add(panel1);
panel2 = new JPanel();
panel2.setSize(280, 110);
panel2.setLocation(155, 10);
panel2.setBackground(Color.blue);
panel2.setVisible(false);
bg.add(panel2);
}
public static void main(String[] args) {
new Frame();
}
#Override
public void actionPerformed(ActionEvent a) {
if (a.getSource() == btn1){
panel1.setVisible(true);panel2.setVisible(false);
}
if (a.getSource() == btn2){
panel1.setVisible(false);panel2.setVisible(true);
}
}
}
I'm currently writing an interface where a have a JFrame class and two JPanel classes. When the script is first executed, Panel A is shown. I have a JButton in Panel A which I would like, when clicked, to display Panel B instead of Panel A.
Is there any way I could do this?
Read tutorial for that.
You can use next() method of CardLayout for showing next card,
or you can use show(...); for showing specific card.
Simple example:
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Example {
public static void main(String[] args){
JFrame frame = new JFrame();
final JPanel panel = new JPanel(new CardLayout());
JLabel l1 = new JLabel("1");
JLabel l2 = new JLabel("2");
JLabel l3 = new JLabel("3");
panel.add(l1,"l1");
panel.add(l2,"l2");
panel.add(l3,"l3");
JButton btn = new JButton("next");
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
CardLayout layout = (CardLayout) panel.getLayout();
layout.next(panel);
}
});
JButton btnSpec = new JButton("l3");
btnSpec.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
CardLayout layout = (CardLayout) panel.getLayout();
layout.show(panel, "l3");
}
});
frame.add(panel);
frame.add(btn,BorderLayout.SOUTH);
frame.add(btnSpec,BorderLayout.NORTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}