Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

JComboBox - Simple Version !

combobox

  • Please log in to reply
9 replies to this topic

#1 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 29 January 2009 - 03:12 PM

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
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
Here we are importing the parts needed to create our application !
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 !
JComboBox Answer = new JComboBox();
JLabel Welcome = new JLabel();

So here is our Box that we are going to use !
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
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);
	}
The public part is our constructor(since we need to create a layout it will always be done by our constructor !)
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.
public void actionPerformed(ActionEvent e) {
		String Name = (String) Answer.getSelectedItem();
		Welcome.setText("Welcome "+Name+" to CodeCall!");
		Answer.addItem(Name);
	}
While we listing for event changes, while it happens it will add in our text to the box and print out a "welcome" phrase !

The Main
public static void main(String[] arg) {
		TestTest Test = new TestTest();
	}
}
So I hope you guys enjoy this tutorial as I did !

Also the whole code!
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();
	}
}
Screenie for you guys ;)
[ATTACH]1301[/ATTACH]

Attached Thumbnails

  • output.PNG

  • 1

#2 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 29 January 2009 - 05:03 PM

Nice job! +rep
  • 0

#3 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 29 January 2009 - 10:51 PM

Nice job! +rep


Thank you !
  • 0

#4 mendim.

mendim.

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 741 posts

Posted 30 January 2009 - 10:53 AM

Great Job , Turk4n .. :) +rep
  • 0

#5 Egz0N

Egz0N

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1155 posts

Posted 30 January 2009 - 03:01 PM

nice job mate .. and +rep :)
  • 0

#6 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 31 January 2009 - 01:01 AM

Great Job , Turk4n .. :) +rep


Thanks :D

nice job mate .. and +rep :)


Thank you
  • 0

#7 Guest_arslan220_*

Guest_arslan220_*
  • Guest

Posted 27 July 2009 - 02:08 AM

thanks for that.
  • 0

#8 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 27 July 2009 - 11:14 AM

thanks for that.


Lol, thanks why so banned?
  • 0

#9 Miharu

Miharu

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 07 August 2009 - 08:11 PM

Thanks +rep
  • 0

#10 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 07 August 2009 - 09:22 PM

Thanks +rep


You are welcome.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download