Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JScrollPane not working

807600Nov 18 2007 — edited Nov 20 2007
Ok, so I'm trying to get this scroll pane to work on the text area..... Anyone able to help?

<code>
// TextSpeaker.java
// Remember to set the classpath as following: "c:\freetts\freetts-1.2.1\lib\jsapi.jar;c:\freetts\freetts-1.2.1\lib\freetts.jar;"
// @author Jeff Friesen
// @version 04/13/2006
// Modified by: Ocdt McClafferty, L.M. 24656
import java.awt.*;
import java.awt.event.*;
import javax.speech.*;
import javax.speech.synthesis.*;
import javax.swing.*;

public class TextSpeaker
{
static Synthesizer synth;
static JScrollPane jScrollPane1;

public static JFrame createGUI ()
{
JFrame frame = new JFrame ("Text Speaker");
WindowListener wl;
wl = new WindowAdapter ()
{
public void windowClosing (WindowEvent e)
{
try
{
// Deallocate synthesizer
// resources.

synth.deallocate ();
}
catch (EngineException e2)
{
}
System.exit (0);
}
}
;
frame.addWindowListener (wl);
JPanel p = new JPanel ();
p.add (new JLabel ("Specify text to " + "speak:"));
final JTextArea text = new JTextArea (40, 90);
text.setColumns(90);
text.setLineWrap(true);
text.setRows(40);
text.setWrapStyleWord(true);
jScrollPane1 = new JScrollPane(text);
p.add (text);
frame.getContentPane ().add (p, BorderLayout.NORTH);
p = new JPanel ();
p.setLayout (new FlowLayout (FlowLayout.RIGHT));
JButton btnSpeak = new JButton ("Speak");
ActionListener al;
al = new ActionListener ()
{
public void actionPerformed
(ActionEvent e)
{
// Speak the context of the text
// field, ignoring JSML tags.
// Pass null as the second
// argument because I am not
// interested in attaching a
// listener that receives events
// as text is spoken.

synth.speakPlainText (text.getText (), null);

try
{
// Block this thread until
// the synthesizer's queue
// is empty (all text has
// been spoken). Normally,
// blocking the
// event-dispatching thread
// is not a good idea.
// However, the amount of
// text to be spoken should
// not take more than a few
// seconds to speak, and the
// user probably would not
// need to do anything with
// the GUI until the text
// had been spoken.

synth.waitEngineState (Synthesizer.QUEUE_EMPTY);
}
catch (InterruptedException e2)
{
}
}
}
;
btnSpeak.addActionListener (al);

p.add (btnSpeak);

JButton btnClear = new JButton ("Clear");

al = new ActionListener ()
{
public void actionPerformed (ActionEvent e)
{
text.setText ("");
text.requestFocusInWindow ();
}
}
;
btnClear.addActionListener (al);
p.add (btnClear);
frame.getContentPane ().add (p, BorderLayout.SOUTH);
frame.getRootPane ().setDefaultButton (btnSpeak);
frame.pack ();

return frame;
}


public static void main (String[] args)
{
try
{
// Create a synthesizer for the default
// locale.

synth = Central.createSynthesizer (null);

// Allocate synthesizer resources.

synth.allocate ();

// Place synthesizer in the RESUMED
// state so that it can produce speech
// as it receives text.

synth.resume ();
}
catch (Exception e)
{
JOptionPane.showMessageDialog (null, e.getMessage ());
System.exit (0);
}

createGUI ().setVisible (true);
}
}

</code>
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 18 2007
Added on Nov 18 2007
2 comments
134 views