The Artima Developer Community
Sponsored Link

Java Answers Forum
Please Help!

1 reply on 1 page. Most recent reply: May 12, 2002 10:29 AM by Evgen

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Khalid

Posts: 1
Nickname: khalid
Registered: May, 2002

Please Help! Posted: May 12, 2002 7:57 AM
Reply to this message Reply
Advertisement
Dear Helper

Ok so I am struggling to get this done. I know it is very simple but can't find my way around it. If you can help I will be more than thankful.

I have isolated the main problem from the whole Applet so I am presenting just that part of the Applet which is giving me problems:

Objective: To have two drop down boxes and a button to get their values and display them, so i can pass the variables to another class for complex calculations - simple I am just concerned about getting two values from drop down boxes here is the code:

//IMPORT

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


public class itemtest extends Applet implements ActionListener,ItemListener
{

//<----DECLARATIONS-------------->

Choice choice1;
int khalid1;
String value1, value2;
BookingDialog d;
Calculation v;

//<---------INIT METHOD-------------->

public void init(){

setSize(500,375);
setBackground(Color.lightGray);
setForeground(Color.black);

//Create a two Choice Menus

Choice choice1 = new Choice();
choice1.setForeground(Color.white);
choice1.setBackground(Color.darkGray);
choice1.addItem("Choose Show");
choice1.addItem("Show1");
choice1.add("Show2");
choice1.add ("Show3");
this.add(choice1);

Choice choice2 = new Choice();
choice2.setForeground(Color.white);
choice2.setBackground(Color.darkGray);
choice2.addItem("10");
choice2.addItem("20");
choice2.add("30");
choice2.add ("40");
this.add(choice2);

//Button

Button b1=new Button("Book Now");
b1.setFont(new Font("SansSerif", 1, 12));
b1.setForeground(Color.black);
add(b1);
b1.addActionListener(this);


}//<----------END INIT METHOD------------------>


/*BELOW ARE ALL THE METHODS I HAVE BEEN TRYING THIS IS WHERE I NEED HELP
TO GET THE VALUE OF BOTH THE DROP DOWN BOXES AND ADD THEM. It compiles
gracefully but at execution I get the NULL POINTER EXCEPTION */

<------------------------------------------------------------------->

public void itemStateChanged(ItemEvent e){

// String sendvalue=(String) e.getItem();
// khalid=Integer.parseInt(sendvalue);
// repaint();
}

public String getchoice1()
{
String value1 = choice1.getItem(choice1.getSelectedIndex());
String value2 = choice2.getItem(choice2.getSelectedIndex());
return value1,value2;
}


public void actionPerformed(ActionEvent e){
if(e.getActionCommand() == "Book Now")
{

getchoice1();

//String sendvalue=choice1.getItem(choice1.getSelectedIndex());
//int sendvalue=choiceTickets.getSelectedIndex();
//d=new BookingDialog(new Frame());
//v=new Calculation(new Frame(),value1,value2);
}
}
public void paint(Graphics g){
g.drawString("The sendvalue is "+value1+value2,50,100);
}
}


Evgen

Posts: 14
Nickname: evgen79
Registered: May, 2002

Re: Please Help! Posted: May 12, 2002 10:29 AM
Reply to this message Reply
You can return 2 string if declared return type one string.

////////////////////////////////////////////
import java.applet.*; 
import java.awt.*; 
import java.awt.event.*; 
 
public class itemtest extends Applet implements ActionListener,ItemListener 
{ 
//<----DECLARATIONS--------------> 
 
Choice choice1, choice2; 
Label l1, l2;
int khalid1; 
String value1, value2; 
 
//<---------INIT METHOD--------------> 
public void init(){ 
setSize(500,375); 
setBackground(Color.lightGray); 
setForeground(Color.black); 
 
//Create a two Choice Menus 
choice1 = new Choice(); 
choice1.setForeground(Color.white); 
choice1.setBackground(Color.darkGray); 
choice1.addItem("Choose Show"); 
choice1.addItem("Show1"); 
choice1.add("Show2"); 
choice1.add ("Show3"); 
this.add(choice1);
 
choice2 = new Choice(); 
choice2.setForeground(Color.white); 
choice2.setBackground(Color.darkGray); 
choice2.addItem("10"); 
choice2.addItem("20"); 
choice2.add("30"); 
choice2.add ("40"); 
this.add(choice2); 
 
//Button 
Button b1=new Button("Book Now"); 
b1.setFont(new Font("SansSerif", 1, 12)); 
b1.setForeground(Color.black); 
add(b1); 
b1.addActionListener(this); 
 
//Labels of first and second values
l1 = new Label("                 ");
l2 = new Label("                 ");
add(l1); add(l2);
 
}//<----------END INIT METHOD------------------> 
 
 
//<-------------------------------------------------------------------> 
public void itemStateChanged(ItemEvent e){ 
} 
 
public String[] getchoice1() 
{ 
String[] str;
String value1 = choice1.getItem(choice1.getSelectedIndex()); 
String value2 = choice2.getItem(choice2.getSelectedIndex()); 
str = new String[2];
str[0] = value1;
str[1] = value2;
return str;
} 
 
 
public void actionPerformed(ActionEvent e){ 
if(e.getActionCommand() == "Book Now") 
{ 
String[] StrRes;
StrRes = getchoice1(); 
value1 = StrRes[0];
value2 = StrRes[1];
l1.setText(value1);
l2.setText(value2);
} 
} 
public void paint(Graphics g){ 
g.drawString("The sendvalue is "+value1+value2,50,100); 
} 
} 

Flat View: This topic has 1 reply on 1 page
Topic: Please help me with a Java Radix Sorter Previous Topic   Next Topic Topic: java.security.AccessControlException on Macintosh

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use