I edited the whole class.I try to convert an ItemID which is displayed in displayArea JTextArea(is redirected here from system.out) to an ItemName which is displayed in a messagedialog box after I press button "Get".I've got a Hashtable table to do the conversion.Here is part of my code:
public class HashtableTest2 extends JFrame {
private JLabel statusLabel;
private Hashtable<String,String> table;
private JTextArea displayArea;
private JTextField movieNameField;
private JTextField firstNameField;
@SuppressWarnings("unused")
private PrintStream standardOut;
private JTextArea textField;
private JTextField textField_1;
// set up GUI to demonstrate Hashtable features
public HashtableTest2()
{
super( "Movie Recommender" );
statusLabel = new JLabel();
table = new Hashtable<String,String>();
displayArea = new JTextArea( 4, 20 );
displayArea.setTabSize(8);
displayArea.setEditable( false );
//Redirect console to displayArea
PrintStream printStream = new PrintStream(new CustomOutputStream(displayArea));
standardOut = System.out;
System.setOut(printStream);
System.setErr(printStream);
JPanel northSubPanel = new JPanel();
northSubPanel.add( new JLabel( "ID" ) );
firstNameField = new JTextField( 8 );
northSubPanel.add( firstNameField );
northSubPanel.add( new JLabel( " movie name (key)" ) );
movieNameField = new JTextField( 8 );
northSubPanel.add( movieNameField );
JPanel northPanel = new JPanel();
northPanel.setLayout( new BorderLayout() );
northPanel.add( northSubPanel, BorderLayout.NORTH );
northPanel.add( statusLabel, BorderLayout.SOUTH );
JPanel southPanel = new JPanel();
southPanel.setLayout( new GridLayout( 2, 5 ) );
// adds event handler for ID textArea displayed in MovieName textArea
movieNameField.addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent arg0) {
table.put("GoldenEye","2");
table.put("Four Rooms", "3");
table.put("Get Shorty","4");
table.put("Copycat","5");
table.put("Shanghai Triad","6");
table.put("Twelve Monkeys","7");
table.put("Babe","8");
table.put("Dead Man Walking","9");
table.put("Richard III","10");
table.put("204", "Back");
table.put("5", "Copycat");
table.put("7", "Twelve Monkeys");
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
});
// button to get value for specific key
JButton getButton = new JButton( "Get" );
getButton.addActionListener(
new ActionListener() {
// get value for specific key
public void actionPerformed( ActionEvent event )
{
Object value = table.get( movieNameField.getText() );
firstNameField.setText(value.toString());
TestItemRecommend other = new TestItemRecommend(HashtableTest2.this);
other.simple_2();
String t = displayArea.getText();
textField_1.setText(t);
String c = textField_1.getText();
//Display In messagedialog box what displayArea shows and Convert it from ID to Name
JTextArea panel = new JTextArea();
panel.setText(t);
JScrollPane scrollPane = new JScrollPane(panel);
JOptionPane.showMessageDialog(null, scrollPane,"Movies We Recommend", 0);
}
}
);
southPanel.add( getButton );
textField_1 = new JTextField();
southPanel.add(textField_1);
textField_1.setColumns(10);
// button to display hash table elements
JButton listElementsButton = new JButton( "List objects" );
listElementsButton.addActionListener(
new ActionListener() {
// display hash table elements
public void actionPerformed( ActionEvent event )
{
String d = textField_1.getText();
String e = table.get(d);
if (e==null) e= "Nothing Found.";
textField.setText(e);
StringBuffer buffer = new StringBuffer();
for ( Enumeration<String> enumeration = table.elements();
enumeration.hasMoreElements(); )
buffer.append(
enumeration.nextElement() ).append( '\n' );
displayArea.setText( buffer.toString() );
}
}
);
southPanel.add( listElementsButton );
and I'm getting no result.I found that the problem is in String e for which I get a null!If I manually set the value of textField_1,I get result,but when I'm asking to do this automatically,(by Code)it doesn't.What do you suggest? Thank you!