JButton variable not found - java

I am trying to change the background color on click of a radio button, but the second radio button, "bt2" variable is not found by the compiler.
I keep getting this error message:
"cannot find symbol bt1" in the e.getSource()==bt1
Here is my code:
public class ColorChooser extends JFrame implements ActionListener {
public static void main(String[] args) {
new ColorChooser();
}
public ColorChooser() {
super("ColorChooser");
Container content = getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
JRadioButton bt1 = new JRadioButton("Red");
JRadioButton bt2 = new JRadioButton("Yellow");
bt1.addActionListener(this);
bt2.addActionListener(this);
content.add(bt1);
content.add(bt2);
setSize(300, 100);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == bt1)
getContentPane().setBackground(Color.RED);
else
getContentPane().setBackground(Color.YELLOW);
}
}

You should declare your bt1 as instance variable
Like that
public class ColorChooser extends JFrame implements ActionListener
{
private JRadioButton bt1;
...
}

declare bt1 outside of constructor
JRadioButton bt1;
then initialize inside constructor
bt1 = new JRadioButton("Red");
the problem in your code is bt1 is not visible to outside of constructor.it's declared as a block variable.you should declare as instance varible [in class area].
example
public class ColorChooser extends JFrame implements ActionListener {
JRadioButton bt1;//declare
public static void main(String[] args) {
new ColorChooser();
}
public ColorChooser() {
super("ColorChooser");
Container content = getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
bt1 = new JRadioButton("Red");//initializing
JRadioButton bt2 = new JRadioButton("Yellow");
bt1.addActionListener(this);
bt2.addActionListener(this);
content.add(bt1);
content.add(bt2);
setSize(300, 100);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == bt1) {
getContentPane().setBackground(Color.RED);
} else {
getContentPane().setBackground(Color.YELLOW);
}
}
}

Related

How can i make a JButton to switch its function

I had a frame with two buttons. The first button opened a second frame and the other button was closing it. It worked. Now I want to make a single button that opens a frame and by clicking it again the frame shall close. But it is not working. I set boolean conditions. By clicking the button once it is set from false to true. If it is set to "true" the actionListener is supposed to recognize that annother action (close frame) shall be performed. But nothing happens. Here the code.
public class Main {
public static void main(String[] args) {
FrameOne frameOne = new FrameOne ();
}
}
.
public class FrameOne extends JFrame implements ActionListener {
private FrameTwo frameTwo;
private boolean frameIsOpen = false; // By clicking the button once the value is set to true.
private JButton btn = new JButton();
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FrameOne(){
btn.addActionListener(this);
add(btn);
setSize(400,400);
setLocation(300, 250);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btn && frameIsOpen == false) {
frameTwo = new FrameTwo(); // Opens the new frame
boolean frameIsOpen = true;} // Fulfills the condition for the else if statement.
else if (e.getSource() == btn && frameIsOpen == true) {
frameTwo.dispatchEvent(new WindowEvent(frameTwo, WindowEvent.WINDOW_CLOSING)); }
}
}
.
public class FrameTwo extends JDialog {
FrameTwo() {
setSize(400,400);
setLocation(900, 250);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
}
You do not need having a boolean value. You can make the frameTwo null when you close it. Then check if it is null and create/show a new dialog. Also since you want to create a new dialog every time (and not hide it), I suggest you instead of frameTwo.dispatchEvent(new WindowEvent(frameTwo, WindowEvent.WINDOW_CLOSING)); to use frameTwo.dispose().
An example:
public class TestFrame extends JFrame {
private JDialog dialog;
public TestFrame() {
super("test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
JButton button = new JButton("open");
button.addActionListener(e -> {
if (dialog == null) {
dialog = new CustomDialog();
dialog.setVisible(true);
} else {
dialog.dispose();
dialog = null;
}
});
add(button);
setLocationByPlatform(true);
pack();
}
static class CustomDialog extends JDialog {
public CustomDialog() {
super();
add(new JLabel("hello world"));
setLocationByPlatform(true);
pack();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new TestFrame().setVisible(true));
}
}

Button on FrameOne shall close FrameTwo. ActionListener referes object of another class in JAVA

I have two frames in separate classes (FrameOne and FrameTwo) FrameOne has two buttons. One to open FrameTwo and One shall close FrameTwo. How to open FrameTwo I know. But not how to close from Frame One. How do I code it to make it working? Thanks. (I know that there are similar questions. I red them all. But it didn't gave me the answer. Also GUI guides didn't helped.)
public class Main {
public static void main(String[] args) {
FrameOne frame = new FrameOne();
}
}
FrameOne class:
public class FrameOne extends JFrame implements ActionListener{
private JButton btn1, btn2;
FrameOne () {
setVisible(true);
setSize(400,400);
setLayout(new FlowLayout());
setTitle("Main");
setDefaultCloseOperation(EXIT_ON_CLOSE);
btn1 = new JButton("opens FrameTwo");
btn2 = new JButton("close FrameTwo");
btn1.addActionListener(this);
btn2.addActionListener(this);
add(btn1);
add(btn2);
}
#Override
public void actionPerformed (ActionEvent e) {
if(e.getSource()== btn1) {
FrameTow frameTwo = new FrameTwo();
}
else if(e.getSource()== btn2) ;
// {???.dispose(); }
}
}
`
Frame2 class:
public class FrameTow extends JFrame {
FrameTwo () {
setVisible(true);
setSize(400,400);
setTitle("FrameTwo");
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocation(400, 400);
}
}
Any of the below solutions will work
frameTwo.dispatchEvent(new WindowEvent(frameTwo, WindowEvent.WINDOW_CLOSING));
OR
frameTwo.setVisible(false);
The short answer is modify FrameOne :
private JFrame frameTwo; //introduce a field
#Override
public void actionPerformed (ActionEvent e) {
if(e.getSource()== btn1) {
frameTwo = new FrameTwo(); //use field in action listener
}
else if(e.getSource()== btn2){
frameTwo.dispose(); //use field in action listener
}
}
The longer answer: using a JDialog for the 2nd frame is a better practice:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
new FrameOne();
}
}
class FrameOne extends JFrame implements ActionListener{
private final JButton btn1, btn2;
private JDialog frameTwo; //introduce a field
FrameOne () {
setSize(400,400);
setLayout(new FlowLayout());
setTitle("Main");
setDefaultCloseOperation(EXIT_ON_CLOSE);
btn1 = new JButton("opens FrameTwo");
btn2 = new JButton("close FrameTwo");
btn1.addActionListener(this);
btn2.addActionListener(this);
add(btn1);
add(btn2);
setVisible(true); //make it visible after construction is completed
}
#Override
public void actionPerformed (ActionEvent e) {
if(e.getSource()== btn1) {
frameTwo = new FrameTwo(); //use field in action listener
}
else if(e.getSource()== btn2){
frameTwo.dispose(); //use field in action listener
}
}
}
class FrameTwo extends JDialog {
FrameTwo() {
setSize(400,400);
setTitle("FrameTwo");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setLocation(400, 400);
setVisible(true); //make it visible after construction is completed
}
}
In the example you show, one can open several instances of FrameTwo. Which one should the second button close?
Assuming you want there to be only one, you could introduce a field in in FrameOne, initially set to null. btn1 would then only open a frame if the field is null, and assign it to the field. Then btn2 can call dispose() on the field (and reset it to null).
Example, based on your attempt:
public class FrameOne extends JFrame implements ActionListener {
private JButton btn1, btn2;
private FrameTwo frameTwo = null;
FrameOne () {
setVisible(true);
setSize(400,400);
setLayout(new FlowLayout());
setTitle("Main");
setDefaultCloseOperation(EXIT_ON_CLOSE);
btn1 = new JButton("opens FrameTwo");
btn2 = new JButton("close FrameTwo");
btn1.addActionListener(this);
btn2.addActionListener(this);
add(btn1);
add(btn2);
}
#Override
public void actionPerformed (ActionEvent e)
{if(e.getSource()== btn1)
{
if (frameTwo == null) {
frameTwo = new FrameTwo();
}
}
else if(e.getSource()== btn2) {
frameTwo.dispatchEvent(new WindowEvent(frameTwo, WindowEvent.WINDOW_CLOSING));
frameTwo = null;
}}}

How to declare just one

I have the following working code with two buttons as event listeners. One button to get and display a Panel panel1 and the other to get and display another Panel panel2 removing the existing displayed panel. I created two actionPerformed methods for each button to carry out each other's tasks. I just want to make one to shorten the code but I don't know how to detect which button in a panel is displaying at compile time. Any help will be appreciated.
//Switching between panels (screens)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ScreenDemo extends JFrame {
private JPanel panel1, panel2;
private JButton btn1, btn2;
JLabel label1 = new JLabel("Screen 1");
JLabel label2 = new JLabel("Screen 2");
public ScreenDemo() {
createPanel(); //Created at Line 14
addPanel(); //Created at Line 28
}
private void createPanel() {
//Panel for first screen
panel1 = new JPanel(new FlowLayout());
btn1 = new JButton("Move to Screen 2");
btn1.addActionListener(new addScreen1ButtonListener());
//Panel for second screen
panel2 = new JPanel(new FlowLayout());
btn2 = new JButton("Move to Screen 1");
btn2.addActionListener(new addScreen2ButtonListener());
}
private void addPanel() {
panel1.add(label1);
panel1.add(btn1);
panel2.add(label2);
panel2.add(btn2);
add(panel1); //Add the first screen panel
}
class addScreen1ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
getContentPane().removeAll();
getContentPane().add(panel2);
repaint();
printAll(getGraphics()); //Prints all content
}
}
class addScreen2ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ea) {
getContentPane().removeAll();
getContentPane().add(panel1);
repaint();
printAll(getGraphics()); //Prints all content
}
}
public static void main(String[] args) {
ScreenDemo screen = new ScreenDemo();
screen.setTitle("Switching Screens");
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen.setSize(500, 500);
screen.setVisible(true);
}
}
implement ActionListener for your class, and try this code
ScreenDemo extends JFrame implements ActionListener
...
btn1.addActionListener(this);
btn2.addActionListener(this);
...
#override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource().equals(btn1)) {
...
}
else if(ae.getSource().equals(btn1)) {
...
}
}
you have to implements the class from ActionListener
than register JButton like : btn.addActionListener(this);
so close now, You just need to capture the event and get the source from where it is called like:
public void actionPerformed(ActionEvent ae) {
if(ae.getSource == btn1){
//shuffle panel from btn1
}
if(ae.getSource == btn2) {
//shuffle panel from btn2
}
}

Action Listener: sending a text input to an object in Java

I don't know how to send a text input to an object in Java, from a button pressed on an interface using the action Listener() method.
import javax.swing.*;
import java.awt.event.*;
public class Preassessment extends javax.swing.JFrame implements ActionListener {
static JTextField concept = new JTextField(15);
JButton enter = new JButton("Enter");
JLabel conceptLabel = new JLabel("Concept: ");
public Preassessment() {
super("Preassessment Sys");
setSize(350, 180);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
pane.add(conceptLabel);
pane.add(concept);
pane.add(enter);
add(pane);
enter.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
if(event.getSource() == enter){
conceptLabel.setText(concept.getText());
}
}
public static void main(String[] arguments) {
Preassessment preassess = new Preassessment();
Preassessment agInterface = new Preassessment(); //object to receive the text
}
}
Your question is not concrete enough. In any case you need a reference to the targetobject somewhere in your code, so one way to do it is:
public class Preassessment extends javax.swing.JFrame implements ActionListener {
static JTextField concept = new JTextField(15);
JButton enter = new JButton("Enter");
JLabel conceptLabel = new JLabel("Concept: ");
public Preassessment(ActionListener listener) {
super("Preassessment Sys");
setSize(350, 180);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
pane.add(conceptLabel);
pane.add(concept);
pane.add(enter);
add(pane);
enter.addActionListener(listener);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
if(event.getSource() == enter){
conceptLabel.setText(concept.getText());
}
}
public static void main(String[] arguments) {
Preassessment agInterface = new Preassessment(); //object to receive the text
Preassessment preassess = new Preassessment(agInterface);
}
}

how to disjoin child from jfram

please note to this:
public class test extended Jframe implement actionlistener{
test()
{
Jpanel panel = new Jpqnel;
Jbutton b = new Jbutton("1");
b.addactionlistener(this);
panel.add(b);
add(panel);
}
public void actionPerformed(ActionEvent e) {
}
I want when i click on b button the child that add to Jframe(in this example: panel)
disjoint from it.
how can i do that?
By disjoin, if you mean remove, try
public class test extends JFrame implements ActionListener {
JPanel panel;
test() {
panel = new JPanel();
JButton b = new JButton("1");
b.addActionListener(this);
panel.add(b);
add(panel);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
this.remove(panel);
repaint();
}
public static void main(String a[]) {
new test();
}
}

Categories

Resources