Results 1 to 2 of 2
  1. #1
    danielD is offline Member
    Join Date
    May 2012
    Location
    new york
    Posts
    1
    Rep Power
    0

    Default ListListener problems

    Hi all, Working on final project and frustrated with the lack of details my book has for listlisteners. It shows 1 example and it uses audio files.(no help to me)
    Id like to have this working for turn in tomorrow but that depends on whether i can find some straightforward info on setup. I did find a few API's but honestly
    im confused as to how to use the methods. ANY help, hints, tutorial would be greatly appreciated. Thanks
    my code:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Space
    {
       
       public static void main (String[] args)
       {
          JFrame frame = new JFrame ("Space Trip");
          frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    		
    		//panel and label setup for splitpane
    		JPanel SpacePanel = new JPanel();
    		JPanel imagePanel = new JPanel();
    		ImageIcon icon = new ImageIcon("take-off.gif");
    		JLabel imageLabel = new JLabel(icon);
    		SpacePanel imageList = new SpacePanel(imageLabel);
    		imagePanel.add (imageLabel);
    		imagePanel.setBackground (Color.black);
    		
    		
    		//splitpane with 2 panels and expandable set to true
    		JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, imageList, imagePanel);
    		split.setOneTouchExpandable(true);
    
          frame.getContentPane().add(split);
          frame.pack();
          frame.setVisible(true);
       }
    }
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.event.*;
    
    public class SpacePanel extends JPanel
    {
       protected JList list, world;
    	private JComboBox Destinationbox;
    	protected JLabel label, destination;
    	protected String mass, atmosphere, temp, gravity, moons, found, image, info, distance; 
    	
       public SpacePanel (JLabel imageLabel)
     	{
    		label = imageLabel;
    		setPreferredSize (new Dimension (300, 0));
    		setBackground (Color.white);
    	
    		// array list of left pane inforamtion
    		String[] fileNames = { "Distance from Earth: " + distance,
    										"\n\n",
                                  "Mass: " + mass,
    										"\n\n",
                                  "Atmosphere: " + atmosphere,
    										"\n\n",
                                  "Surface temerature: " + temp,
    										"\n\n",
                                  "Surface pressure: " + gravity,
    										"\n\n",
                                  "Number of orbiting moons: " + moons,
    										"\n\n",
                                  "Discovered: " + found,
    										"\n\n",
    										" Info: " + info, "\n\n"};
    										
    		//array of planet names for dropdown
    		String[] planets = {"", "Mercury", "Venus", "Earth", "Mars", "Jupiter",
    								  "Saturn", "Uranus", "Neptune", "Pluto", "Quit"};
    		
    		//lists forinfo display and listener index						  
    		list = new JList (fileNames);
    		add (list);
    		world = new JList (planets);
    		
    		//JPanel and JLabel for dropdown
    		JPanel boxPanel = new JPanel();
    		boxPanel.setPreferredSize (new Dimension (150, 60));						  
          destination = new JLabel("Choose a planet to view");
    		boxPanel.add(destination);
    		Destinationbox = new JComboBox (planets);
    		add (boxPanel);
    		
    		//listener setup for world list
    		world.addListSelectionListener (new ListListener());
          world.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
    	   boxPanel.add(Destinationbox);
    		
       }
    	
       //Represents the listener for the informationlist.
       private class ListListener implements ListSelectionListener
       {
          public void valueChanged (ListSelectionEvent event)
          {
             if (world.isSelectionEmpty())
                label.setIcon (null);
             else
             {
                String planets = (String)world.getSelectedValue();
                
        		}	
       	}
    
    	}
    }
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class Planets
    {
    	String mercury, venus, earth, mars, jupiter, saturn, uranus, neptune, pluto;
    
    	private JLabel image;
    	private ImageIcon icon;
    	
    	//override class for each planet
    	Planets Mercury = new Planets();
    	Planets Venus = new Planets();
    	Planets Earth = new Planets();
    	Planets Mars = new Planets();
    	Planets Jupiter = new Planets();
    	Planets Saturn = new Planets();
    	Planets Uranus = new Planets();
    	Planets Neptune = new Planets();
    	Planets Pluto = new Planets();
    	
    	
    	//
    	public String Mercury(String distance,String mass, String atmosphere, String temp, String gravity,
    									String moons, String found, String info)
    	{
    		mercury += Mercury;
    		distance = "From 77 million km to 222 million km";
    		mass = "4877.921 km in diameter and 5.5% of earth";
    		atmosphere = "95% Helium, 5% Hydrogen";
    		temp = "Ranges from 950deg F on day side,to -346deg F on night side";
    		gravity = " 1/20th that of Earth.100lbs = 38lbs";
    		moons = "No moons";
    		found = "@ 3rd millennium BC.Named after the Roman god of commerce and thievery";
    		info = "By core volume, it is the densest rocky planet. Its iron core is 2 to 3 times the size of Earth's";
    		icon = new ImageIcon("mercury.jpg");
    		image.setIcon(icon);
    		return mercury;
    	    
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: ListListener problems

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. SQL problems
    By Johanis in forum New To Java
    Replies: 23
    Last Post: 10-30-2011, 06:19 PM
  2. InputStream/Jar Problems/File IO Problems
    By rdjava in forum Advanced Java
    Replies: 31
    Last Post: 01-17-2011, 11:12 AM
  3. JAR problems
    By Evil Smurf in forum New To Java
    Replies: 4
    Last Post: 09-01-2009, 01:17 AM
  4. many to many problems
    By cecily in forum JDBC
    Replies: 1
    Last Post: 08-02-2007, 05:51 PM
  5. a few problems
    By gary in forum AWT / Swing
    Replies: 0
    Last Post: 07-11-2007, 04:57 PM

Posting Permissions

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