Why does my audio playback freeze ActionListners? - java

Im using the this javaPlayer to playback my sounds.
But in my larger
project it freezes/ignores almost every input JFrame.EXIT_ON_CLOSE and ActionListners that alter the frame content. I tried to repruduce the issue in this snippet.
The btn.addActionListener(e -> System.out.println("Check1")); is not applied to my button. Only if I comment out the whole try/catch-block the "Check1" is also reached.
What could be the problem? I already tried to send the playerpart with Swing.invokeLater to another thread.
package mainMVC;
import java.io.FileInputStream;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
public class Alone {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JButton btn = new JButton("Test");
btn.addActionListener(e -> System.out.println("Check"));
frame.add(btn);
frame.pack();
try (FileInputStream fis = new FileInputStream("src/EgyptianTavernFullofGuitarists_1.mp3"))
{
Player player = new Player(fis);
player.play();
} catch (IOException | JavaLayerException e) {
e.printStackTrace();
}
btn.addActionListener(e -> System.out.println("Check1"));
}
}

I don't know what's a problem. I've tested your code in my IDE, and it works. During start Frame, music plays. ActionListener returns Check and music doesn't stop, same EXIT_ON_CLOSE.

EDIT: I found that execution is stopped till the playback of the file is finished.
I managed it with a manual thread creation. Now both "Check" and "Check1" are printed.
File: Audio.java
package mainMVC;
import java.io.FileInputStream;
import java.io.IOException;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
class Audio extends Thread {
public void run(){
try (FileInputStream fis = new FileInputStream("src/EgyptianTavernFullofGuitarists_1.mp3"))
{
Player player = new Player(fis);
player.play();
} catch (IOException | JavaLayerException e) {
e.printStackTrace();
}
}
}
File: Alone.java
package mainMVC;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Alone {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JButton btn = new JButton("Test");
btn.addActionListener(e -> System.out.println("Check"));
frame.add(btn);
frame.pack();
Audio myThread = new Audio();
myThread.start();
btn.addActionListener(e -> System.out.println("Check1"));
}
}

Related

no icon shows on java swing in linux ubunutu

I copied the path of the image but no icon appears click on the image to see
no icon
I created simple frame and needed to include icon
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.awt.Color;
public class App {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("JFrame title goes here");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //exit out of app
frame.setResizable(false); // prevent frame from being resized
frame.setSize(700,700);
frame.setVisible(true);
try {
URL resource = frame.getClass().getResource("/games.png");
BufferedImage image = ImageIO.read(resource);
frame.setIconImage(image);
} catch (IOException e) {
e.printStackTrace();
}
frame.getContentPane().setBackground(new Color(123,50,250));
}
}

Swing GUI freezes after clicking JButton for three times [duplicate]

I wrote a simple application in Swing that writes text to a file. Here is my main class:
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class WritingTextToFileApp {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new MainFrame("Application");
frame.setSize(500, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
Here is the other class:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class MainFrame extends JFrame {
public MainFrame(String title) {
super(title);
//Set Layout Manager
setLayout(new BorderLayout());
//Create Swing Components
JTextArea textArea = new JTextArea();
JButton button = new JButton("Add");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
File file = new File("C:\\Users\\Vincent Wen\\Desktop\\Test.txt");
try (BufferedWriter br = new BufferedWriter(new FileWriter(file))) {
br.write(input);
br.newLine();
} catch (IOException ex) {
System.out.println("Unable to write to file:" + file.toString());
}
}
});
//Add Swing components to conent pane
Container c = getContentPane();
c.add(textArea, BorderLayout.CENTER);
c.add(button, BorderLayout.SOUTH);
}
}
Whenever I press the button, the program freezes and nothing happens. Is there something wrong with the code? I am new to Swing so any help would be appreciated.
Swing runs the actions synchronously in the same thread that's handling the GUI input and rendering. That means that when you click the button, it waits for the action listener to complete running before it goes back to handling input and drawing the GUI. In this case, it's effectively stopping the GUI from running until you type something into the console.
You can use SwingWorker to run it asynchronously so that it continues running the GUI while it runs the action.
The problem is that when you press the button, java expects to read data from System.ini (console).
Try to start your application by using the java command on a console. Then enter some text in the console after pressing the button and press enter. Your program you work.
I fixed my problem by using textArea.getText() instead of using scanner.

Read JTextfield put into word file

I create a small GUI with Netbeans. I've got an issue with settext and gettext. I'm raelly happy if you can say whre the problem is and what i have to do or you show me the solution.
i want to create a word file by clicking a button. This is working fine but there should be some text out of a JTextfiel in the word file and this isnt working.
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.HeadlessException;
import java.io.FileOutputStream;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.IOException;
private void createActionPerformed(java.awt.event.ActionEvent evt) {
try{
FileOutputStream outStream = new FileOutputStream("Bewerberinterview.docx");
XWPFDocument doc;
doc = new XWPFDocument();
XWPFParagraph paraTit=doc.createParagraph();
paraTit.setAlignment(ParagraphAlignment.CENTER);
XWPFRun paraTitRun=paraTit.createRun();
paraTitRun.setBold(true);
paraTitRun.setFontSize(20);
paraTitRun.setText(title.getText());
doc.createParagraph().createRun().addBreak();
doc.createParagraph().createRun().setText(name_content.getText());
doc.write(outStream);
doc.close();
System.out.println("createdocument.docx written successully");
}catch (HeadlessException | IOException e){
JOptionPane.showMessageDialog(null, e);
}
}
When I starting my application and put in some Text in the box and clicking "button 1 = create". The file will create fine but there is no text in it.
The following code, which is a Minimal, Reproducible Example for your described problem, works as I would expect it should.
For each click on button write it writes the file Bewerberinterview.docx file having content from the both text fields.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import org.apache.poi.xwpf.usermodel.*;
import java.io.File;
import java.io.FileOutputStream;
public class TextDemo extends JPanel implements ActionListener {
protected JTextField title;
protected JTextField name_content;
protected JButton write;
public TextDemo() {
super();
title = new JTextField(20);
name_content = new JTextField(20);
write = new JButton("write");
write.addActionListener(this);
add(title);
add(name_content);
add(write);
}
public void actionPerformed(ActionEvent evt) {
String titleText = title.getText();
String name_contentText = name_content.getText();
System.out.println(titleText);
System.out.println(name_contentText);
try {
FileOutputStream outStream = new FileOutputStream("Bewerberinterview.docx");
XWPFDocument doc = new XWPFDocument();
XWPFParagraph paragraph = doc.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun run = paragraph.createRun();
run.setBold(true);
run.setFontSize(20);
run.setText(titleText);
doc.createParagraph().createRun().addBreak();
doc.createParagraph().createRun().setText(name_contentText);
doc.write(outStream);
outStream.close();
doc.close();
System.out.println("File " + new File("Bewerberinterview.docx").getAbsolutePath() + " written successully.");
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TextDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add contents to the window.
frame.add(new TextDemo());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
So your described problem does not exists.
It does? Well then show a Minimal, Reproducible Example which shows the problem.

Java Swing button freezes program

I wrote a simple application in Swing that writes text to a file. Here is my main class:
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class WritingTextToFileApp {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new MainFrame("Application");
frame.setSize(500, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
Here is the other class:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class MainFrame extends JFrame {
public MainFrame(String title) {
super(title);
//Set Layout Manager
setLayout(new BorderLayout());
//Create Swing Components
JTextArea textArea = new JTextArea();
JButton button = new JButton("Add");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
File file = new File("C:\\Users\\Vincent Wen\\Desktop\\Test.txt");
try (BufferedWriter br = new BufferedWriter(new FileWriter(file))) {
br.write(input);
br.newLine();
} catch (IOException ex) {
System.out.println("Unable to write to file:" + file.toString());
}
}
});
//Add Swing components to conent pane
Container c = getContentPane();
c.add(textArea, BorderLayout.CENTER);
c.add(button, BorderLayout.SOUTH);
}
}
Whenever I press the button, the program freezes and nothing happens. Is there something wrong with the code? I am new to Swing so any help would be appreciated.
Swing runs the actions synchronously in the same thread that's handling the GUI input and rendering. That means that when you click the button, it waits for the action listener to complete running before it goes back to handling input and drawing the GUI. In this case, it's effectively stopping the GUI from running until you type something into the console.
You can use SwingWorker to run it asynchronously so that it continues running the GUI while it runs the action.
The problem is that when you press the button, java expects to read data from System.ini (console).
Try to start your application by using the java command on a console. Then enter some text in the console after pressing the button and press enter. Your program you work.
I fixed my problem by using textArea.getText() instead of using scanner.

Exported Java program splash screen not showing

So I created a simple java program(not important) and in eclipse I have a gif file that is the splash screen. When I exported the program into a jar the splash screen came up blank. Any help with this? Thanks in advance. Here is the main class:
package com.ethan.main;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JWindow;
import javax.swing.SwingConstants;
public class SingAlong {
public static void main (String args[]) {
int close = 0;
JWindow window1 = new JWindow();
window1.getContentPane().add(
new JLabel("", new ImageIcon("res/Backgrounds/Final GIF.gif"), SwingConstants.LEFT));
window1.setBounds(0, 100, 300, 200);
window1.setVisible(true);
if(close == 1){
window1.dispose();
}
try {
Thread.sleep(6850);
close = 1;
}
catch (InterruptedException e) {
e.printStackTrace();
}
JFrame window = new JFrame("Science Quiz");
window.setContentPane(new SingAlongMain());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(true);
window.pack();
window.setVisible(true);
}
}

Categories