Making JavaFX FileChooser not show on the taskbar when used in Swing - java

I have looked at this page on how to use the JavaFX FileChooser with Swing, and it works well:
JavaFX FileChooser in swing
I have also looked at this page on how to make a stage now show in the taskbar with StageStyle.UTILITY:
JavaFX: can you create a stage that doesn't show on the task bar and is undecorated?
However, when I combine the two, the file dialog that I use in the swing application opens a separate window in the taskbar, that does not appear with JFileChooser. I was wondering how to make the JavaFX FileChooser not show in the taskbar when using it in a swing application.
Here is a snippet of code that demonstrates the issue:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFrame;
import javafx.application.Platform;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class Test extends JFrame implements ActionListener {
private JButton button;
private FileChooser chooser;
public Test(){
super();
setDefaultCloseOperation(EXIT_ON_CLOSE);
button = new JButton("Test");
button.addActionListener(this);
add(button);
new javafx.embed.swing.JFXPanel();
Platform.setImplicitExit(false);
chooser = new FileChooser();
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
Platform.runLater(new Runnable(){
public void run(){
Stage stage = new Stage();
stage.initStyle(StageStyle.UTILITY);
File testfile = chooser.showOpenDialog(stage);
}
});
}
public static void main(String[] args){
new Test();
}
}

Related

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.

Long menu item in Java AWT application not visible

When I start Java application with AWT GUI on remote server that uses my local X-server (Ubuntu 16.04 with Unity), menu items with long names are not visible. It is very interesting that I can see this behavior only on one specific server whilst it works on others.
Demonstration code is:
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Main extends Frame {
public Main() {
this.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Menu mn = new Menu("File");
MenuItem mi = new MenuItem("It is very very long text");
mn.add(mi);
mi = new MenuItem("long");
mn.add(mi);
MenuBar mb = new MenuBar();
mb.add(mn);
this.setLayout(new BorderLayout());
this.setMenuBar(mb);
this.setSize(200,200);
this.setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}

Is it possible to embed an external program in JavaFX?

I'm currently developing a JavaFX desktop application that will take the place of an existing browser based application.
This new application have the option of launching external applications made in swing.
I'm now wondering if there's any chance of launching these external applications inside the existing JavaFX application?
In other words, is there a way to embed external applications in JavaFX?
Do you mean something like this?
JavaFX:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
Button fxButton = new Button("javafx button");
fxButton.setOnAction(event -> new SwingTest());
Scene scene = new Scene(fxButton, 600, 400);
stage.setScene(scene);
stage.show();
}
}
Swing:
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import java.awt.Button;
public class SwingTest extends JFrame {
SwingTest() {
Button swingButton = new Button("swing button");
getContentPane().add(swingButton);
setSize(600, 600);
setVisible(true);
}
}

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.

Resize JDialog by pushing it against the edge of the screen

I have a JFrame and a JDialog. The latter simply contains a JTextArea. Windows 7 gives now the possibility to resize a window to the half of the screen by pushing it against the right or the left edge. This works fine with the JFrame window. However, the JDialog window does not react to it, though it is manually resizable. I suppose it is closely connected with the missing maximize/minimize function of JDialogs. On the other hand, creating a JFrame as subframe is not recommended.
** Edit 1 **
Here a test code:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
public class JDialogResizetest extends JFrame {
private JButton openButton;
public JDialogResizetest() {
openButton = new JButton();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new FlowLayout());
openButton.setText("Open");
openButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
openButtonActionPerformed(evt);
}
});
getContentPane().add(openButton);
pack();
}
private void openButtonActionPerformed(ActionEvent evt) {
JDialog dialog = new JDialog(this, false);
dialog.getContentPane().add(new JTextPane());
dialog.setVisible(true);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JDialogResizetest().setVisible(true);
}
});
}
}
Here a picture of the desired behavior:

Categories