Java Programming Tutorial - 67 - JRadioButton Final Program

Sdílet
Vložit
  • čas přidán 7. 09. 2024

Komentáře • 157

  • @odo432
    @odo432 Před 12 lety

    Yay. I'm learning.
    Managed to modify this code to add two new radio buttons in a separate group (Serif and Calibri) so when you click on them it changes the font style without affecting the results of the four buttons we created in this tutorial.
    It took me a while to figure it out but I got there in the end. All that was needed was to change how the buttons accessed the HandlerClass and create a few if statements.

  • @brockjaeger2929
    @brockjaeger2929 Před 8 lety +102

    If you are watching this you are the last 4% of the viewers since video 1.

  • @doruciubotaru7607
    @doruciubotaru7607 Před 9 lety +12

    This example can also be made without using those four Font fields as in the previous example with JCheckBox. In overriden method itemStateChanged from HandlerClass to use a Font reference i.e. "Font font" and after that with "if, else" statements to select the type of font you want to be displayed when a particular type of button is selected, using method isSelected().
    if (bib.isSelected()) {
    font = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
    } else if (bb.isSelected()) {
    font = new Font("Serif", Font.BOLD, 14);
    } else if (ib.isSelected()) {
    font = new Font("Serif", Font.ITALIC, 14);
    } else {
    font = new Font("Serif", Font.PLAIN, 14);
    }

  • @zohaibameer6766
    @zohaibameer6766 Před 11 lety +6

    3 people are still sick of that sandwich story :D

  • @moreorless6523
    @moreorless6523 Před 8 lety +6

    bucky: "im just a plain old guy."
    me: "no, you are cool and awesome!"

  • @omegaindustry
    @omegaindustry Před 12 lety

    dude, idk if you ever got my messages but i just wantred to let you know that all the tutorials and stuff you make/made are super super appreciated! ive watched all the beginner java tutorials (up till this one so far) in complete order, and ive learned more from you in just a couple days then i did reading a java book for 2 weeks! thanks so much bro! keep up the good work!

  • @BobTheZealot
    @BobTheZealot Před 9 lety +16

    The valuable part is from 3:00 to 3:05.

  • @mradosovic
    @mradosovic Před 12 lety

    Bucky, I really appreciate that you are showing us all this and if I learn enough to get a job as a programmer, I promise I'll send you a regular donation, really.

  • @arveersingh9728
    @arveersingh9728 Před 8 lety +1

    wow! i wrote 60 lines of code for the first time and didn't get bored.

  • @zwerty007
    @zwerty007 Před 12 lety +1

    I am so happy, I managed to complete the program by only watching up to 4:00!

  • @darkinin
    @darkinin Před 12 lety

    @12me91 I think the reason he did it without making an object for the HandlerClass this time was because each button needs to pass in its own exclusive font to the constructor, instead of simply naming the constructor.
    In the previous tutorials he always defined the variables that were being passed into the constructor in the object declaration itself, but this time, due to having different inputs that are exclusive of each other, each one needs to pass its own variable.

  • @Jozekban
    @Jozekban Před 13 lety

    wow this is the first time that you are using commentaries in java in this tutorial series

  • @darkinin
    @darkinin Před 12 lety

    @12me91 An easier way to think of it is because in the previous tutorials we used if/else statements, in which the fonts were declared within the itemStateChanged method. However, this time the fonts were declared in the GUI constructor, with the radio buttons. Due to this, in order to make the buttons work they need to be made to pass their own independent font variable into the HandlerClass constructor, which can't be done when an object has already been declared.
    Hope this helps.

  • @MisterFlink
    @MisterFlink Před 12 lety

    Great tutorials Bucky! I've been following since the first one to brush up my java skills.
    Since itemStateChanged is called twice; once for select and once for deselect, I added this to make sure that the font is set only on select:
    if(event.getStateChange()==ItemEvent.SELECTED)
    tf.setFont(font);

  • @rodolfobeguiristain
    @rodolfobeguiristain Před 8 lety +7

    Now for your "buckies greatest hits!" 0:45 "phlem phlem phlem phelm"

  • @jul.jones90
    @jul.jones90 Před 11 lety

    JTextField - Creates a Text field on the screen . So there goes everything .
    p. s The add() ; function adds your buttons . objects and everything to the screen . So basically as soon as you use add you can see it their on the screen .

  • @6LordMortus9
    @6LordMortus9 Před 11 lety

    I know the video is old, but I thought I would thank you for putting comments in the program :) It's always helpful.

  • @specimon
    @specimon Před 14 lety

    @ Majin. Dont know if thats what you are looking for but - in the ItemListener just assign a new String to the textfield according to the JRadioButton you clicked - its kind of a mix between this tutorial and the last one -fairly simple

  • @777firefalcon
    @777firefalcon Před 12 lety

    I find these a lot easier to understand if I code them along with him, then I can look over it when I'm done and figure out exactly what I did.

  • @shadysW0RLD
    @shadysW0RLD Před 13 lety

    your awesome dude learn more from you than my university lecturer

  • @Innovater6
    @Innovater6 Před 11 lety

    Wow seems like ages ago I started watching this playlist :D

  • @frankudjus2393
    @frankudjus2393 Před 11 lety

    After seeing your comment i decided to try the same :) took me some time but eventually figured it out

  • @Notrollism
    @Notrollism Před 12 lety

    yh you can. use a method to get the string from the textfield and use this..
    int newValue;
    public void convertString(String stringfromtextfield){
    newValue = Integer.parseInt(stringfromtextfield);
    }
    this will set the value of the int newValue to the converted string value of the sting passed in when you call this method

  • @MrYouMath
    @MrYouMath Před 12 lety

    @ 5:35: I rewatched the video bucky. And you did it again :D.

  • @rickie_bo
    @rickie_bo Před 9 lety +3

    3:00 Let the party start!

  • @bautrey
    @bautrey Před 12 lety

    This was exactly what I was looking for my program I was making.

  • @Dimthegamer115
    @Dimthegamer115 Před 11 lety

    Buccy doesn't seem to, as far as I can tell, as long as I can remember the specific words, most of which are easy you're good. It's sort of inspiring to me when he spells stuff wrong, I'm dyslexic and have wanted to code since I was around 2 and playing sonic, it shows you don't need to, to be good at it.

  • @ferhatacar5641
    @ferhatacar5641 Před 6 lety

    I have just realized that Your examples are same as in the Deitel java book :D :D but I understand from your videos more than book

  • @albinfriedner
    @albinfriedner Před 13 lety

    @xero907 Nice one! :D
    You can also write" public void itemStateChanged(ItemEvent event){ tf.setFont(font); pb.setEnabled(false); bb.setEnabled(false); ib.setEnabled(false); bib.setEnabled(false);". Then you can´t change your choise!

  • @mihaicostica8760
    @mihaicostica8760 Před 4 lety

    why did he change the method of writing thehandler(i mean why did he make a constructor), compared to the other GUI tutorials? i got confused...couldn't he jus not declare font variables and just make it as usual with a few else if statemens? like if bb.isSelected() then the font = new Font etc...? what's the difference between these methods of writing this program?

  • @Miner493R
    @Miner493R Před 11 lety

    A good way to really learn even more than he is teaching you is to create a small program straying from the tutorial outline. like what @odo432 did, you get better at java by doing that. As well as watching and learning from the tutorial itself.(I can sometimes talk/sound very smart for a 13 year old :)

  • @Fbiman93
    @Fbiman93 Před 14 lety

    You're like 10 years younger than me, but I wish you were one of my teachers in my high school.

  • @shashwatkhanna6036
    @shashwatkhanna6036 Před 8 lety +1

    You can even do this with this code
    class Handler implements ActionListener{
    Font fu = null;
    @Override
    public void actionPerformed(ActionEvent e) {
    if(bold_Italic.isSelected()){
    fu = bif;
    }else if(bold.isSelected()){
    fu = bf;
    }else if(italic.isSelected()){
    fu = itf;
    }else if(plain.isSelected()){
    fu = pf;
    }else{
    System.out.print("Error");
    }
    tr.setFont(fu);
    }

  • @xsyed
    @xsyed Před 12 lety

    i one of the greatest thing i did in my life is SUBSCRIBING TO THENEWBOSTON --> LOVE IT

  • @freekupo
    @freekupo Před 10 lety +1

    Is it less efficient to make make a new handler object everytime a radio button is clicked? Would it be better we made only one handler object, and made the handler test for which button was clicked? and which method would be used to do that? Thanks!

    • @plenn657
      @plenn657 Před 10 lety +1

      Both can be done, however, even I'm not sure which would be more efficient. For the second method of solving the solution you could do something similar to what was done in the JCheckBox tutorial to check which button was clicked using the isSelected() method. However, I'm not sure, which solution is more efficient, the one that takes up more memory by making more objects without needing if statements or the one that uses one handler object but uses multiple if statements

  • @JordanShackelford
    @JordanShackelford Před 9 lety +4

    My text isn't changing style when I click a different radio button. It stays plain.

    • @Dragosknight
      @Dragosknight Před 9 lety

      Jordan Shackelford Check with the handler or constructor or font you have set in

    • @erinlee928
      @erinlee928 Před 6 lety

      maybe you didn't do the add()?

  • @ravihammond
    @ravihammond Před 12 lety

    Bucky, your green lightsaber is missing it's handle :O

  • @AlSaiqal
    @AlSaiqal Před 12 lety

    I like how this video has no dislikes :)

  • @joshhamil9631
    @joshhamil9631 Před 8 lety

    If i want to make Objects seperately for Handlerclass like in the previous video, how many do I have to make ?

    • @wildglorypsn
      @wildglorypsn Před 8 lety

      +josh hamil you just have to make 1 object for the Handlerclass to access it.

  • @Alavistaven2012
    @Alavistaven2012 Před 12 lety

    Did anyone instinctively yell "bullshit" at 5:37? I

  • @ohadmarbach6569
    @ohadmarbach6569 Před 10 lety

    Inside handlerclass, why is the constructor public?
    The program also works when it is private. what is the logic behind making it public if it is used only inside this class?

  • @Iceman12481632
    @Iceman12481632 Před 11 lety

    EventListener is just a tagging interface that every listener like ActionListener must extend.

  • @jacoblupone4531
    @jacoblupone4531 Před 9 lety

    So, if I want different outcomes for each radio button, I have to make a different handler class for them?

  • @Kalvin104
    @Kalvin104 Před 13 lety

    Brilliant Tutorial =D

  • @Islandinthesun87
    @Islandinthesun87 Před 12 lety

    NVM, I found out that I missed a bracket, but I also needed to refresh the class I made. IDK why :(
    i literally cut out this line:
    private class HandlerClass implements ItemListener
    and re-pasted it after i missed and redid the bracket
    everything's good now, so I guess eclipse runs the previous code a little awkwardly once errors occur.

  • @dbh937
    @dbh937 Před 12 lety

    I knew you needed to know some math to learn how to program well, but no one told me I needed to know how to spell!

  • @aaron12343000
    @aaron12343000 Před 11 lety

    god bless all no matter what!

  • @kooanna
    @kooanna Před 11 lety

    double banger!

  • @cjwhitetds767
    @cjwhitetds767 Před 12 lety

    I'm confused by what happens from 7:50 to 8:25. I think its because the word font is used like 15 times in 35 seconds. Can someone explain how that part functions?

  • @SirFency
    @SirFency Před 4 lety

    i followed most of it pretty clearly but the passing of the font at the end got very confusing for me.

  • @plasmard
    @plasmard Před 12 lety

    convert it to a String , String nickiminaj = String.format("%s", event.getActionCommand);
    and then you can use the .parseInt

  • @i_am_reshad
    @i_am_reshad Před 4 lety

    thanks bucky

  • @manpildo
    @manpildo Před 12 lety

    Is it somehow possible to convert JTextField into an Integer like in JOptionPane? I want to make a calculator with it, but it says you can't use Integer.parseInt with JTextField.

  • @Tulanir1
    @Tulanir1 Před 11 lety

    If i want to click a JRadioButton as an option, and then click a button to confirm, i did this: I made 3 jradiobuttons and added them to the frame. Then i made a jbutton called confirm and added it to the frame. I made an object for a class called HandlerClass(which i hadn't created yet) i typed confirm.addActionHandler(handler); and then in the handlers actionPerformed method i want to test if the right button is true or false? How do i do this?

  • @sigh2infinity
    @sigh2infinity Před 12 lety

    what if i want multiple things to happen when a radio button is selected? do you just write them in the itemStateChanged method?

  • @Shaldares
    @Shaldares Před 12 lety

    @Claymore2408 A lot of the reason I'm here is because of minecraft, but I've been an amateur programmer for years. That's what I eventually want to become, so I'm getting stuff out of this either way. But yeah, I'm sure a lot of the ones that left were minecrafters.

  • @tomhas10
    @tomhas10 Před 11 lety

    The amount of times this has happened to me during this series...

  • @AlaaAlMaliki
    @AlaaAlMaliki Před 14 lety

    why we didn't use JOptionPane in here? wasn't supposed to bring us out the screen pop up?

  • @12me91
    @12me91 Před 12 lety

    why was handler done differently this time? do the radio buttons need a different way of doing it, from the check boxes, or is he just showing us all the ways it can be done?

  • @daybetter3605
    @daybetter3605 Před 6 lety

    please i got the error in the private class ?????
    private class HandlerClass implements ItemListener{
    private Font font ;
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    i got this message error
    gui.HandlerClass is not abstract and does not override abstract method itemStateChanged(ItemEvent) in ItemListener
    ----
    (Alt-Enter shows hints)

    • @bogdanpasca2300
      @bogdanpasca2300 Před 6 lety

      you need to override the method from ItemListener in HandlerClass :
      public void itemStateChanged(ItemEvent event)
      {
      tf.setFont(font);
      }

  • @weizhang915
    @weizhang915 Před 11 lety

    did he pass a font object to constructor? I dont think so. the handlerclass is to deal with the event. so it is nothing to do with constructor.

  • @robertngovan2284
    @robertngovan2284 Před 10 lety +3

    Why using itemlistener instead of actionlistener?which is the diference?

    • @iJediPVP
      @iJediPVP Před 10 lety +12

      The item listener checks if the state is changed. Either checked or unchecked. The action listener waits for an action such as a click.

    • @DjanilsonMartins
      @DjanilsonMartins Před 9 lety

      i_Jedi So, he could use both in this case, right?

    • @x_5768
      @x_5768 Před 9 lety

      +Djanilson Martins yes, i tried in my program and it works

    • @kshitizbaniya9692
      @kshitizbaniya9692 Před 6 lety

      we can use both itemlistener and actionlistener . doesn't matter which one you use.

  • @xrtze
    @xrtze Před 14 lety +1

    Great Video, just lauging bout
    "Bucky is awesome and hot... Oh, thank you!"
    XD

  • @RaiZRiZ
    @RaiZRiZ Před 10 lety

    If the handler class is made public and placed on its own in a new tab will the program still work the same.

    • @Lorithan
      @Lorithan Před 10 lety

      No, it won't have access to the private variables in the Gui class.

    • @RaiZRiZ
      @RaiZRiZ Před 10 lety

      Hunter Marshall Oh, thanks :)

    • @doruciubotaru7607
      @doruciubotaru7607 Před 9 lety

      Hunter Marshall indeed, in this case you can use setters and getters

  • @superstarben37
    @superstarben37 Před 12 lety

    Does anyone know why we also need to include the java.awt.event.*; part even though we have java.awt.*;?

  • @MajinHico
    @MajinHico Před 14 lety

    I want to as one question. He uses here
    xxx.setFont(yyy); with radio buttons.
    What i want to do, is to use
    xxx.setString(yyy); with radio buttons
    anyone help plz.
    How i can set String???

  • @deepak13ism
    @deepak13ism Před 12 lety

    Here we don't have any means to show the output to screen ( NO JOptionPane,no println....) but still the output is in the screen ...How is that possible??
    Help Wanted.....

  • @amitgaurav773
    @amitgaurav773 Před 7 lety

    Couldnt get why u constructed the handler constructor with font as argument

  • @nipunimalsara1551
    @nipunimalsara1551 Před 12 lety

    thanks a lot

  • @hayrow
    @hayrow Před 12 lety

    Yup, but one class may only implement one listener

  • @jacoblupone4531
    @jacoblupone4531 Před 9 lety

    What do I put if I'm trying to put up another GUI after this one?

  • @friendsforever7890
    @friendsforever7890 Před 11 lety

    hey.. nice work!! do you have any tutorial for calling JFrame on clicking any JMenuItem?

  • @aaron12343000
    @aaron12343000 Před 11 lety +1

    I get an error at this point: pf = new Font("Serif" Font.PLAIN, 14);
    and I cant resolve it. Please help!

    • @koungmeng
      @koungmeng Před 5 lety +1

      you forget comma. it should be "(Serif, Font.PLAIN,14); lol its been 5 years.

  • @maxdrew911
    @maxdrew911 Před 12 lety

    Bucky, dont make those promises, if you cant keep em lol

  • @Inkieto92
    @Inkieto92 Před 6 lety +1

    How come you didnt do it like this:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class GUI_JRadioButton extends JFrame{
    private JRadioButton plainB;
    private JRadioButton boldB;
    private JRadioButton italicB;
    private JRadioButton bothB;
    private JTextField myText;
    private ButtonGroup group;
    public GUI_JRadioButton(){
    super("JRadioButton Prog.");
    setLayout(new FlowLayout());
    myText = new JTextField("Type in some text!", 25);
    add(myText);
    plainB = new JRadioButton("Plain", true);
    boldB = new JRadioButton("Bold", false);
    italicB = new JRadioButton("Italic", false);
    bothB = new JRadioButton("Bold & Italic", false);
    add(plainB);
    add(boldB);
    add(italicB);
    add(bothB);
    group = new ButtonGroup();
    group.add(plainB);
    group.add(bothB);
    group.add(italicB);
    group.add(boldB);
    HandlerClass handler = new HandlerClass();
    plainB.addItemListener(handler);
    boldB.addItemListener(handler);
    italicB.addItemListener(handler);
    bothB.addItemListener(handler);
    }
    private class HandlerClass implements ItemListener{
    public void itemStateChanged(ItemEvent event){
    Font myFont = null;
    if(plainB.isSelected())
    myFont = new Font("Serif", Font.PLAIN, 25);
    else if(boldB.isSelected())
    myFont = new Font("Serif", Font.BOLD, 25);
    else if (italicB.isSelected())
    myFont = new Font("Serif", Font.ITALIC, 25);
    else
    myFont = new Font("Serif", Font.BOLD + Font.ITALIC, 25);
    myText.setFont(myFont);
    }
    }
    }
    Thats how you did your last program.

  • @dfsfklsj
    @dfsfklsj Před 12 lety

    can you have more than one class implementing ItemListener/ActionListener?

  • @TheRemonie
    @TheRemonie Před 10 lety

    I don't understand why he passed in pf, bf, itf and bif in the addItemListener thing?!

    • @garbo181
      @garbo181 Před 9 lety

      In 3:53 he writes the constructor for the HandlerClass. This constructor takes a Font as a parameter HandlerClass(Font f), so he has to pass a font when he calls it at lines 47,48,49,50. Hope it helped

  • @Islandinthesun87
    @Islandinthesun87 Před 12 lety

    does anyone get those errors:
    "HandlerClass cannot be resolved to a type"
    It looked like he had those errors himself, but he was still able to compile!!! how?!?!

  • @douglaswright4716
    @douglaswright4716 Před 11 lety

    For me it was a missing "d" at the end of "itemStateChanged".

  • @josephjohn5492
    @josephjohn5492 Před 8 lety +1

    what if i should use ActioniListener in place of ItemListener

    • @xAJarrett
      @xAJarrett Před 8 lety +1

      +joseph john It would work the same way in this instance but the two are different and have their purposes. For instance, with the previous tutorial where we wanted to know whether a box was checked or not, ActionListener would be inadequate. ActionListner would perform whenever it was clicked. Whereas the ItemListener would perform when it the box was checked.

  • @djdancealone3419
    @djdancealone3419 Před 7 lety

    Compiler and spellchecker .... double banger .... lolololol

  • @daniellive590
    @daniellive590 Před 11 lety

    Haha, so true. I have similar experience.

  • @FrostByte989
    @FrostByte989 Před 13 lety

    What's the difference between ItemListener and ActionListener?

  • @TheSweetdreams32
    @TheSweetdreams32 Před 10 lety

    Why don't you need to make an object for the handlerclass, like in the other tutorials? EDIT nvm.. i figured it out.. im stupid :)

  • @DanielCrabbprn
    @DanielCrabbprn Před 11 lety

    At first I was thinking why not just do "private Font pf, bf, itf, bif;" intead of each on their own line. then I was like why not just initialize them when you declare them instead of taking up an extra line of code each...
    "private Font pf = new Font("Serif", Font.PLAIN, 14);"

  • @madtuts1
    @madtuts1 Před 12 lety

    Wooohooo i made it to tutorial 67, nah jk I skipped it because the others were too basic for me.

  • @GeekyMe97
    @GeekyMe97 Před 12 lety

    @izaak791 Wow... First video I see with over 200 likes and 0 dislikes! :D
    He deserves it ^^

  • @No1in7Billion
    @No1in7Billion Před 11 lety

    Why I cannot do this
    private class HandlerClass implements ItemListener{
    public void itemStateChanged(ItemEvent event){
    private Font font;
    public HandlerClass(Font f){
    font = f;
    }
    tf.setFont(font);
    }
    }

  • @MonkeyGunman
    @MonkeyGunman Před 13 lety

    weird the buttons doesnt function on my computer.

  • @OBeymydreamz
    @OBeymydreamz Před 12 lety

    can you give me some hints on how you made it happen?

  • @pppppaaaaaccccchhh
    @pppppaaaaaccccchhh Před 12 lety

    true story, i misspelled HandlerClass too

  • @MrMetemer
    @MrMetemer Před 12 lety

    5:40 comment for easier breeding

  • @Saruman_Kaz
    @Saruman_Kaz Před 15 lety

    no1 cares if u're first, second etc..
    just stop spamming ffs!!
    nice vid.

  • @TheYetiNinja
    @TheYetiNinja Před 12 lety

    Real Bucky fans comment in his playlist. :D

  • @MrWill003
    @MrWill003 Před 12 lety

    apples and Gui extend JFrame

  • @morte3252
    @morte3252 Před 8 lety

    Four listeners? That's a bit much.

  • @josephjohn5492
    @josephjohn5492 Před 8 lety +2

    don't understand tf.setFont(font)

    • @ColtWillcox
      @ColtWillcox Před 8 lety +1

      font is f, and f is one of (pf, bf, itf, bif), depends what JRadioButton is clicked.

    • @josephjohn5492
      @josephjohn5492 Před 8 lety +1

      got it, thanks dude

  • @Anilkumar-ec8sj
    @Anilkumar-ec8sj Před 8 lety

    Getting problem with the source code , any body with the source code .

    • @xAJarrett
      @xAJarrett Před 8 lety +2

      +Anil kumar import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      public class Gui3 extends JFrame{
      private JTextField tf;
      private Font pf;
      private Font bf;
      private Font itf;
      private Font bif;
      private JRadioButton pb;
      private JRadioButton bb;
      private JRadioButton ib;
      private JRadioButton bib;
      private ButtonGroup group;public Gui3(){
      super("this new thing");
      setLayout(new FlowLayout());
      //text and size
      tf = new JTextField("bucky is awesome and hot", 25);
      add(tf);
      //
      pb = new JRadioButton("plain", true);
      bb = new JRadioButton("bold", false);
      ib = new JRadioButton("italic", false);
      bib = new JRadioButton("bold + italic", false);
      add(pb);
      add(bb);
      add(ib);
      add(bib);
      group = new ButtonGroup();
      group.add(pb);
      group.add(bb);
      group.add(ib);
      group.add(bib);
      pf = new Font("Serif", Font.PLAIN, 14);
      bf = new Font("Serif", Font.BOLD, 14);
      itf = new Font("Serif", Font.ITALIC, 14);
      bif = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
      tf.setFont(pf);
      pb.addItemListener(new HandlerClass(pf));
      bb.addItemListener(new HandlerClass(bf));
      ib.addItemListener(new HandlerClass(itf));
      bib.addItemListener(new HandlerClass(bif));
      }
      private class HandlerClass implements ItemListener{
      private Font font;
      public HandlerClass(Font f){
      font = f;
      }
      public void itemStateChanged(ItemEvent event){
      tf.setFont(font);
      }

      }
      }

  • @tarikpolat
    @tarikpolat Před 7 lety

    I just have a question, I will copy and paste the whole code that I wrote but you don't have to read that all. The only difference is this:
    bucky wrote font=f, but I wrote f=font in the public HandlerClass(Font f){} (so in the constructor of private class HandlerClass). And my code worked but didn't change the font when I clicked the radio buttons...
    Here is the whole code that I wrote, but as I said the only difference is the code in the constructor of the private class HandlerClass:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class JRadioButtonBackground extends JFrame{
    private JTextField tf;
    private Font pf;
    private Font bf;
    private Font itf;
    private Font bif;
    private JRadioButton pb;
    private JRadioButton bb;
    private JRadioButton ib;
    private JRadioButton bib;
    private ButtonGroup group;
    public JRadioButtonBackground(){
    super("the title");
    setLayout(new FlowLayout());
    tf = new JTextField("This is a sentence.", 20);
    add(tf);
    pb = new JRadioButton("plain", false);
    bb = new JRadioButton("bold", true);
    ib = new JRadioButton("italic", false);
    bib = new JRadioButton("bold and italic", false);
    add(pb);
    add(bb);
    add(ib);
    add(bib);
    group = new ButtonGroup();
    group.add(pb);
    group.add(bb);
    group.add(ib);
    group.add(bib);
    pf = new Font("Serif", Font.PLAIN, 14);
    bf = new Font("Serif", Font.BOLD, 14);
    itf = new Font("Serif", Font.ITALIC, 14);
    bif = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
    tf.setFont(bf);
    //wait for event to happen, pass in font object to constructor
    pb.addItemListener(new HandlerClass(pf));
    bb.addItemListener(new HandlerClass(bf));
    ib.addItemListener(new HandlerClass(itf));
    bib.addItemListener(new HandlerClass(bif));
    }
    private class HandlerClass implements ItemListener{
    private Font font;
    //the font object gets variable font
    public HandlerClass(Font f){
    f = font;
    }
    //sets the font to the font object that was passed in
    public void itemStateChanged(ItemEvent event){
    tf.setFont(font);
    }
    }
    }

    • @youssefhatem9083
      @youssefhatem9083 Před 7 lety

      you should do that : font=f
      because f here is the font that will be sent to the constructor

    • @tarikpolat
      @tarikpolat Před 7 lety

      Yousef Hatem Thank you but I am not sure if I understand your answer.
      So?: If you use a constructor with a parameter in it, you should use that parameter on the right side of the equality

    • @youssefhatem9083
      @youssefhatem9083 Před 7 lety +1

      for example when this line of code running pb.addItemListener(new HandlerClass(pf));
      it sends to the constructor pf so f will equal to pf and because we want to set the font of the text to whatever we choose from the RadioButton (in this example it's pf) we make a variable which named font and then set font equal to f(pf) so we now have the font so we can easily set the text to that font

    • @tarikpolat
      @tarikpolat Před 7 lety

      Yousef Hatem And now I understand it well :). Thank you so much that you tried to explain it in another way.

    • @youssefhatem9083
      @youssefhatem9083 Před 7 lety

      you are welcome :)

  • @Dimthegamer115
    @Dimthegamer115 Před 11 lety

    *spends 6 days trying to work out why my program isn't working finally notices the I at the start of "itemStateChanged" isn't a capital*..... are you really fucking kidding me right now. *flips table*