Java Programming Tutorial – 67 – JRadioButton Final Program

[ad_1]
Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –


Posted

in

by

Tags:

Comments

34 responses to “Java Programming Tutorial – 67 – JRadioButton Final Program”

  1. Rob V Avatar

    Java Code, Demos, Tutorials,…  Helped me a lot – Happy Coding!  http://yabuilder.com/9hMB

  2. Mrmagical Rabbit Avatar

    the viewers dropped significantly not much determination to learn and create something

  3. Amit Gaurav Avatar

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

  4. Tarık POLAT Avatar

    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);
    }
    }
    }

  5. Darnell Stewart Honda Avatar

    Compiler and spellchecker …. double banger …. lolololol

  6. Harshit Khandelwal Avatar

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

    public class help extends JFrame{

    private JTextField tf;
    private JRadioButton pb;
    private JRadioButton bb;
    private JRadioButton ib;
    private JRadioButton bib;
    private Font plf;
    private Font blf;
    private Font itf;
    private Font bif;
    private ButtonGroup group;

    public help(){
    super("the title");
    setLayout(new FlowLayout());
    tf = new JTextField("write your destiny",36);
    add(tf);
    pb = new JRadioButton("PLAIN",true);
    bb = new JRadioButton("ITALIC",false);
    ib = new JRadioButton("ITALIC",false);
    bib = new JRadioButton("BOLDITALIC",false);
    add(pb);
    add(bb);
    add(ib);
    add(bib);

    group = new ButtonGroup();
    group.add(pb);
    group.add(bb);
    group.add(ib);
    group.add(bib);

    plf = new Font("serif",Font.PLAIN,14);
    blf = new Font("serif",Font.BOLD,14);
    itf = new Font("serif",Font.ITALIC,14);
    bif= new Font("serif",Font.BOLD + Font.ITALIC,14);

    thehandler handler = new thehandler();
    pb.addActionListener(handler);
    ib.addActionListener(handler);
    bb.addActionListener(handler);
    bib.addActionListener(handler);

    }
    public class thehandler implements ActionListener{

    public void actionPerformed(ActionEvent event){
    if(event.getSource()==pb)
    tf.setFont(plf);
    else if(event.getSource()==bb)
    tf.setFont(blf);
    else if(event.getSource()==ib)
    tf.setFont(itf);
    else if(event.getSource()==bib)
    tf.setFont(bif);
    }
    }

    }

    this code is doing the same work,in this code i use action listener , so why do we need to use itemlistener.

  7. Amin A Avatar

    Hi guys , could someone tell me about my last part coding which I wrote based on the previous tutorial and it worked.

    import java.awt.FlowLayout;
    import java.awt.Font;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JRadioButton;
    import javax.swing.ButtonGroup;
    import java.awt.event.ItemListener;
    import java.awt.event.ItemEvent;

    public class Gui extends JFrame {

    private JTextField tf;
    private JRadioButton pb;
    private JRadioButton bb;
    private JRadioButton ib;
    private JRadioButton bib;
    private Font pf;
    private Font bf;
    private Font itf;
    private Font bif;
    private ButtonGroup group;

    public Gui(){
    super("THIS IS TITLE");
    setLayout(new FlowLayout());

    tf = new JTextField("This is Test", 25);
    add(tf);

    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 = new JRadioButton("Normal", false);
    bb = new JRadioButton("Bold", false);
    ib = new JRadioButton ("Italic",false);
    bib = new JRadioButton ("Both", false);
    add(pb);
    add(bb);
    add(ib);
    add(bib);

    group = new ButtonGroup ();
    group.add(pb);
    group.add(bb);
    group.add(ib);
    group.add(bib);

    // for below coding I just followed the previous tutorial
    thehandeler handel = new thehandeler();
    pb.addItemListener(handel);
    bb.addItemListener(handel);
    ib.addItemListener(handel);
    bib.addItemListener(handel);

    }

    private class thehandeler implements ItemListener{

    public void itemStateChanged(ItemEvent e) {

    if(pb.isSelected())
    tf.setFont(pf);
    else if (bb.isSelected())
    tf.setFont(bf);
    else if (ib.isSelected())
    tf.setFont(itf);
    else if (bib.isSelected())
    tf.setFont(bif);

    }
    }
    }

  8. gay mario Avatar

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

  9. Wenk Hsueh Avatar

    Four listeners? That's a bit much.

  10. ahmad ameen Avatar

    Execute those six, please…

  11. Arveer Singh Avatar

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

  12. Anil kumar Avatar

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

  13. Shashwat Khanna Avatar

    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);
    }

  14. joseph john Avatar

    don't understand tf.setFont(font)

  15. joseph john Avatar

    what if i should use ActioniListener in place of ItemListener

  16. Brock Jaeger Avatar

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

  17. josh hamil Avatar

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

  18. A Shell Of My Former Glorious Self Avatar

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

  19. Risto Bozinov Avatar

    3:00 Let the party start!

  20. BHGoalie Avatar

    I Have an error in my class apples for the methods:
    go.setDefaultCloseOperation
    go.setSize
    go.setVisible
    it says create these methods in my Gui class what do i do?

  21. Jacob Lupone Avatar

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

  22. Jacob Lupone Avatar

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

  23. Doru Ciubotaru Avatar

    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);
                }

  24. Bob The Zealot Avatar

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

  25. Jordan Shackelford Avatar

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

  26. TheRemonie Avatar

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

  27. Robert NgoVan Avatar

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

  28. RaiZ Avatar

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

  29. freekupo Avatar

    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!

  30. Ohad Marbach Avatar

    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?

  31. ray beek Avatar

    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 🙂

  32. Innovater6 Avatar

    Wow seems like ages ago I started watching this playlist 😀

  33. No1in7Billion Avatar

    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);
    }
    }

  34. Daniel Crabb Avatar

    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);"

Leave a Reply

Your email address will not be published. Required fields are marked *