0

Currently I use Netbeans IDE 8. 0, I want to replace skin JPanel when I choose a radio button. The problem which I faced two strong panel in form1 with radio button in form2. Two of them are not in a form. I tried to research in Google but no answer.

In source code, which have I recognized in error since the panel in other form, then what can I do?

Then source code radio button which I created:

private void rwhiteMouseClicked(java.awt.event.MouseEvent evt) {                                    
        rgreen.setSelected(false);
        rred.setSelected(false);
        rblack.setSelected(false);
       jPanel1.setBackground(new java.awt.Color(255, 255, 255));
    }                                   

    private void rgreenMouseClicked(java.awt.event.MouseEvent evt) {                                    
        rwhite.setSelected(false);
        rred.setSelected(false);
        rblack.setSelected(false);
    jPanel1.setBackground(new java.awt.Color(0, 204, 0))
    }                                   

    private void rredMouseClicked(java.awt.event.MouseEvent evt) {                                  
        rgreen.setSelected(false);
        rwhite.setSelected(false);
        rblack.setSelected(false);
         jPanel1.setBackground(new java.awt.Color(255, 0, 0));
    }                                 

    private void rblackMouseClicked(java.awt.event.MouseEvent evt) {                                    
        rgreen.setSelected(false);
        rred.setSelected(false);
        rwhite.setSelected(false);
          jPanel1.setBackground(new java.awt.Color(0, 0, 0));
    }  
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Tino_Lor
  • 23
  • 3
  • Perhaps there is a language barrier, but at least for me, I am having difficulty understanding your question and your problem. Please consider clarifying both, telling more, and showing more pertinent code, preferably a [mcve]. – Hovercraft Full Of Eels Dec 01 '17 at 02:39
  • 1
    Side note: you almost never want to use a MouseListener with a JRadioButton. ActionListener, yes sometimes, ItemListener, but MouseListener, very rarely. – Hovercraft Full Of Eels Dec 01 '17 at 02:40
  • 2
    Side note 2: No need to call setSelected on the other JRadioButtons. Add them all to the same ButtonGroup to have this effect. That you're doing this suggests that you've not yet read the [JRadioButton Tutorial](https://docs.oracle.com/javase/tutorial/uiswing/components/button.html). Please check this out. – Hovercraft Full Of Eels Dec 01 '17 at 02:40
  • okk...i will clarify...my problem is radio button exist in form A and jpanel exist in form B they are not in one form..I want to when I click in radio button then a go form B which existing jpanel..automatically jpanel color changed...Have you understood sir.. – Tino_Lor Dec 01 '17 at 02:49
  • Can you post your code what is formA and formB – Ganesh Patel Dec 01 '17 at 04:05
  • First, You have to add your radio button in `ButtonGroup` and set action command on your radio button then you have to add `ItemListener` on your button. In `ItemListener` you have to get the action command using action command you have to change your panel. And don't forget to call the `setOpaque(true)` method on your panel – Ganesh Patel Dec 01 '17 at 04:13
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 3) Don't use a `MouseListener` for buttons! An `ActionListener` works for both mouse clicks and keyboard input. – Andrew Thompson Dec 01 '17 at 06:10
  • 1
    @GaneshPatel *"Can you post your code what is formA and formB"* Better to post an MCVE / SSCCE as I mentioned in my comment. *"you have to add `ItemListener`"* That's less optimal than an `ActionListener` in that it will fire two events for every time a single button is clicked on. *"don't forget to call the `setOpaque(true)` method on your panel"* It's the default state of a `JPanel`. Perhaps you're thinking of `JLabel`. – Andrew Thompson Dec 01 '17 at 06:15
  • @AndrewThompson Sir I have told `ItemListener` because to capture the state of radio Button is selected or not – Ganesh Patel Dec 01 '17 at 11:09
  • 1
    If the buttons are in a `ButtonGroup` as suggested by @HovercraftFullOfEels 8 hours ago, there would be no need to check! The button that fires the event would be selected. You've implemented that advice, right? Otherwise we're just wasting our time trying to help. – Andrew Thompson Dec 01 '17 at 11:24

0 Answers0