Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Problem with JRadionButton are all selected if clicked one by one

Problem with JRadionButton are all selected if clicked one by one

Scheduled Pinned Locked Moved Java
javawpftoolshelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    flashery
    wrote on last edited by
    #1

    Hello again I am creating again a new app in java which can animate images. I have problem with JRadionButton if I am going to select another one it marked with a dot and if I select another one it also marked with a dot without removing the pass which I had selected. Here's my code

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package star;

    /**
    *
    * @author flashery
    */
    import java.awt.Color;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import javax.swing.*;

    public class StarAnimate extends JFrame implements ItemListener {

    JPanel panel = new JPanel();
    
    JRadioButton btnBoard1 = new JRadioButton("Board 1", true);
    JRadioButton btnBoard2 = new JRadioButton("Board 2");
    JRadioButton btnBoard3 = new JRadioButton("Board 3");
    
    Board board1 = new Board();
    Board2 board2 = new Board2();
    Board3 board3 = new Board3();
    
    public StarAnimate() {
        init();
    }
    
    private void init() {
    
        btnBoard1.setBounds(5, 10, 100, 30);
        btnBoard1.setBackground(Color.red);
        btnBoard1.addItemListener(this);
    
        btnBoard2.setBounds(5, 40, 100, 30);
        btnBoard2.setBackground(Color.red);
        btnBoard2.addItemListener(this);
    
        btnBoard3.setBounds(5, 70, 100, 30);
        btnBoard3.setBackground(Color.red);
        btnBoard3.addItemListener(this);
    
        panel.setLayout(null);
        panel.setBounds(0, 0, 160, 540);
        panel.setBackground(Color.red);
    
        panel.add(btnBoard1);
        panel.add(btnBoard2);
        panel.add(btnBoard3);
    
    
        this.add(panel);
    
        this.setDefaultCloseOperation(EXIT\_ON\_CLOSE);
        this.setSize(560, 540);
        this.setLayout(null);
        this.setLocationRelativeTo(null);
        this.setTitle("Star Animation");
        this.setResizable(true);
        this.setVisible(true);
    }
    
    public static void main(String\[\] args) {
        StarAnimate star = new StarAnimate();
    }
    
    @Override
    public void itemStateChanged(ItemEvent e) {
    
        if (e.getSource() == btnBoard1) {
            
            
            this.add(board1);
            this.remove(board2);
            this.remove(board3);
        }
        if (e.getSource() == btnBoard2) {
            this.add(board2);
            this.remove(board1);
            this.remove(board3);
        }
        if (e.getSource() == btnBoard3) {
            this.add(b
    
    F 1 Reply Last reply
    0
    • F flashery

      Hello again I am creating again a new app in java which can animate images. I have problem with JRadionButton if I am going to select another one it marked with a dot and if I select another one it also marked with a dot without removing the pass which I had selected. Here's my code

      /*
      * To change this template, choose Tools | Templates
      * and open the template in the editor.
      */
      package star;

      /**
      *
      * @author flashery
      */
      import java.awt.Color;
      import java.awt.event.ItemEvent;
      import java.awt.event.ItemListener;
      import javax.swing.*;

      public class StarAnimate extends JFrame implements ItemListener {

      JPanel panel = new JPanel();
      
      JRadioButton btnBoard1 = new JRadioButton("Board 1", true);
      JRadioButton btnBoard2 = new JRadioButton("Board 2");
      JRadioButton btnBoard3 = new JRadioButton("Board 3");
      
      Board board1 = new Board();
      Board2 board2 = new Board2();
      Board3 board3 = new Board3();
      
      public StarAnimate() {
          init();
      }
      
      private void init() {
      
          btnBoard1.setBounds(5, 10, 100, 30);
          btnBoard1.setBackground(Color.red);
          btnBoard1.addItemListener(this);
      
          btnBoard2.setBounds(5, 40, 100, 30);
          btnBoard2.setBackground(Color.red);
          btnBoard2.addItemListener(this);
      
          btnBoard3.setBounds(5, 70, 100, 30);
          btnBoard3.setBackground(Color.red);
          btnBoard3.addItemListener(this);
      
          panel.setLayout(null);
          panel.setBounds(0, 0, 160, 540);
          panel.setBackground(Color.red);
      
          panel.add(btnBoard1);
          panel.add(btnBoard2);
          panel.add(btnBoard3);
      
      
          this.add(panel);
      
          this.setDefaultCloseOperation(EXIT\_ON\_CLOSE);
          this.setSize(560, 540);
          this.setLayout(null);
          this.setLocationRelativeTo(null);
          this.setTitle("Star Animation");
          this.setResizable(true);
          this.setVisible(true);
      }
      
      public static void main(String\[\] args) {
          StarAnimate star = new StarAnimate();
      }
      
      @Override
      public void itemStateChanged(ItemEvent e) {
      
          if (e.getSource() == btnBoard1) {
              
              
              this.add(board1);
              this.remove(board2);
              this.remove(board3);
          }
          if (e.getSource() == btnBoard2) {
              this.add(board2);
              this.remove(board1);
              this.remove(board3);
          }
          if (e.getSource() == btnBoard3) {
              this.add(b
      
      F Offline
      F Offline
      Firo Atrum Ventus
      wrote on last edited by
      #2

      Add your JRadioButton into a JRadioButtonGroup

      Excuse me for my improper grammar and typos. It's because English is my primary language, not my first language. My first languages are C# and Java. VB, ASP, JS, PHP and SQL are my second language. Indonesian came as my third language. My fourth language? I'm still creating it, I'll let you know when it's done! :-D

      F 1 Reply Last reply
      0
      • F Firo Atrum Ventus

        Add your JRadioButton into a JRadioButtonGroup

        Excuse me for my improper grammar and typos. It's because English is my primary language, not my first language. My first languages are C# and Java. VB, ASP, JS, PHP and SQL are my second language. Indonesian came as my third language. My fourth language? I'm still creating it, I'll let you know when it's done! :-D

        F Offline
        F Offline
        flashery
        wrote on last edited by
        #3

        Lol this is it thanks. Anyway its just ButtonGroup not JRadioButtonGroup.

        F 1 Reply Last reply
        0
        • F flashery

          Lol this is it thanks. Anyway its just ButtonGroup not JRadioButtonGroup.

          F Offline
          F Offline
          Firo Atrum Ventus
          wrote on last edited by
          #4

          flashery wrote:

          Anyway its just ButtonGroup not JRadioButtonGroup.

          Sorry about that, my brain's currently running in safe-mode (ie. debugging some VB apps)

          Excuse me for my improper grammar and typos. It's because English is my primary language, not my first language. My first languages are C# and Java. VB, ASP, JS, PHP and SQL are my second language. Indonesian came as my third language. My fourth language? I'm still creating it, I'll let you know when it's done! :-D

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups