Java text to speech I can't get it to talk - java

I have been trying to make a text to speech program i'm on a windows pc just for reference. I can't get my program to say what i've told it to. If someone can help me fix this or point me to a resource that will help me fix it, it will be much appreciated
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class javatalker extends JFrame{
private static final long serialVersionUID = 1L;
JPanel panel = new JPanel();
JTextField textfield = new JTextField(35);
JButton button = new JButton("Push To Talk");
JCheckBox checkbox1 = new JCheckBox("Normal");
JCheckBox checkbox2 = new JCheckBox("Blitzcrank");
public javatalker() {
panel.add(textfield);
panel.add(button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if(textfield.getText().equalsIgnoreCase("")){
System.out.println("Typle in a string");
} else{
Runtime rt = Runtime.getRuntime();
try{
if(checkbox1.isSelected() == true){
Process p = rt.exec("say" + textfield.getText());
}
if(checkbox2.isSelected() == true){
Process p = rt.exec("say -v Cellos" + textfield.getText());
} else{
System.out.println("Please select a voice");
}
}catch(Exception ex) {
ex.getStackTrace();
}
}
}
});
panel.add(checkbox1);
panel.add(checkbox2);
panel.setBackground(Color.black);
add(panel);
setTitle("Voicip");
setVisible(true);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
public static void main(String args[]){
new javatalker();
}
}

say command does not exist on windows.
You could create a script
#echo off
echo Dim Speak >> %HOMEPATH%\speak.vbs
echo Set Speak=CreateObject("sapi.spvoice") >>
%HOMEPATH%\speak.vbs
echo Speak.Speak "%1">> %HOMEPATH%\speak.vbs
%HOMEPATH%\speak.vbs
del %HOMEPATH%\speak.vbs
pasted from https://superuser.com/questions/223913/os-x-say-command-for-windows
Name the script speak.bat and put it in C:\Windows\system32
Then modify
Process p = rt.exec("say" + textfield.getText());
to
Process p = rt.exec("speak" + textfield.getText());
You might also have a look at espeak which is open-source.

Related

Having a user search and input a file in Java

I am working a side project that involves the user entering a file. I know there is the option for them to enter a string representing the text files name and then have the computer search for it but that would mean the file has to be in a certain location on the computer or the user has to enter the whole pathway to the file.
Is there anything similar to file input tag in HTML5 that prompts a window that allows the user to search through their laptop for the file? I've attached a picture of what I mean would be prompted on a mac.
Use file chooser by swing
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class FileChooserTest extends JFrame {
private JTextField filename = new JTextField(), dir = new JTextField();
private JButton open = new JButton("Open"), save = new JButton("Save");
public FileChooserTest() {
JPanel p = new JPanel();
open.addActionListener(new OpenL());
p.add(open);
save.addActionListener(new SaveL());
p.add(save);
Container cp = getContentPane();
cp.add(p, BorderLayout.SOUTH);
dir.setEditable(false);
filename.setEditable(false);
p = new JPanel();
p.setLayout(new GridLayout(2, 1));
p.add(filename);
p.add(dir);
cp.add(p, BorderLayout.NORTH);
}
class OpenL implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser c = new JFileChooser();
// Demonstrate "Open" dialog:
int rVal = c.showOpenDialog(FileChooserTest.this);
if (rVal == JFileChooser.APPROVE_OPTION) {
filename.setText(c.getSelectedFile().getName());
dir.setText(c.getCurrentDirectory().toString());
}
if (rVal == JFileChooser.CANCEL_OPTION) {
filename.setText("You pressed cancel");
dir.setText("");
}
}
}
class SaveL implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser c = new JFileChooser();
// Demonstrate "Save" dialog:
int rVal = c.showSaveDialog(FileChooserTest.this);
if (rVal == JFileChooser.APPROVE_OPTION) {
filename.setText(c.getSelectedFile().getName());
dir.setText(c.getCurrentDirectory().toString());
}
if (rVal == JFileChooser.CANCEL_OPTION) {
filename.setText("You pressed cancel");
dir.setText("");
}
}
}
public static void main(String[] args) {
run(new FileChooserTest(), 250, 110);
}
public static void run(JFrame frame, int width, int height) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setVisible(true);
}
} ///:~
File dialog

user input for JLabel

I have a problem with a piece of code. When I click on it in my GUI, it reopens once I've inputted text. Can anyone explain to me what is wrong with the code. I'm using this to set a name in a JLabel in my GUI
setNameButton.addActionListener((ActionEvent e) -> {
String usernameinput;
String defaultUsername = "dom" + "baker";
usernameinput = JOptionPane.showInputDialog(
setNameButton, "Enter a username",
"Set username", JOptionPane.OK_CANCEL_OPTION);
{
username.setText(String.valueOf(usernameinput));
}
});
I've created a simple GUI to test your code and the dialog opens just once.
I have cleaned up a bit your listener, but basically it's the same code.
Your problem may be in another part of your code.
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class SimpleFrameTest extends JFrame {
JLabel username = new JLabel("Press button to enter your name here");
public SimpleFrameTest() {
setSize(300, 300);
setTitle("Test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(true);
initComponents();
setVisible(true);
}
private void initComponents() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
username.setAlignmentX(Component.CENTER_ALIGNMENT);
JButton setNameButton = new JButton("Set name");
setNameButton.setAlignmentX(Component.CENTER_ALIGNMENT);
setNameButton.addActionListener((ActionEvent e) -> {
String usernameinput = JOptionPane.showInputDialog(setNameButton, "Enter a username", "Set username", JOptionPane.OK_CANCEL_OPTION);
if (usernameinput != null) {
username.setText(String.valueOf(usernameinput));
}
});
panel.add(Box.createRigidArea(new Dimension(5,10)));
panel.add(username);
panel.add(Box.createRigidArea(new Dimension(5,10)));
panel.add(setNameButton);
add(panel);
}
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SimpleFrameTest();
}
});
}
}
your code is behaving like that because of this
setNameButton.addActionListener((ActionEvent e) -> {
String usernameinput;
String defaultUsername = "dom"
+ "baker";
usernameinput = JOptionPane.showInputDialog(null,
as you can see, the JOptionPane.showInputDialog(setNameButton is taking the setNameButton as a parameter so you are in some kind of recursive infinite loop
use another button for the modalDialog:
Example:
setNameButton.addActionListener((ActionEvent e) -> {
String usernameinput;
String defaultUsername = "dom"
+ "baker";
usernameinput = JOptionPane.showInputDialog(null, "Enter a username", "Set username", JOptionPane.OK_CANCEL_OPTION);
{
username.setText(String.valueOf(usernameinput));
}
});

Adding an int per click (java)

I am making a "fake virus" in java. when you run it a window called "Your computer has a virus" pops up and the window has a button that says "Your computer has (1) viruses. Click here to uninstall them" but when you click it one more window pops up. but i want it to be like each time you click it the number of "viruses" is added 1 to. (For example the second window that pops up after clicking button says "Your computer has (2) viruses"). I have tried to add it but it didn't work. (sorry for my terrible grammar). Here is my code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class FirstWindow extends JFrame {
int virusAmount = 1;
private static final long serialVersionUID = 1L;
public FirstWindow(){
super("Your computer has a virus");
setSize(400, 75);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel p = new JPanel();
JButton b = new JButton("Your computer has (" + virusAmount++ + ") virus(es). Click here to uninstall them.");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FirstWindow f2 = new FirstWindow();
f2.setVisible(true);
}
});
p.add(b);
add(p);
}
}
Just define it in the constructor:
public FirstWindow(int i){}
Full example:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class FirstWindow extends JFrame {
int virusAmount;
private static final long serialVersionUID = 1L;
public FirstWindow(int i) {
virusAmount = i;
setSize(400, 75);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel p = new JPanel();
JButton b;
if(virusAmount == 1){
b = new JButton("Your computer has a virus");
}
else{
b = new JButton("Your computer has (" + virusAmount + ") virus(es). Click here to uninstall them.");
}
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FirstWindow f2 = new FirstWindow(virusAmount+1);
f2.setVisible(true);
}
});
p.add(b);
add(p);
}
}
public class Main {
public static void main(String[] args) {
FirstWindow fw = new FirstWindow(1);
fw.setVisible(true);
}
}

Getting rid of Dialog Boxes and replacing with JLabel

I am currently working on an applet and am having a bit of trouble finishing it off. My code works just fine however I need to change the final portion from a JOptionDialog Message Dialog into just a JLabel that gets added to the applet. I've tried every way I can think of and am still coming up short. My current code looks as followed:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Password extends JApplet implements ActionListener {
Container PW = getContentPane();
JLabel password = new JLabel("Enter Password(and click OK):");
Font font1 = new Font("Times New Roman", Font.BOLD, 18);
JTextField input = new JTextField(7);
JButton enter = new JButton("OK");
public void start() {
PW.add(password);
password.setFont(font1);
PW.add(input);
PW.add(enter);
PW.setLayout(new FlowLayout());
enter.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String pass1 = input.getText();
String passwords[] = {"Rosebud", "Redrum", "Jason", "Surrender", "Dorothy"};
for(int i=0;i<passwords.length;i++) {
if (pass1.equalsIgnoreCase(passwords[i])) {
JOptionPane.showMessageDialog(null, "Access Granted");
return
}
else {
JOptionPane.showMessageDialog(null, "Access Denied");
}
}
}
}
Please help!
Try this one:
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Password extends JApplet implements ActionListener {
Container PW = getContentPane();
JLabel password = new JLabel("Enter Password(and click OK):");
JLabel message = new JLabel();
Font font1 = new Font("Times New Roman", Font.BOLD, 18);
JTextField input = new JTextField(7);
JButton enter = new JButton("OK");
public void start() {
PW.add(password);
password.setFont(font1);
PW.add(input);
PW.add(enter);
PW.add(message);
PW.setLayout(new FlowLayout());
enter.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String pass1 = input.getText();
String passwords[] = {"Rosebud", "Redrum", "Jason", "Surrender", "Dorothy"};
for(int i=0;i<passwords.length;i++) {
if (pass1.equalsIgnoreCase(passwords[i])) {
message.setText("Access Granted");
return;
}
else {
message.setText("Access Denied");
}
}
}
}
Its sample code so no alignment is done it will show message next to button. You can change alignment as you wish ;)

Java program exits when I click OK button in JOptionPane.showMessageDialog

when I click the OK button in second.java program, the program exit the program. I want it not to exit (since there is a thread running). I tried removing setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE).
CODE
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class second extends JFrame implements ActionListener {
JLabel enterName;
JTextField name;
JButton click;
String storeName;
public second(){
setLayout(null);
setSize(300,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
enterName = new JLabel("Enter Your Name: ");
click = new JButton("Click");
name = new JTextField();
enterName.setBounds(60,30,120,30);
name.setBounds(80,60,130,30);
click.setBounds(100,190,60,30);
click.addActionListener(this);
add(click);
add(name);
add(enterName);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == click) {
storeName = name.getText();
JOptionPane.showMessageDialog(null, "Hello" + storeName);
System.exit(0);
}
}
public static void main(String args[]){
second s = new second();
s.setVisible(true);
}
}
Many Thanks
You'll need to remove the System.exit(0); line. That's all.

Categories