-
TextField loading
Hello everyone :)
So I have this program :
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import javax.swing.*;
public class StrikerFrame extends Frame implements ActionListener{
Button btnGo, btnBack;
List lstStrikers;
Label lblName;
TextField txtPlayer;
public StrikerFrame(){
setLayout(null);
btnBack = new Button ("BACK <");
btnBack.addActionListener(this);
btnGo = new Button("GO >");
btnGo.addActionListener(this);
lstStrikers = new List(19);
lstStrikers.addActionListener(this);
lblName = new Label("NOW CHOOSE YOUR FAVOURITE STRIKER:::");
txtPlayer = new TextField();
txtPlayer.setEditable(false);
try{
FileReader fr = new FileReader("Strikers.txt");
Scanner s = new Scanner(fr);
while(s.hasNext()){
String line = s.nextLine();
lstStrikers.add(line);
}
s.close();
}catch(Exception e){
System.out.println("FAILED");
}
lstStrikers.setBounds(100,100,600,325);
btnBack.setBounds(500,525,100,50);
btnGo.setBounds(200,525,100,50);
lblName.setBounds(100,50,700,50);
txtPlayer.setBounds(100,450,700,50);
try{
FileReader fread = new FileReader("Strikers.txt");
Scanner scan = new Scanner(fread);
char z = lstStrikers.getSelectedIndex();
String sLine = scan.readLine(z);
txtPlayer.add(sLine);
scan.close();
}catch(Exception e){
System.out.println("FAILED");
}
add(lstStrikers);
add(btnBack);
add(btnGo);
add(lblName);
add(txtPlayer);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==btnBack){
setVisible(false);
}else if(ae.getSource()==btnGo){
System.exit(0);
}
}
}
And the thing I don't know is where i have the Bold text.
The program will show a list of Players and the person will choose the player by clicking on him from the list.
Therefore I want that the player chosen will be shown in a Textfield underneath the list and show the user which player he chose.
I don't know exactly what to do :S
(I use java awt :P)
Thanks very much for this useful forum :)
Drew
-
Re: TextField loading
Don't use AWT but rather use Swing as it's more powerful and flexible. Google the Swing layout manager tutorials which will show you how to place your components where you want them to be, and then the JList or JComboBox tutorials which will show you how to display a selectable list. To set the text of a JTextField, simply call setText(yourStringHere) on it.