PDA

View Full Version : Java Help: My first Applet



Shelnutt2
03-25-2006, 07:27 AM
After some delays I'm back to trying to write my *simple* java program. I just started working on a GUI, and applet. This is my first attempt and I'm really stuck here. I've created the first half of it, I've got it were the user can choose the amounts for each drop list. I'm using Java GUI Builder to visual build the GUI, then JCreator to modify the code. Now what I need to do is be able to (as seen in my command line version) take what the user selects, subtract it from a defined number, then get the computer to print it out on the other side of the screen. How do I do this? Where can I go to get information on the next step I need? I had a few Java books but I had to return to the library a while ago.

I'm modeling the applet after this command line version of my program.

http://24.110.34.82/Command%20Line.java.txt

Here is my code so far for my applet version.

http://24.110.34.82/GUI2.java.txt

Thanks guys

Hell-Fire
03-27-2006, 07:22 PM
I cant view either link.

Are you asking how to grab the things the people select from the drop down boxes?

I need to see your source code.

Shelnutt2
03-28-2006, 02:42 AM
here is my source code. Yes I want to take what the user selects out of the drop down menu and then have it comput the selected value, then spit out the results. This might also be a little advanced for em right now, but I want to also make it were the first drop down list is where the person selcets the number of subsequent drop down list. That way if someone needs more, or less they are able to have the right amount of drop down list. Also In my code below it shows that all the drop down list have different chocies...as soon as I get a list from a tech guy all the list will be the same, so I can just add some loops to create the preceding drop down menus?

ok I think I have an illistartion to explain what I'm trying to do...


User selects first value to be 3. So now there are 3 drop down list below.

User selects value 3 -> program does 4-3 -> spits out 1
User selects value .5 -> program deos 1-.5 -> spits out .5
User selects value 1 -> progam does 1-1 -> spits out 0



import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

class gui extends Applet {
Label textfield_1;
Choice choice_1;
Choice choice_2;
Choice choice_3;
Choice choice_4;
Choice choice_5;
Choice choice_6;
Choice choice_7;
Choice choice_8;
Choice choice_9;
Choice choice_10;
Choice choice_11;
Choice choice_12;
Label textfield_2;
Label textfield_3;
Label textfield_4;
Label textfield_5;
Label textfield_6;
Label textfield_7;
Label textfield_8;
Label textfield_9;
Label textfield_10;
Label textfield_11;
Label textfield_12;
Label textfield_13;
Label textfield_14;
TextField textfield_15;
TextField textfield_16;
TextField textfield_17;
TextField textfield_18;
TextField textfield_19;
TextField textfield_20;
TextField textfield_21;
TextField textfield_22;
TextField textfield_23;
TextField textfield_24;
TextField textfield_25;
TextField textfield_26;
Label textfield_27;
Choice choice_13;
Label textfield_28;
Label textfield_29;

public void init() {
guiLayout customLayout = new guiLayout();

setFont(new Font("Helvetica", Font.PLAIN, 12));
setLayout(customLayout);

textfield_1 = new Label("This program will tell you how many credits you need to graduate");
add(textfield_1);

choice_1 = new Choice();
choice_1.addItem("item1");
choice_1.addItem("item2");
add(choice_1);

choice_2 = new Choice();
choice_2.addItem("item1");
choice_2.addItem("item2");
add(choice_2);

choice_3 = new Choice();
choice_3.addItem("item1");
choice_3.addItem("item2");
add(choice_3);

choice_4 = new Choice();
choice_4.addItem("item1");
choice_4.addItem("item2");
add(choice_4);

choice_5 = new Choice();
choice_5.addItem("item1");
choice_5.addItem("item2");
add(choice_5);

choice_6 = new Choice();
choice_6.addItem("item1");
choice_6.addItem("item2");
add(choice_6);

choice_7 = new Choice();
choice_7.addItem("item1");
choice_7.addItem("item2");
add(choice_7);

choice_8 = new Choice();
choice_8.addItem("item1");
choice_8.addItem("item2");
add(choice_8);

choice_9 = new Choice();
choice_9.addItem("item1");
choice_9.addItem("item2");
add(choice_9);

choice_10 = new Choice();
choice_10.addItem("item1");
choice_10.addItem("item2");
add(choice_10);

choice_11 = new Choice();
choice_11.addItem("item1");
choice_11.addItem("item2");
add(choice_11);

choice_12 = new Choice();
choice_12.addItem("item1");
choice_12.addItem("item2");
add(choice_12);

textfield_2 = new Label("language Arts Credits");
add(textfield_2);

textfield_3 = new Label("Mathematics credits");
add(textfield_3);

textfield_4 = new Label("Science credits");
add(textfield_4);

textfield_5 = new Label("World History credits");
add(textfield_5);

textfield_6 = new Label("American History credits");
add(textfield_6);

textfield_7 = new Label("Econimic credits");
add(textfield_7);

textfield_8 = new Label("American Goverment credits");
add(textfield_8);

textfield_9 = new Label("Practical/Fine Art credits");
add(textfield_9);

textfield_10 = new Label("Life Management Skill credits");
add(textfield_10);

textfield_11 = new Label("Personal Fitness credits");
add(textfield_11);

textfield_12 = new Label("Personal Education credits");
add(textfield_12);

textfield_13 = new Label("Elective credits");
add(textfield_13);

textfield_14 = new Label("These are the credits you still");
add(textfield_14);

textfield_15 = new TextField("textfield_15");
add(textfield_15);

textfield_16 = new TextField("textfield_16");
add(textfield_16);

textfield_17 = new TextField("textfield_17");
add(textfield_17);

textfield_18 = new TextField("textfield_18");
add(textfield_18);

textfield_19 = new TextField("textfield_19");
add(textfield_19);

textfield_20 = new TextField("textfield_20");
add(textfield_20);

textfield_21 = new TextField("textfield_21");
add(textfield_21);

textfield_22 = new TextField("textfield_22");
add(textfield_22);

textfield_23 = new TextField("textfield_23");
add(textfield_23);

textfield_24 = new TextField("textfield_24");
add(textfield_24);

textfield_25 = new TextField("textfield_25");
add(textfield_25);

textfield_26 = new TextField("textfield_26");
add(textfield_26);

textfield_27 = new Label("need for each subject");
add(textfield_27);

choice_13 = new Choice();
choice_13.addItem("item1");
choice_13.addItem("item2");
add(choice_13);

textfield_28 = new Label("How many classes");
add(textfield_28);

textfield_29 = new Label("s have you taken");
add(textfield_29);

setSize(getPreferredSize());

}

public static void main(String args[]) {
gui applet = new gui();
Frame window = new Frame("gui");

window.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

applet.init();
window.add("Center", applet);
window.pack();
window.setVisible(true);
}
}

class guiLayout implements LayoutManager {

public guiLayout() {
}

public void addLayoutComponent(String name, Component comp) {
}

public void removeLayoutComponent(Component comp) {
}

public Dimension preferredLayoutSize(Container parent) {
Dimension dim = new Dimension(0, 0);

Insets insets = parent.getInsets();
dim.width = 975 + insets.left + insets.right;
dim.height = 891 + insets.top + insets.bottom;

return dim;
}

public Dimension minimumLayoutSize(Container parent) {
Dimension dim = new Dimension(0, 0);
return dim;
}

public void layoutContainer(Container parent) {
Insets insets = parent.getInsets();

Component c;
c = parent.getComponent(0);
if (c.isVisible()) {c.setBounds(insets.left+304,insets.top+8,408,32); }
c = parent.getComponent(1);
if (c.isVisible()) {c.setBounds(insets.left+224,insets.top+136,72,24) ;}
c = parent.getComponent(2);
if (c.isVisible()) {c.setBounds(insets.left+224,insets.top+168,72,24) ;}
c = parent.getComponent(3);
if (c.isVisible()) {c.setBounds(insets.left+224,insets.top+200,72,24) ;}
c = parent.getComponent(4);
if (c.isVisible()) {c.setBounds(insets.left+224,insets.top+232,72,24) ;}
c = parent.getComponent(5);
if (c.isVisible()) {c.setBounds(insets.left+224,insets.top+264,72,24) ;}
c = parent.getComponent(6);
if (c.isVisible()) {c.setBounds(insets.left+224,insets.top+296,72,24) ;}
c = parent.getComponent(7);
if (c.isVisible()) {c.setBounds(insets.left+224,insets.top+328,72,24) ;}
c = parent.getComponent(8);
if (c.isVisible()) {c.setBounds(insets.left+224,insets.top+360,72,24) ;}
c = parent.getComponent(9);
if (c.isVisible()) {c.setBounds(insets.left+224,insets.top+392,72,24) ;}
c = parent.getComponent(10);
if (c.isVisible()) {c.setBounds(insets.left+224,insets.top+424,72,24) ;}
c = parent.getComponent(11);
if (c.isVisible()) {c.setBounds(insets.left+224,insets.top+456,72,24) ;}
c = parent.getComponent(12);
if (c.isVisible()) {c.setBounds(insets.left+224,insets.top+488,72,24) ;}
c = parent.getComponent(13);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+136,184,24); }
c = parent.getComponent(14);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+168,184,24); }
c = parent.getComponent(15);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+200,184,24); }
c = parent.getComponent(16);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+232,184,24); }
c = parent.getComponent(17);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+264,184,24); }
c = parent.getComponent(18);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+296,184,24); }
c = parent.getComponent(19);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+328,184,24); }
c = parent.getComponent(20);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+360,184,24); }
c = parent.getComponent(21);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+392,184,24); }
c = parent.getComponent(22);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+424,184,24); }
c = parent.getComponent(23);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+456,184,24); }
c = parent.getComponent(24);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+488,184,24); }
c = parent.getComponent(25);
if (c.isVisible()) {c.setBounds(insets.left+416,insets.top+56,216,24) ;}
c = parent.getComponent(26);
if (c.isVisible()) {c.setBounds(insets.left+440,insets.top+136,72,24) ;}
c = parent.getComponent(27);
if (c.isVisible()) {c.setBounds(insets.left+440,insets.top+168,72,24) ;}
c = parent.getComponent(28);
if (c.isVisible()) {c.setBounds(insets.left+440,insets.top+200,72,24) ;}
c = parent.getComponent(29);
if (c.isVisible()) {c.setBounds(insets.left+440,insets.top+232,72,24) ;}
c = parent.getComponent(30);
if (c.isVisible()) {c.setBounds(insets.left+440,insets.top+264,72,24) ;}
c = parent.getComponent(31);
if (c.isVisible()) {c.setBounds(insets.left+440,insets.top+296,72,24) ;}
c = parent.getComponent(32);
if (c.isVisible()) {c.setBounds(insets.left+440,insets.top+328,72,24) ;}
c = parent.getComponent(33);
if (c.isVisible()) {c.setBounds(insets.left+440,insets.top+360,72,24) ;}
c = parent.getComponent(34);
if (c.isVisible()) {c.setBounds(insets.left+440,insets.top+392,72,24) ;}
c = parent.getComponent(35);
if (c.isVisible()) {c.setBounds(insets.left+440,insets.top+424,72,24) ;}
c = parent.getComponent(36);
if (c.isVisible()) {c.setBounds(insets.left+440,insets.top+456,72,24) ;}
c = parent.getComponent(37);
if (c.isVisible()) {c.setBounds(insets.left+440,insets.top+488,72,24) ;}
c = parent.getComponent(38);
if (c.isVisible()) {c.setBounds(insets.left+416,insets.top+80,216,24) ;}
c = parent.getComponent(39);
if (c.isVisible()) {c.setBounds(insets.left+224,insets.top+80,72,24); }
c = parent.getComponent(40);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+56,184,24);}
c = parent.getComponent(41);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+80,184,24);}
}
}

Hell-Fire
03-28-2006, 07:45 AM
Well, let me first say that the bulk of my Java knowledge is with GUIs (ect) - not applets. The two are very similiar, but there are subtle differences.

To do something with the items selected in your "Choice" boxes, you can use one of two method calls: getSelectedItem() or getSelectedIndex()

getSelectedItem() - returns a String representation of the selected choice
getSelectedIndex() - returns the index value (int) of the selected choice

The method that returns the String is great for values where you want a string returned for reasons I leave to you. The getSelecetedIndex() method is great to use in instances where you want to manipulate numbers.

For every choice box, you have to also add the addItemListener() method. This will let the applet react to the users choices.

Example: choice_1.addItemListener(this);

Now, once a choice is made you simply update the text fields with new text based on whatever calculations you have to do. Good practice would be to update your text fields with the choices you make in the boxes. This lets you know that you have detected changes in the choices.

The are various ways to control the number of choice boxes visible to the user, but the simplest way for you I believe would be to create the max number you would ever need. Set them all the visible(false) until the initial choice is made. Then set the appropriate number to visible(true) once the user makes the initial choice. This is all done using what I said above with the getSelectedItem() method.

Please take what I am about to say with a grain of salt. You really need to rename all those fields. I have no idea what I am looking at until running the applet to see what goes where. If a text field is for age, then name it textAge...ect ect ect. The same for choice boxes. If the box is to determine how many more additional choice boxes are needed, name it choiceDecision or something along those lines.

Shelnutt2
03-28-2006, 11:46 AM
Well, let me first say that the bulk of my Java knowledge is with GUIs (ect) - not applets. The two are very similiar, but there are subtle differences.

To do something with the items selected in your "Choice" boxes, you can use one of two method calls: getSelectedItem() or getSelectedIndex()

getSelectedItem() - returns a String representation of the selected choice
getSelectedIndex() - returns the index value (int) of the selected choice

The method that returns the String is great for values where you want a string returned for reasons I leave to you. The getSelecetedIndex() method is great to use in instances where you want to manipulate numbers.

For every choice box, you have to also add the addItemListener() method. This will let the applet react to the users choices.

Example: choice_1.addItemListener(this);

Now, once a choice is made you simply update the text fields with new text based on whatever calculations you have to do. Good practice would be to update your text fields with the choices you make in the boxes. This lets you know that you have detected changes in the choices.

The are various ways to control the number of choice boxes visible to the user, but the simplest way for you I believe would be to create the max number you would ever need. Set them all the visible(false) until the initial choice is made. Then set the appropriate number to visible(true) once the user makes the initial choice. This is all done using what I said above with the getSelectedItem() method.

Please take what I am about to say with a grain of salt. You really need to rename all those fields. I have no idea what I am looking at until running the applet to see what goes where. If a text field is for age, then name it textAge...ect ect ect. The same for choice boxes. If the box is to determine how many more additional choice boxes are needed, name it choiceDecision or something along those lines.

Alighty, Thank you very much, I'm going to work with my code somemore now.

I should rename then besides the defult name. When I reworked the applet this morning I forgot to add in my comments to them.

edit:
Time for me to go check out that "Java for Dummies" book again.