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.
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); }
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!
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.
@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.
@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.
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);
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 .
@ 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
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
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.
@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!
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?
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 :)
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); }
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!
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
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?
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.
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?
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.
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?
@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.
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?
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)
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???
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.....
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.
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
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?!?!
+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.
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);"
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); } }
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); } } }
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
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
*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*
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.
If you are watching this you are the last 4% of the viewers since video 1.
And here we are! \m/
Right...
It's like 3% now. Dang!
I didn't watch video 1
The 3% that might have learned something after all this.
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);
}
i am using that 1
nice
3 people are still sick of that sandwich story :D
bucky: "im just a plain old guy."
me: "no, you are cool and awesome!"
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!
The valuable part is from 3:00 to 3:05.
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.
wow! i wrote 60 lines of code for the first time and didn't get bored.
I am so happy, I managed to complete the program by only watching up to 4:00!
@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.
wow this is the first time that you are using commentaries in java in this tutorial series
@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.
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);
Now for your "buckies greatest hits!" 0:45 "phlem phlem phlem phelm"
3:00
"handelerelerelrelrelr'
Actually he says: "Font! Font, font font! xD
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 .
I know the video is old, but I thought I would thank you for putting comments in the program :) It's always helpful.
@ 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
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.
your awesome dude learn more from you than my university lecturer
Wow seems like ages ago I started watching this playlist :D
After seeing your comment i decided to try the same :) took me some time but eventually figured it out
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
@ 5:35: I rewatched the video bucky. And you did it again :D.
3:00 Let the party start!
This was exactly what I was looking for my program I was making.
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.
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
@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!
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?
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 :)
You're like 10 years younger than me, but I wish you were one of my teachers in my high school.
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);
}
i one of the greatest thing i did in my life is SUBSCRIBING TO THENEWBOSTON --> LOVE IT
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!
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
My text isn't changing style when I click a different radio button. It stays plain.
Jordan Shackelford Check with the handler or constructor or font you have set in
maybe you didn't do the add()?
Bucky, your green lightsaber is missing it's handle :O
I like how this video has no dislikes :)
If i want to make Objects seperately for Handlerclass like in the previous video, how many do I have to make ?
+josh hamil you just have to make 1 object for the Handlerclass to access it.
Did anyone instinctively yell "bullshit" at 5:37? I
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?
EventListener is just a tagging interface that every listener like ActionListener must extend.
So, if I want different outcomes for each radio button, I have to make a different handler class for them?
Brilliant Tutorial =D
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.
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!
god bless all no matter what!
double banger!
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?
i followed most of it pretty clearly but the passing of the font at the end got very confusing for me.
convert it to a String , String nickiminaj = String.format("%s", event.getActionCommand);
and then you can use the .parseInt
thanks bucky
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.
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?
what if i want multiple things to happen when a radio button is selected? do you just write them in the itemStateChanged method?
@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.
The amount of times this has happened to me during this series...
why we didn't use JOptionPane in here? wasn't supposed to bring us out the screen pop up?
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?
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)
you need to override the method from ItemListener in HandlerClass :
public void itemStateChanged(ItemEvent event)
{
tf.setFont(font);
}
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.
Why using itemlistener instead of actionlistener?which is the diference?
The item listener checks if the state is changed. Either checked or unchecked. The action listener waits for an action such as a click.
i_Jedi So, he could use both in this case, right?
+Djanilson Martins yes, i tried in my program and it works
we can use both itemlistener and actionlistener . doesn't matter which one you use.
Great Video, just lauging bout
"Bucky is awesome and hot... Oh, thank you!"
XD
If the handler class is made public and placed on its own in a new tab will the program still work the same.
No, it won't have access to the private variables in the Gui class.
Hunter Marshall Oh, thanks :)
Hunter Marshall indeed, in this case you can use setters and getters
Does anyone know why we also need to include the java.awt.event.*; part even though we have java.awt.*;?
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???
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.....
Couldnt get why u constructed the handler constructor with font as argument
thanks a lot
Yup, but one class may only implement one listener
What do I put if I'm trying to put up another GUI after this one?
hey.. nice work!! do you have any tutorial for calling JFrame on clicking any JMenuItem?
I get an error at this point: pf = new Font("Serif" Font.PLAIN, 14);
and I cant resolve it. Please help!
you forget comma. it should be "(Serif, Font.PLAIN,14); lol its been 5 years.
Bucky, dont make those promises, if you cant keep em lol
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.
can you have more than one class implementing ItemListener/ActionListener?
I don't understand why he passed in pf, bf, itf and bif in the addItemListener thing?!
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
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?!?!
For me it was a missing "d" at the end of "itemStateChanged".
what if i should use ActioniListener in place of ItemListener
+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.
Compiler and spellchecker .... double banger .... lolololol
Heihachi Honda lol XD
Haha, so true. I have similar experience.
What's the difference between ItemListener and ActionListener?
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 :)
ray beek why?
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);"
Wooohooo i made it to tutorial 67, nah jk I skipped it because the others were too basic for me.
@izaak791 Wow... First video I see with over 200 likes and 0 dislikes! :D
He deserves it ^^
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);
}
}
weird the buttons doesnt function on my computer.
can you give me some hints on how you made it happen?
true story, i misspelled HandlerClass too
5:40 comment for easier breeding
no1 cares if u're first, second etc..
just stop spamming ffs!!
nice vid.
Real Bucky fans comment in his playlist. :D
apples and Gui extend JFrame
Four listeners? That's a bit much.
don't understand tf.setFont(font)
font is f, and f is one of (pf, bf, itf, bif), depends what JRadioButton is clicked.
got it, thanks dude
Getting problem with the source code , any body with the source code .
+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);
}
}
}
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);
}
}
}
you should do that : font=f
because f here is the font that will be sent to the constructor
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
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
Yousef Hatem And now I understand it well :). Thank you so much that you tried to explain it in another way.
you are welcome :)
*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*