Java actionListener in another class won't open the window





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I am new to java and I want to make a simple program with 3 radioButtons, with only one button selected at a time.
I made the same program with the actionListener in the same class and it worked, but whe I moved the actionListener it in a different class I got stuck.



Here is the class where I created the window:



import javax.swing.JRadioButton;
import javax.swing.JFrame;
import java.awt.FlowLayout;;

public class window extends JFrame{
public JRadioButton radio1= new JRadioButton("Salam1");
public JRadioButton radio2= new JRadioButton("Salam2");
public JRadioButton radio3= new JRadioButton("Salam3");

public window(){
super("Title");
setLayout(new FlowLayout());

add(radio1);
add(radio2);
add(radio3);

action acc = new action();
radio1.addActionListener(acc);
radio2.addActionListener(acc);
radio3.addActionListener(acc);
}
}


And this is my ActionListener Class:



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class action implements ActionListener{
window sarma = new window();

public void actionPerformed(ActionEvent event){
if(sarma.radio1.isSelected()){
sarma.radio2.setSelected(false);
sarma.radio3.setSelected(false);
}
if(sarma.radio2.isSelected()){
sarma.radio1.setSelected(false);
sarma.radio3.setSelected(false);
}
if(sarma.radio3.isSelected()){
sarma.radio2.setSelected(false);
sarma.radio1.setSelected(false);
}
}
}


The main class



    import javax.swing.JFrame;
public class first{
public static void main(String args) {
window salam = new window();
salam.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
salam.setSize(500,150);
salam.setResizable(false);
salam.setVisible(true);
}
}


After I created the window object (named sarma) in the action class, the window won't open when I try to run the program.
So, how could I make this program work?










share|improve this question




















  • 1





    1) Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and use it consistently. 2) Research ButtonGroup to replace the ActionListener. It would achieve everything the action listener is actually trying to achieve.

    – Andrew Thompson
    Jan 4 at 13:43


















1















I am new to java and I want to make a simple program with 3 radioButtons, with only one button selected at a time.
I made the same program with the actionListener in the same class and it worked, but whe I moved the actionListener it in a different class I got stuck.



Here is the class where I created the window:



import javax.swing.JRadioButton;
import javax.swing.JFrame;
import java.awt.FlowLayout;;

public class window extends JFrame{
public JRadioButton radio1= new JRadioButton("Salam1");
public JRadioButton radio2= new JRadioButton("Salam2");
public JRadioButton radio3= new JRadioButton("Salam3");

public window(){
super("Title");
setLayout(new FlowLayout());

add(radio1);
add(radio2);
add(radio3);

action acc = new action();
radio1.addActionListener(acc);
radio2.addActionListener(acc);
radio3.addActionListener(acc);
}
}


And this is my ActionListener Class:



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class action implements ActionListener{
window sarma = new window();

public void actionPerformed(ActionEvent event){
if(sarma.radio1.isSelected()){
sarma.radio2.setSelected(false);
sarma.radio3.setSelected(false);
}
if(sarma.radio2.isSelected()){
sarma.radio1.setSelected(false);
sarma.radio3.setSelected(false);
}
if(sarma.radio3.isSelected()){
sarma.radio2.setSelected(false);
sarma.radio1.setSelected(false);
}
}
}


The main class



    import javax.swing.JFrame;
public class first{
public static void main(String args) {
window salam = new window();
salam.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
salam.setSize(500,150);
salam.setResizable(false);
salam.setVisible(true);
}
}


After I created the window object (named sarma) in the action class, the window won't open when I try to run the program.
So, how could I make this program work?










share|improve this question




















  • 1





    1) Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and use it consistently. 2) Research ButtonGroup to replace the ActionListener. It would achieve everything the action listener is actually trying to achieve.

    – Andrew Thompson
    Jan 4 at 13:43














1












1








1








I am new to java and I want to make a simple program with 3 radioButtons, with only one button selected at a time.
I made the same program with the actionListener in the same class and it worked, but whe I moved the actionListener it in a different class I got stuck.



Here is the class where I created the window:



import javax.swing.JRadioButton;
import javax.swing.JFrame;
import java.awt.FlowLayout;;

public class window extends JFrame{
public JRadioButton radio1= new JRadioButton("Salam1");
public JRadioButton radio2= new JRadioButton("Salam2");
public JRadioButton radio3= new JRadioButton("Salam3");

public window(){
super("Title");
setLayout(new FlowLayout());

add(radio1);
add(radio2);
add(radio3);

action acc = new action();
radio1.addActionListener(acc);
radio2.addActionListener(acc);
radio3.addActionListener(acc);
}
}


And this is my ActionListener Class:



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class action implements ActionListener{
window sarma = new window();

public void actionPerformed(ActionEvent event){
if(sarma.radio1.isSelected()){
sarma.radio2.setSelected(false);
sarma.radio3.setSelected(false);
}
if(sarma.radio2.isSelected()){
sarma.radio1.setSelected(false);
sarma.radio3.setSelected(false);
}
if(sarma.radio3.isSelected()){
sarma.radio2.setSelected(false);
sarma.radio1.setSelected(false);
}
}
}


The main class



    import javax.swing.JFrame;
public class first{
public static void main(String args) {
window salam = new window();
salam.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
salam.setSize(500,150);
salam.setResizable(false);
salam.setVisible(true);
}
}


After I created the window object (named sarma) in the action class, the window won't open when I try to run the program.
So, how could I make this program work?










share|improve this question
















I am new to java and I want to make a simple program with 3 radioButtons, with only one button selected at a time.
I made the same program with the actionListener in the same class and it worked, but whe I moved the actionListener it in a different class I got stuck.



Here is the class where I created the window:



import javax.swing.JRadioButton;
import javax.swing.JFrame;
import java.awt.FlowLayout;;

public class window extends JFrame{
public JRadioButton radio1= new JRadioButton("Salam1");
public JRadioButton radio2= new JRadioButton("Salam2");
public JRadioButton radio3= new JRadioButton("Salam3");

public window(){
super("Title");
setLayout(new FlowLayout());

add(radio1);
add(radio2);
add(radio3);

action acc = new action();
radio1.addActionListener(acc);
radio2.addActionListener(acc);
radio3.addActionListener(acc);
}
}


And this is my ActionListener Class:



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class action implements ActionListener{
window sarma = new window();

public void actionPerformed(ActionEvent event){
if(sarma.radio1.isSelected()){
sarma.radio2.setSelected(false);
sarma.radio3.setSelected(false);
}
if(sarma.radio2.isSelected()){
sarma.radio1.setSelected(false);
sarma.radio3.setSelected(false);
}
if(sarma.radio3.isSelected()){
sarma.radio2.setSelected(false);
sarma.radio1.setSelected(false);
}
}
}


The main class



    import javax.swing.JFrame;
public class first{
public static void main(String args) {
window salam = new window();
salam.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
salam.setSize(500,150);
salam.setResizable(false);
salam.setVisible(true);
}
}


After I created the window object (named sarma) in the action class, the window won't open when I try to run the program.
So, how could I make this program work?







java swing awt actionlistener






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 13:41









Andrew Thompson

154k29166349




154k29166349










asked Jan 4 at 13:26









DarsulDarsul

104




104








  • 1





    1) Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and use it consistently. 2) Research ButtonGroup to replace the ActionListener. It would achieve everything the action listener is actually trying to achieve.

    – Andrew Thompson
    Jan 4 at 13:43














  • 1





    1) Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and use it consistently. 2) Research ButtonGroup to replace the ActionListener. It would achieve everything the action listener is actually trying to achieve.

    – Andrew Thompson
    Jan 4 at 13:43








1




1





1) Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and use it consistently. 2) Research ButtonGroup to replace the ActionListener. It would achieve everything the action listener is actually trying to achieve.

– Andrew Thompson
Jan 4 at 13:43





1) Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and use it consistently. 2) Research ButtonGroup to replace the ActionListener. It would achieve everything the action listener is actually trying to achieve.

– Andrew Thompson
Jan 4 at 13:43












1 Answer
1






active

oldest

votes


















1














The current problem with the code is down to the fact that the action listener has no reference to the original window, and instead creates an entirely separate instance that is never set visible. (As detailed by D.G).



But the action listener is not needed. The effect can be achieved using a ButtonGroup, like this:



import javax.swing.*;
import java.awt.*;

public class RadioButtonWindow extends JFrame{
public JRadioButton radio1= new JRadioButton("Salam1");
public JRadioButton radio2= new JRadioButton("Salam2");
public JRadioButton radio3= new JRadioButton("Salam3");

public RadioButtonWindow(){
super("Title");
setLayout(new FlowLayout());

add(radio1);
add(radio2);
add(radio3);

// Only one button in this group can be selected at a time!
ButtonGroup bg = new ButtonGroup();
bg.add(radio1);
bg.add(radio2);
bg.add(radio3);
}

public static void main(String args) {
RadioButtonWindow salam = new RadioButtonWindow ();
salam.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Wrong way to size a GUI!
//salam.setSize(500,150);
salam.setResizable(false);
// Correct way to size a GUI
salam.pack();
salam.setVisible(true);
}
}





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54039847%2fjava-actionlistener-in-another-class-wont-open-the-window%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    The current problem with the code is down to the fact that the action listener has no reference to the original window, and instead creates an entirely separate instance that is never set visible. (As detailed by D.G).



    But the action listener is not needed. The effect can be achieved using a ButtonGroup, like this:



    import javax.swing.*;
    import java.awt.*;

    public class RadioButtonWindow extends JFrame{
    public JRadioButton radio1= new JRadioButton("Salam1");
    public JRadioButton radio2= new JRadioButton("Salam2");
    public JRadioButton radio3= new JRadioButton("Salam3");

    public RadioButtonWindow(){
    super("Title");
    setLayout(new FlowLayout());

    add(radio1);
    add(radio2);
    add(radio3);

    // Only one button in this group can be selected at a time!
    ButtonGroup bg = new ButtonGroup();
    bg.add(radio1);
    bg.add(radio2);
    bg.add(radio3);
    }

    public static void main(String args) {
    RadioButtonWindow salam = new RadioButtonWindow ();
    salam.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // Wrong way to size a GUI!
    //salam.setSize(500,150);
    salam.setResizable(false);
    // Correct way to size a GUI
    salam.pack();
    salam.setVisible(true);
    }
    }





    share|improve this answer




























      1














      The current problem with the code is down to the fact that the action listener has no reference to the original window, and instead creates an entirely separate instance that is never set visible. (As detailed by D.G).



      But the action listener is not needed. The effect can be achieved using a ButtonGroup, like this:



      import javax.swing.*;
      import java.awt.*;

      public class RadioButtonWindow extends JFrame{
      public JRadioButton radio1= new JRadioButton("Salam1");
      public JRadioButton radio2= new JRadioButton("Salam2");
      public JRadioButton radio3= new JRadioButton("Salam3");

      public RadioButtonWindow(){
      super("Title");
      setLayout(new FlowLayout());

      add(radio1);
      add(radio2);
      add(radio3);

      // Only one button in this group can be selected at a time!
      ButtonGroup bg = new ButtonGroup();
      bg.add(radio1);
      bg.add(radio2);
      bg.add(radio3);
      }

      public static void main(String args) {
      RadioButtonWindow salam = new RadioButtonWindow ();
      salam.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      // Wrong way to size a GUI!
      //salam.setSize(500,150);
      salam.setResizable(false);
      // Correct way to size a GUI
      salam.pack();
      salam.setVisible(true);
      }
      }





      share|improve this answer


























        1












        1








        1







        The current problem with the code is down to the fact that the action listener has no reference to the original window, and instead creates an entirely separate instance that is never set visible. (As detailed by D.G).



        But the action listener is not needed. The effect can be achieved using a ButtonGroup, like this:



        import javax.swing.*;
        import java.awt.*;

        public class RadioButtonWindow extends JFrame{
        public JRadioButton radio1= new JRadioButton("Salam1");
        public JRadioButton radio2= new JRadioButton("Salam2");
        public JRadioButton radio3= new JRadioButton("Salam3");

        public RadioButtonWindow(){
        super("Title");
        setLayout(new FlowLayout());

        add(radio1);
        add(radio2);
        add(radio3);

        // Only one button in this group can be selected at a time!
        ButtonGroup bg = new ButtonGroup();
        bg.add(radio1);
        bg.add(radio2);
        bg.add(radio3);
        }

        public static void main(String args) {
        RadioButtonWindow salam = new RadioButtonWindow ();
        salam.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Wrong way to size a GUI!
        //salam.setSize(500,150);
        salam.setResizable(false);
        // Correct way to size a GUI
        salam.pack();
        salam.setVisible(true);
        }
        }





        share|improve this answer













        The current problem with the code is down to the fact that the action listener has no reference to the original window, and instead creates an entirely separate instance that is never set visible. (As detailed by D.G).



        But the action listener is not needed. The effect can be achieved using a ButtonGroup, like this:



        import javax.swing.*;
        import java.awt.*;

        public class RadioButtonWindow extends JFrame{
        public JRadioButton radio1= new JRadioButton("Salam1");
        public JRadioButton radio2= new JRadioButton("Salam2");
        public JRadioButton radio3= new JRadioButton("Salam3");

        public RadioButtonWindow(){
        super("Title");
        setLayout(new FlowLayout());

        add(radio1);
        add(radio2);
        add(radio3);

        // Only one button in this group can be selected at a time!
        ButtonGroup bg = new ButtonGroup();
        bg.add(radio1);
        bg.add(radio2);
        bg.add(radio3);
        }

        public static void main(String args) {
        RadioButtonWindow salam = new RadioButtonWindow ();
        salam.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Wrong way to size a GUI!
        //salam.setSize(500,150);
        salam.setResizable(false);
        // Correct way to size a GUI
        salam.pack();
        salam.setVisible(true);
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 4 at 13:54









        Andrew ThompsonAndrew Thompson

        154k29166349




        154k29166349
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54039847%2fjava-actionlistener-in-another-class-wont-open-the-window%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas