-
Make a program notify
Hi. Basically, I want my program, if minimized or not the like...active window, to pop-up (I'm pretty sure that is not the technical term, but I don't know what it is). Whenever a new word/phrase changes or it tells you the answer I want it to notify the user somehow. Either by popping itself up to show you, or blink on the bar or something. Is there I can do this without drastically changing the program? Here it is --
Code:
import java.util.ArrayList;
import java.util.Random;
public class Library {
public ArrayList<String> spanish = new ArrayList<String>(); //make a spanish list
public ArrayList<String> english = new ArrayList<String>(); //make an english list
static Random random = new Random(); //make a random object
private int index;
public void setIndex(int index) {
this.index = index;
}
public int getIndex() {
return index;
}
//method to add all Strings to the lists
public void makeLists() {
spanish.add("Cómo se llama?"); english.add("What is his/her name?");
spanish.add("Cómo se llama usted?"); english.add("What is your name?");
spanish.add("Por favor"); english.add("Please");
spanish.add("De dónde eres?"); english.add("Where are you from?");
spanish.add("De dónde es usted?"); english.add("Where are you from?(form)");
spanish.add("Hasta la vista"); english.add("See you later");
spanish.add("Hasta pronto"); english.add("See you soon");
spanish.add("Nos vemos"); english.add("See you");
spanish.add("Cómo estás?"); english.add("How are you?");
spanish.add("Cómo está usted?"); english.add("How are you?(form)");
spanish.add("Qué hay de nuevo?"); english.add("What's new?");
spanish.add("Qué tal?"); english.add("How are you?");
spanish.add("Con permiso"); english.add("Pardon me");
spanish.add("Lo siento"); english.add("I'm sorry");
spanish.add("No hay de qué"); english.add("You're welcome");
spanish.add("De nada"); english.add("You're welcome");
spanish.add("Mucho gusto"); english.add("Pleased to meet you");
spanish.add("Le presento a..."); english.add("I would like to introduce...");
spanish.add("Te presento a..."); english.add("I would like to introduce...(form)");
spanish.add("Soy de..."); english.add("I'm from...");
spanish.add("Cuántos?"); english.add("How many?");
spanish.add("el autobús"); english.add("bus");
spanish.add("la capital"); english.add("capital city");
spanish.add("el chico"); english.add("boy");
spanish.add("la chica"); english.add("girl");
spanish.add("la computadora"); english.add("computer");
spanish.add("la comunidad"); english.add("community");
spanish.add("el/la conductor(a)"); english.add("driver");
spanish.add("la conversación"); english.add("conversation");
spanish.add("la cosa"); english.add("thing");
spanish.add("el cuaderno"); english.add("notebook");
spanish.add("el día"); english.add("day");
spanish.add("el diario"); english.add("diary");
spanish.add("el diccionario"); english.add("dictionary");
spanish.add("la escuela"); english.add("school");
spanish.add("la grafia"); english.add("photograph");
spanish.add("la grabadora"); english.add("tape recorder");
spanish.add("el lápiz"); english.add("pencil");
spanish.add("la maleta"); english.add("suitcase");
spanish.add("la mano"); english.add("hand");
spanish.add("la mujer"); english.add("woman");
spanish.add("el país"); english.add("country");
spanish.add("la palabra"); english.add("word");
spanish.add("el/la pasajero/a"); english.add("passenger");
spanish.add("bailar"); english.add("to dance");
spanish.add("buscar"); english.add("to look for");
spanish.add("caminar"); english.add("to walk");
spanish.add("cantar"); english.add("to sing");
spanish.add("cenar"); english.add("to have dinner");
spanish.add("comprar"); english.add("to buy");
spanish.add("contestar"); english.add("to answer");
spanish.add("conversar"); english.add("to converse");
spanish.add("desayunar"); english.add("to have breakfast");
spanish.add("descansar"); english.add("to rest");
spanish.add("desear"); english.add("to wish");
spanish.add("dibujar"); english.add("to draw");
spanish.add("enseñar"); english.add("to teach");
spanish.add("escuchar"); english.add("to listen");
spanish.add("esperar"); english.add("to wait for");
spanish.add("estar"); english.add("to be");
spanish.add("estudiar"); english.add("to study");
spanish.add("explicar"); english.add("to explain");
spanish.add("gustar"); english.add("to like");
spanish.add("hablar"); english.add("to speak");
spanish.add("llegar"); english.add("to arrive");
spanish.add("llevar"); english.add("to carry");
spanish.add("mirar"); english.add("to look at");
spanish.add("necesitar"); english.add("to need");
spanish.add("practicar"); english.add("to practice");
spanish.add("preguntar"); english.add("to ask");
spanish.add("preparar"); english.add("to prepare");
spanish.add("regresar"); english.add("to return");
spanish.add("terminar"); english.add("to end");
spanish.add("tomar"); english.add("to take");
spanish.add("trabajar"); english.add("to work");
spanish.add("viajar"); english.add("to travel");
spanish.add("el/la compañero/a de clase"); english.add("classmate");
spanish.add("el/la compañero/a de cuarto"); english.add("roommate");
spanish.add("el borrador"); english.add("eraser");
spanish.add("el escritorio"); english.add("desk");
spanish.add("el libro"); english.add("book");
spanish.add("la mesa"); english.add("table");
spanish.add("la mochila"); english.add("bookbag");
spanish.add("el papel"); english.add("paper");
spanish.add("la papelera"); english.add("wastebasket");
spanish.add("la pizarra"); english.add("blackboard");
spanish.add("la pluma"); english.add("pen");
spanish.add("la puerta"); english.add("door");
spanish.add("el reloj"); english.add("watch");
spanish.add("la silla"); english.add("seat");
spanish.add("la tiza"); english.add("chalk");
spanish.add("la ventana"); english.add("window");
spanish.add("la biblioteca"); english.add("library");
spanish.add("la cafetería"); english.add("cafeteria");
spanish.add("la casa"); english.add("house");
spanish.add("el estadio"); english.add("stadium");
spanish.add("la librería"); english.add("bookstore");
spanish.add("la residencia estudiantil"); english.add("dormitory");
spanish.add("la universidad"); english.add("university");
spanish.add("la clase"); english.add("class");
spanish.add("el curso, la materia"); english.add("course");
spanish.add("la especialización"); english.add("major");
spanish.add("el examen"); english.add("test");
spanish.add("el horario"); english.add("schedule");
spanish.add("la prueba"); english.add("quiz");
spanish.add("la tarea"); english.add("homework");
spanish.add("la administración"); english.add("business");
spanish.add("de empresas"); english.add("administration");
spanish.add("las ciencias"); english.add("sciences");
spanish.add("la contabilidad"); english.add("accounting");
spanish.add("el periodismo"); english.add("journalism");
spanish.add("la química"); english.add("chemistry");
spanish.add("al lada de"); english.add("next to");
spanish.add("a la derecha de"); english.add("to the right of");
spanish.add("a la izquierda de"); english.add("to the left of");
spanish.add("cerca de"); english.add("near");
spanish.add("debajo de"); english.add("below");
spanish.add("delante de"); english.add("in front of");
spanish.add("detrás de"); english.add("behind");
spanish.add("encima de"); english.add("on top of");
spanish.add("entre"); english.add("between; among");
spanish.add("lejos de"); english.add("far from");
spanish.add("sin"); english.add("without");
spanish.add("sobre"); english.add("on; over");
spanish.add("Adónde?"); english.add("Where (to)?");
spanish.add("ahora"); english.add("now");
spanish.add("Cuál? Cuáles?"); english.add("Which? Which one?");
spanish.add("Por que?"); english.add("Why?");
spanish.add("Porque"); english.add("because");
spanish.add("Cuándo?"); english.add("When?");
spanish.add("Qué día es hoy?"); english.add("What day is it?");
spanish.add("la semana"); english.add("week");
spanish.add("Lunes"); english.add("Monday");
spanish.add("Martes"); english.add("Tuesday");
spanish.add("Miércoles"); english.add("Wednesday");
spanish.add("Jueves"); english.add("Thursday");
spanish.add("Viernes"); english.add("Friday");
spanish.add("Sábado"); english.add("Saturday");
spanish.add("Domingo"); english.add("Sunday");
}
//method to grab a word or phrase from the spanish list
public String getOne() {
int x = random.nextInt(spanish.size());
setIndex(x);
return spanish.get(getIndex());
}
}
Code:
import javax.swing.*;
import java.io.*;
import java.util.Random;
public class Player extends Thread{
private JTextField text; //make the text field
private JTextField input; //make an input text field
static Library library = new Library(); //make a library object
static Random random = new Random(); //make a random object
public Player() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
text = new JTextField("Output field", 30);
input = new JTextField("Input field", 30);
panel.add(text);
panel.add(input);
frame.add(panel);
frame.setVisible(true);
frame.pack();
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
Player player = new Player(); //make a player
library.makeLists(); //make the lists
player.start(); //start
}
public void run() {
try {
text.setVisible(true); //make the field visible
boolean loop = true;
while(loop) { //loop forever
text.setText(library.getOne() + "\n"); //set the text to a random spanish phrase
input.setText("");
input.grabFocus();
Thread.sleep(5000); //wait for 5 seconds
if(input.getText().equalsIgnoreCase(library.english.get(library.getIndex())))
{
text.setText("Right!"); //if i enter in the correct english translation, i'm right
Thread.sleep(1000);
}
else {
text.setText("Wrong. The answer is " + library.english.get(library.getIndex())); //if i'm wrong, display the right answer
Thread.sleep(1000);
}
}
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}
-
I think what you want is a message dialog.
check out: javax.swing.JOptionPane
-
Ya, OP need to look at JOptionPane. Here is the Suns' link.
JOptionPane (Java 2 Platform SE v1.4.2)
-
I hate to nag, but what really bugs me about your program is all the hard-coded data that essentially straight-jackets the program. You can simplify your program greatly and at the same time significantly increase its readability, debugability and extensibility by placing the data elements (the phrases) into a text file or xml file or whatever, and having your program read it in to a map on start-up.
Best of luck and HTH.
-
I haven't had time to check out the JOptionPane just yet, but as for Fubarable's post -- how would I go about doing that? Just saving all of the elements into Notepad (or some other text editor)...then what? What do you mean by reading it into a map? If it will make my program that much better, I am very anxious to know.
-
You can put all the text into a simple text file with one line holding the Spanish phrase and the corresponding English separated somehow by some type of delimiter. One type of file that can be used is a CSV file, but to simplify this, I'd separate the two phrases by a simple pair of characters that would never be found in either your Spanish or English text such as "@@".
Then in the body of the program, read in the file, say with a Scanner object, split each String with the delimiter to extract the Spanish and English phrases. Then place them in two maps, one for English to Spanish, and one for Spanish to English (or you can use a multi-map if you can get your hands on one). Then use the maps to translate. For instance:
Dictionary.txt which for me is located at C:\Users\Pete\Documents\workspace\Pete 2009\src\yr2009\m05\c\Dictionary.txt
Code:
Qué tal?@@How are you?
Con permiso@@Pardon me
Lo siento@@I'm sorry
No hay de qué@@You're welcome
De nada@@You're welcome
Mucho gusto@@Pleased to meet you
Le presento a...@@I would like to introduce...
Te presento a...@@I would like to introduce...(form)
Soy de...@@I'm from...
Cuántos?@@How many?
el autobús@@bus
la capital@@capital city
el chico@@boy
la chica@@girl
DictionaryEg.java
Code:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class DictionaryEg {
private static final String DELIMITER = "@@";
private Map<String, String> englishToSpanishMap = new HashMap<String, String>();
private Map<String, String> spanishToEnglishMap = new HashMap<String, String>();
public DictionaryEg(Scanner scanner) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] tokens = line.split(DELIMITER);
if (tokens.length == 2) {
String spanish = tokens[0].trim();
String english = tokens[1].trim();
englishToSpanishMap.put(english, spanish);
spanishToEnglishMap.put(spanish, english);
}
}
}
public Set<String> getSpanishKeySet() {
return spanishToEnglishMap.keySet();
}
public Set<String> getEnglishKeySet() {
return englishToSpanishMap.keySet();
}
public String englishToSpanish(String key) {
return englishToSpanishMap.get(key);
}
public String spanishToEnglish(String key) {
return spanishToEnglishMap.get(key);
}
}
DictionaryTest.java
Code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class DictionaryTest {
// TODO: change this to match the file location
private static final String DICTIONARY_FILE = "src/yr2009/m05/c/Dictionary.txt";
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new File(DICTIONARY_FILE));
DictionaryEg dictionary = new DictionaryEg(scanner);
for (String spanishKey : dictionary.getSpanishKeySet()) {
System.out.printf("%-22s %-22s%n", spanishKey, dictionary.spanishToEnglish(spanishKey));
}
}
}
-
What do the %-22s %-22s%n do?
-
System.out.printf uses C printf style strings to help format output. This is best described in the Formatter class API and in the Formatting section of the Sun tutorial. You can find both here:
Formatter (Java Platform SE 6)
Formatting (The Java™ Tutorials > Essential Classes > Basic I/O)