Results 1 to 4 of 4
  1. #1
    skuzzie is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default Jcombo/multi class help

    Im making an applet in which you have a combo box, you then choose one of the options and the information is then displayed in the text areas.

    This is what ive done so far, I know I have a few things messed up, but my book cant describe my problem and im lost. This is my first time using more then one class, which is where my problem is.

    Errors:
    Java Code:
    shipwreck.java:173: incompatible types
    found   : int
    required: java.lang.String
    		numberVictims = Integer.parseInt(victimsTf.getText());
    		                                ^
    shipwreck.java:205: cannot find symbol
    symbol  : variable yearOfWreck
    location: class HelperClass
    			yearOfWreck = yearArray[indexOfShip];
    			^
    shipwreck.java:211: cannot find symbol
    symbol  : variable victims
    location: class HelperClass
    			victims = victimsArray;
    			^
    shipwreck.java:212: cannot find symbol
    symbol  : variable victims
    location: class HelperClass
    			return victims;
    			       ^
    4 errors
    Java Code:
    /*	document segment
    	filename:	shipwreck
    	author:		walker
    	date:		November.2010
    */
    
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class shipwreck extends JApplet implements ActionListener
    {
    
    	/* ----------------------------- declarations */
    
    	// color objects
    	Color black = new Color(0, 0, 0);
    	Color white = new Color(255, 255, 255);
    
    	// components
    	JLabel famous;
    	JComboBox shipCombo;
    
    	JLabel nameOfShip;
    	JTextField nameOfShipTf;
    
    	JLabel yearOfWreck;
    	JTextField yearOfWreckTf;
    	
    	JLabel victims;
    	JTextField victimsTf;
    
    	JButton enter;
    	JButton clear;
    
    
    	// variables
    	String[] shipArray = {"Titanic", "E Fitzgerald", "Lusitania"};
    
    
    	String nameOfShipp;
    	String yearOfWreckk;
    	String numberVictims;
    	
    	int shipIndex = 0; /* INDEX FOR JCOMBOBOX */
    
    	// objects
    	HelperClass shipLog; /* DECLARE HELPER CLASS */
    
    
    	public void init()
    	{
    		setLayout(null);
    		setSize(400, 400);
    
    		/* ------------------- initialization */
    		famous = new JLabel();
    		famous.setBounds(50, 50, 100, 20);
    		famous.setFont(new Font("Default", Font.PLAIN, 12));
    		famous.setText("Select a Fruit");
    		famous.setForeground(black);
    		famous.setHorizontalAlignment(JLabel.LEFT);
    		add(famous);
    
    		shipCombo = new JComboBox(shipArray);
    		shipCombo.setBounds(150, 50, 100, 20);
    		shipCombo.setForeground(black);
    		shipCombo.setBackground(white);
    		shipCombo.setMaximumRowCount(3);
    		add(shipCombo);
    
    		nameOfShip = new JLabel();
    		nameOfShip.setBounds(50, 110, 100, 20);
    		nameOfShip.setFont(new Font("Default", Font.PLAIN, 12));
    		nameOfShip.setText("Selected Fruit");
    		nameOfShip.setForeground(black);
    		nameOfShip.setHorizontalAlignment(JLabel.LEFT);
    		add(nameOfShip);
    
    		nameOfShipTf = new JTextField();
    		nameOfShipTf.setBounds(150, 110, 100, 20);
    		nameOfShipTf.setFont(new Font("Default", Font.PLAIN, 12));
    		nameOfShipTf.setHorizontalAlignment(JTextField.CENTER);
    		nameOfShipTf.setForeground(black);
    		nameOfShipTf.setBackground(white);
    		nameOfShipTf.setEditable(false);
    		add(nameOfShipTf);
    
    		yearOfWreck = new JLabel();
    		yearOfWreck.setBounds(50, 140, 100, 20);
    		yearOfWreck.setFont(new Font("Default", Font.PLAIN, 12));
    		yearOfWreck.setText("Fruit Price");
    		yearOfWreck.setForeground(black);
    		yearOfWreck.setHorizontalAlignment(JLabel.LEFT);
    		add(yearOfWreck);
    
    		yearOfWreckTf = new JTextField();
    		yearOfWreckTf.setBounds(150, 140, 100, 20);
    		yearOfWreckTf.setFont(new Font("Default", Font.PLAIN, 12));
    		yearOfWreckTf.setHorizontalAlignment(JTextField.CENTER);
    		yearOfWreckTf.setForeground(black);
    		yearOfWreckTf.setBackground(white);
    		yearOfWreckTf.setEditable(false);
    		add(yearOfWreckTf);
    		
    		victims = new JLabel();
    		victims.setBounds(50, 160, 100, 20);
    		victims.setFont(new Font("Default", Font.PLAIN, 12));
    		victims.setText("Number of victims");
    		victims.setForeground(black);
    		victims.setHorizontalAlignment(JLabel.LEFT);
    		add(victims);
    		
    		victimsTf = new JTextField();
    		victimsTf.setBounds(150, 140, 100, 20);
    		victimsTf.setFont(new Font("Default", Font.PLAIN, 12));
    		victimsTf.setHorizontalAlignment(JTextField.CENTER);
    		victimsTf.setForeground(black);
    		victimsTf.setBackground(white);
    		victimsTf.setEditable(false);
    		add(victimsTf);
    
    		enter = new JButton();
    		enter.setBounds(100, 300, 80, 20);
    		enter.setFont(new Font("Default", Font.PLAIN, 12));
    		enter.setText("Enter");
    		enter.setForeground(black);
    		enter.setBackground(white);
    		add(enter);
    		enter.addActionListener(this);
    
    		clear = new JButton();
    		clear.setBounds(200, 300, 80, 20);
    		clear.setFont(new Font("Default", Font.PLAIN, 12));
    		clear.setText("Clear");
    		clear.setForeground(black);
    		clear.setBackground(white);
    		add(clear);
    		clear.addActionListener(this);
    
    	}
    
    	public void actionPerformed(ActionEvent event)
    	{
    		Object obj = event.getSource();
    		if(obj == enter)
    		{
    			getSelectedShip();
    		}
    		else if(obj == clear)
    		{
    			clearAll();
    		}
    	}
    
    	public void getSelectedShip()
    	{
    		shipIndex = shipCombo.getSelectedIndex();		/* GET THE INDEX OF SELECTED ITEM */
    		nameOfShipp = shipArray[shipIndex];				/* GET THE NAME OF SELECTED ITEM */
    		nameOfShipTf.setText("" + nameOfShip);
    		shipLog = new HelperClass(shipIndex);			/* SEND VALUE OF INDEX TO HELPER CLASS */
    		getYear();
    	}
    
    	public void getYear()
    	{
    		yearOfWreckk = shipLog.getYear();				/* CALL TO HELPER CLASS FOR PRICE */
    		yearOfWreckTf.setText("" + yearOfWreck);
    		getVictim();
    	}
    	public void getVictim()
    	{
    		numberVictims = Integer.parseInt(victimsTf.getText());
    		victimsTf.setText("" + victims);
    	
    	}
    
    	public void clearAll()
    	{
    		shipCombo.setSelectedIndex(0);					/* SETS JCOMBOBOX BACK TO FIRST ITEM */
    		nameOfShipTf.setText("");
    		yearOfWreck.setText("");
    		victimsTf.setText("");
    	}
    } /* END OF INTERFACE CLASS */
    
    /* WHITE SPACE */
    
    class HelperClass /* HELPER CLASS */
    {
    	
    	String[] yearArray = {"1912", "1975", "1915"}; 
    	int indexOfShip;
    
    	int[] victimsArray = {1503, 29, 1198}; 
    
    	public HelperClass(int indexValue)
    	{
    			indexOfShip = indexValue;
    			getYear();
    	}
    	
    	public String getYear()
    	{
    			yearOfWreck = yearArray[indexOfShip];
    			getVictims();
    	}
    	
    	public int getVictims()
    	{
    			victims = victimsArray;
    			return victims;
    	}
    			
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    The errors are telling you just what is wrong. I'll bet if you read them carefully and then review your code, you'll figure it out. If not, we can explain any areas of confusion -- just ask.
    Last edited by Fubarable; 11-21-2010 at 11:30 PM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I have some other issues with your code. For instance this method here:

    Java Code:
       public void getVictim() {
          numberVictims = Integer.parseInt(victimsTf.getText());
          victimsTf.setText("" + victims);
    
       }
    The compiler is correctly complaining that numberVictims is a String variable and you're passing it an int value, but even more important, this appears to be a "getter" method", a method that is supposed to get and return a value from the class, but it returns void or nothing. Even more than that, if you fix your compiler errors, what does this method actually do? It appears to extract text from a JTextField, and then place the same text back in the same JTextField. But what are you trying to achieve by this?

    Edit 1: Also, what is the following method trying to achieve? It's name suggests that it is going to return a year as a String, but it returns nothing and calls getVictims(). Again, what's your rationale behind the code?
    Java Code:
       public String getYear() {
          yearOfWreck = yearArray[indexOfShip];
          getVictims();
       }
    Last edited by Fubarable; 11-21-2010 at 11:31 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Also, what does "fruit" have to do with this application? :)

    Java Code:
          famous = new JLabel();
          famous.setBounds(50, 50, 100, 20);
          famous.setFont(new Font("Default", Font.PLAIN, 12));
          famous.setText([color="red"][b]"Select a Fruit"[/b][/color][b][/b]);
          famous.setForeground(black);
          famous.setHorizontalAlignment(JLabel.LEFT);
          add(famous);
    
          shipCombo = new JComboBox(shipArray);
          shipCombo.setBounds(150, 50, 100, 20);
          shipCombo.setForeground(black);
          shipCombo.setBackground(white);
          shipCombo.setMaximumRowCount(3);
          add(shipCombo);
    
          nameOfShip = new JLabel();
          nameOfShip.setBounds(50, 110, 100, 20);
          nameOfShip.setFont(new Font("Default", Font.PLAIN, 12));
          nameOfShip.setText([color="red"][b]"Selected Fruit"[/b][/color][b][/b]);
          nameOfShip.setForeground(black);
          nameOfShip.setHorizontalAlignment(JLabel.LEFT);
          add(nameOfShip);
    (sorry, couldn't help myself on this one :))

Similar Threads

  1. AutoSuggest JCombo box
    By vsmilejay in forum AWT / Swing
    Replies: 2
    Last Post: 09-03-2010, 06:34 AM
  2. jcombo in jtable
    By anilkumar_vist in forum Advanced Java
    Replies: 0
    Last Post: 12-12-2009, 06:47 AM
  3. Sort jcombo in jtable
    By anilkumar_vist in forum Advanced Java
    Replies: 1
    Last Post: 12-11-2009, 05:23 PM
  4. Display items in the JCombo Box
    By eddy001 in forum AWT / Swing
    Replies: 1
    Last Post: 01-21-2008, 12:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •