9 Replies - 1148 Views - Last Post: 20 February 2012 - 07:07 AM Rate Topic: -----

#1 Funked  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-January 12

Applet: TextField, ActionListener, etc

Posted 19 February 2012 - 04:03 PM

Hey guys. I have an assignment for university which is to create a Java Applet - a map of a park/road system with objects on it. A task we have to do is add a text box at the top, and when we type in a word such as "Restaurant" and click the button beside it, it displays information about the restaurant on the map. I've got the graphics done, and also added some event listeners. I have it working so when you click on the map, it draws a little black oval. Next step is to click on an object and it displays information, but first I want to get the search box working. I have spent hours on google trying various methods, but just can't get it working!! Currently I have it set up so it paints the text_output at 100,100 but this is wrong. I need to somehow store a string of information (restaurant opening times, etc), which can be painted when I type in a certain word and press the button. Please take a look at my code and guide me to my next step! At the minute it gives me a errors, and the applet is blank until I click on the text box, then the map appears as normal. Thanks in advance for any help :)

Note: I removed the code with all my graphics, as it's about 300 lines of code.


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

public class Assignment extends Applet implements MouseListener, ActionListener
{
	String text_output;
	TextField text_input;
	Button btn_click;
	
	// mouse click listeners
	int x, y;
	public void start()
	{
		addMouseListener(this);
	}
	
	public void mouseClicked(MouseEvent e)
	{
		showStatus("Clicked at "+ e.getX() + " " + e.getY());
		x = e.getX() - 10;
		y = e.getY() - 10;
		repaint();
	}
	
    public void mousePressed(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}

    public void mouseReleased(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
	
	public void init()
	{
		resize(800, 800);
		
		text_input = new TextField(20);
		add(text_input);
		text_input.addActionListener(this);
		
		btn_click = new Button("Information");
		add(btn_click);
		btn_click.addActionListener(this);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		// actions preformed by text/button
		text_output = text_input.getText();
	}
	

	
	// graphics
	
	public void paint(Graphics g)
	// pre: g is a valid graphics object
	// post: draws to Applet window
	{
		g.drawString(text_output, 100, 100);

		// mouse click oval
		g.setColor(Color.black);
		g.fillOval(x, y, 10, 10);
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Applet: TextField, ActionListener, etc

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,653
  • Joined: 20-September 08

Re: Applet: TextField, ActionListener, etc

Posted 19 February 2012 - 04:22 PM

It would probably help if you posted the assignment spec
Was This Post Helpful? 0
  • +
  • -

#3 Funked  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-January 12

Re: Applet: TextField, ActionListener, etc

Posted 19 February 2012 - 04:29 PM

Sorry I should have posted that. I can't seem to find a button to edit my original post (I'm probably blind), so here is the question in relation to this task:

Search-term mechanism
The Applet should contain a TextField labeled ‘Search Term’. The Applet displays information (if any is found) matching to the search term. Minimally the search mechanism must be able to match to at least the terms defined in your key; e.g. in the campsite example the key mentions ‘picnic area’, ‘restaurant’ and ‘lake’, so the code must recognise those terms and display information about such areas, e.g., ‘All picnic areas have a children’s playground’ could be displayed in the Applet. The information should be displayed near to the text entry box (because that is where the attention of the user is focused). If there is no match then a message: ‘No match: ’+ search-term is displayed.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,653
  • Joined: 20-September 08

Re: Applet: TextField, ActionListener, etc

Posted 19 February 2012 - 04:36 PM

Since there's no mention of using paint to present the information, i would advise you to avoid that. It's tricky and you don't want to be calculating string metrics, leading etc. Use a text area of some sort. You can hold your information in Map<String, String> where the key points to the full information, the key being the 'keyword'.
There's nothing wrong with overriding paint in order to show your graphical map, which would normally be an image. Be sure to call super.paint so that any components, such as your text area, are displayed properly.

This post has been edited by g00se: 19 February 2012 - 04:38 PM

Was This Post Helpful? 0
  • +
  • -

#5 Funked  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-January 12

Re: Applet: TextField, ActionListener, etc

Posted 19 February 2012 - 04:44 PM

Ok thanks I'll avoid using paint to display info. I'm really struggling with how to store the information, I understand I can store it in a string but I don't know how I can retrieve that information after typing a keyword. I've searched online but can't seem to find much info, do you know of any websites that have examples of something similar to what I need? I done a search here on d.i.c. and can't seem to find anything :( Thanks for your help!
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,653
  • Joined: 20-September 08

Re: Applet: TextField, ActionListener, etc

Posted 19 February 2012 - 06:27 PM

public class Info {
    private String keyword;
    private String description;

    public String getKeyword() {
        return this.keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String toString() {
        return keyword;
    }
}



Use the above. Fill a DefaultComboBoxModel with instances of it and use it as the model for a JComboBox. Make your applet a JApplet instead of Applet
Was This Post Helpful? 1
  • +
  • -

#7 Funked  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-January 12

Re: Applet: TextField, ActionListener, etc

Posted 20 February 2012 - 06:28 AM

Thanks for posting that code. The only thing is, we aren't allowed to make a JApplet, it has to be an Applet. I don't think we can use the java.swing package either. Would I be able to implement the above code into my Applet the way it is? Or is there some things I'd need to change? Thanks for your help!
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,653
  • Joined: 20-September 08

Re: Applet: TextField, ActionListener, etc

Posted 20 February 2012 - 06:36 AM

You can still use the same approach, but it will have to be done in a cruder way: use a Choice component with Map<String, String>. What you add to the Choice will be keys into the Map of key to description
Was This Post Helpful? 0
  • +
  • -

#9 Funked  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-January 12

Re: Applet: TextField, ActionListener, etc

Posted 20 February 2012 - 06:57 AM

Ah ok I see. I think I understand, this is all still fairly new to me. Would I be able to use a case/break method? So I could add like 6 cases for 6 things on the map, then when the user enters one of those, it would jump from the case down to a method containing the string for that keyword. Know what I mean? I'm pretty bad at explaining things, sorry.
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,653
  • Joined: 20-September 08

Re: Applet: TextField, ActionListener, etc

Posted 20 February 2012 - 07:07 AM

You just need a listener on the Choice. When it changes you can do something like

textArea.setText(map.get(choice.getSelectedItem()));

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1