[ad_1]
Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –
Java Programming Tutorial – 67 – JRadioButton Final Program
by
Tags:
Comments
34 responses to “Java Programming Tutorial – 67 – JRadioButton Final Program”
-
Java Code, Demos, Tutorials,… Helped me a lot – Happy Coding! http://yabuilder.com/9hMB
-
the viewers dropped significantly not much determination to learn and create something
-
Couldnt get why u constructed the handler constructor with font as argument
-
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);
}
}
} -
Compiler and spellchecker …. double banger …. lolololol
-
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.
-
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);}
}
} -
Now for your "buckies greatest hits!" 0:45 "phlem phlem phlem phelm"
-
Four listeners? That's a bit much.
-
Execute those six, please…
-
wow! i wrote 60 lines of code for the first time and didn't get bored.
-
Getting problem with the source code , any body with the source code .
-
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);
} -
don't understand tf.setFont(font)
-
what if i should use ActioniListener in place of ItemListener
-
If you are watching this you are the last 4% of the viewers since video 1.
-
If i want to make Objects seperately for Handlerclass like in the previous video, how many do I have to make ?
-
bucky: "im just a plain old guy."
me: "no, you are cool and awesome!" -
3:00 Let the party start!
-
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? -
What do I put if I'm trying to put up another GUI after this one?
-
So, if I want different outcomes for each radio button, I have to make a different handler class for them?
-
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);
} -
My text isn't changing style when I click a different radio button. It stays plain.
-
I don't understand why he passed in pf, bf, itf and bif in the addItemListener thing?!
-
Why using itemlistener instead of actionlistener?which is the diference?
-
If the handler class is made public and placed on its own in a new tab will the program still work the same.
-
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!
-
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? -
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 🙂
-
Wow seems like ages ago I started watching this playlist 😀
-
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);
}
} -
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