Hello,since i like coding on java language and i see inspiring contributions on utopian i decided to start a java course.In this course you are about to learn everything about java language.In the end of the course i am going to create a website for utopian.io and spend all my income from these tutorials to improve this amazing project hopefully.
First of all i am defining my libraries that i need for this program.
applet.applet is to create a applet,"awt.list" to list my defined variables and we can create a horizontal menu with using this library,and the other two libraries for to list our libraries especially itemlistener library is used to implement variables on applet.
import java.applet.Applet;
import java.awt.List;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
Now i am defining my class and implementing "itemlistener" on my class.
public class ListOne extends Applet implements ItemListener
At this step i am creating my menu with using Strings.I decided to create a automobile menu for a simple website.To do that we needed few brands,price of brand models and models.
I defined them and set my price range to make user choose for their budget.
String Brand[] = {"Audi", "Bmw", "Mercedes-Benz", "Pagani"};
String Price[][] = {
{"<10.000$", "<20.000$","<30.000$"},
{"<15.000$", "<45.000$", "<100.000$"},
{"<20.000$", "<40.000$", "<80.000$","<250.000$"},
{"<2.500.000$"}
};
String Cars[][][] = {
{
{"A1", "A3"}, {"A5"} , {"A8"}
},
{
{"1.30"}, {"3.20", "3.30", "5.30"}, {"7.50"}
},
{
{"A180"}, {"C180", "CLA180", "E180"}, {"E250"},{"S350","MAYBACH"}
},
{
{"Huayra"}
}
Now we defined our menu but we need to use "list" to define our strings on our menu and show it to user.With doing this we basically created a menu and used "list" to make a menu.
List brand = new List();
List price = new List();
List cars = new List();
Now i am creating a new class "init" when you define string,integer etc. you can use "init" class to assign them.Then i set layout as "null" this means i made my menu look like regular horizontal because i am going to define them below.
Then i set my menus location and size of the menu."Setmultiplemode" is used for to not allow user choose two options in same time.
public void init() {
setLayout(null);
brand.setLocation(10, 10);
brand.setSize(95, 90);
add(brand);
brand.setMultipleMode(false);
brand.addItemListener(this);
price.setLocation(130, 10);
price.setSize(100, 80);
add(price);
price.setMultipleMode(false);
price.addItemListener(this);
cars.setLocation(250, 10);
cars.setSize(100, 80);
add(cars);
cars.setMultipleMode(false);
all();
}
You can see i used "all" above class.Now im creating a new class for "all" and using for loop to list the brands that i defined."Brand[i]" part i used array to assign users input.
public void all() {
for (int i = 0; i < Brand.length; i++) {
brand.add(Brand[i]);
}
}
In the final class we are using "itemstatechanged" method.This method assigns to address when an item has been selected or unselected by user.
We evaluate if user chooses another brand all the prices will be removed from the regular menu.I used "Removeall" method this method removes current menu.
Then we used "getselectedindex" which is basically used to show program what user have choosed from the menu,then we use for loop again to list the prices on menu screen.
Finally we use "else if" if user chooses brand-price but then chooses another brand or price we use "getslectedindex" again to see what user choosed from the menu then used for loop again to list new menu that user choosed and assign them into array that i defined above named "choosen" and "choosen2".
public void itemStateChanged(ItemEvent abc) {
if (abc.getSource() == brand) {
price.removeAll();
int choosen = brand.getSelectedIndex();
for (int i = 0; i < Price[choosen].length; i++) {
price.add(Price[choosen][i]);
}
}
else if(abc.getSource()==price){
cars.removeAll();
int Choosen = brand.getSelectedIndex();
int Choosen2 = price.getSelectedIndex();
for (int i = 0; i < Cars[Choosen][Choosen2].length; i++) {
cars.add(Cars[Choosen][Choosen2][i]);
import java.applet.Applet;
import java.awt.List;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
@SuppressWarnings("serial")
public class ListOne extends Applet implements ItemListener {
String Brand[] = {"Audi", "Bmw", "Mercedes-Benz", "Pagani"};
String Price[][] = {
{"<10.000$", "<20.000$","<30.000$"},
{"<15.000$", "<45.000$", "<100.000$"},
{"<20.000$", "<40.000$", "<80.000$","<250.000$"},
{"<2.500.000$"}
};
String Cars[][][] = {
{
{"A1", "A3"}, {"A5"} , {"A8"}
},
{
{"1.30"}, {"3.20", "3.30", "5.30"}, {"7.50"}
},
{
{"A180"}, {"C180", "CLA180", "E180"}, {"E250"},{"S350","MAYBACH"}
},
{
{"Huayra"}
}
};
List brand = new List();
List price = new List();
List cars = new List();
public void init() {
setLayout(null);
brand.setLocation(10, 10);
brand.setSize(95, 90);
add(brand);
brand.setMultipleMode(false);
brand.addItemListener(this);
price.setLocation(130, 10);
price.setSize(100, 80);
add(price);
price.setMultipleMode(false);
price.addItemListener(this);
cars.setLocation(250, 10);
cars.setSize(100, 80);
add(cars);
cars.setMultipleMode(false);
all();
}
public void all() {
for (int i = 0; i < Brand.length; i++) {
brand.add(Brand[i]);
}
}
public void itemStateChanged(ItemEvent abc) {
if (abc.getSource() == brand) {
price.removeAll();
int choosen = brand.getSelectedIndex();
for (int i = 0; i < Price[choosen].length; i++) {
price.add(Price[choosen][i]);
}
}
else if(abc.getSource()==price){
cars.removeAll();
int Choosen = brand.getSelectedIndex();
int Choosen2 = price.getSelectedIndex();
for (int i = 0; i < Cars[Choosen][Choosen2].length; i++) {
cars.add(Cars[Choosen][Choosen2][i]);
}
}
}
}