Hey C.C, I hope you enjoy tutorials since I enjoy to share knowledge with others hope you guys feel the same !
Today I will try to introduce you guys to JComboBox with Editable boxes, FUN !
So let's get started !
Since I presume people have worked around with the swing package so making this wouldn't/shouldn't be a bother for you guys to understand !
However I will do my best to make it understand able !
The imports
Here we are importing the parts needed to create our application !Code:import java.awt.*; import javax.swing.*; import java.awt.event.*;
Swing for the gui, awt for allowing us setting up layouts ! awt.event for allowing us to see changes/change/edit/listen !
The variables we need for our function !
So here is our Box that we are going to use !Code:JComboBox Answer = new JComboBox(); JLabel Welcome = new JLabel();
JComboBox is common used when making a text list, example, name list; type your name check in others just view it up !
JLabel is simple, a label containing just pure text !
Layout
The public part is our constructor(since we need to create a layout it will always be done by our constructor !)Code:public TestTest() { setLayout(new FlowLayout()); add(new JLabel("What's your name?",JLabel.RIGHT)); add(Answer); add(Welcome); Welcome.setHorizontalAlignment(JLabel.LEFT); Answer.setEditable(true); Answer.addActionListener(this); setSize(350,90); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); }
By applying a flowlayout we will be able to set a "flow" in the frame !
Hence we will be able to use the layout whenever we want, kinda dynamically.
By using Add we add them to our layout and after we have added we have to give them proper position !
As I have been talking about our box will be editable meaning we can enter how much information we want to and it will remember it.
Adding the ActionListener will allow us to view "changes" clicks,and movements !
setSize is no problem nor setVisible same goes with setDefaultCloseOperation !
Simple things that should be understood !
Our listener, while making applications and want to make it "do" something.
While we listing for event changes, while it happens it will add in our text to the box and print out a "welcome" phrase !Code:public void actionPerformed(ActionEvent e) { String Name = (String) Answer.getSelectedItem(); Welcome.setText("Welcome "+Name+" to CodeCall!"); Answer.addItem(Name); }
The Main
So I hope you guys enjoy this tutorial as I did !Code:public static void main(String[] arg) { TestTest Test = new TestTest(); } }
Also the whole code!
Screenie for you guysCode:import java.awt.*; import javax.swing.*; import java.awt.event.*; JComboBox Answer = new JComboBox(); JLabel Welcome = new JLabel(); public TestTest() { setLayout(new FlowLayout()); add(new JLabel("What's your name?",JLabel.RIGHT)); add(Answer); add(Welcome); Welcome.setHorizontalAlignment(JLabel.LEFT); Answer.setEditable(true); Answer.addActionListener(this); setSize(350,90); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { String Name = (String) Answer.getSelectedItem(); Welcome.setText("Welcome "+Name+" to CodeCall!"); Answer.addItem(Name); } public static void main(String[] arg) { TestTest Test = new TestTest(); } }
![]()
Nice job! +rep
Great Job , Turk4n ..+rep
nice job mate .. and +rep![]()
thanks for that.
Thanks +rep
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks