Create new thread for a new JFrame - java

I'm learning about threads and I've got a problem with it. I'm trying to make 2 frames, one is a main frame and another will be shown later after clicking on a button. I want to stop the main frame while the new frame is running. Can you guys help me with a very simple example for this? (And the new frame will be closed after clicking on a button too). Just 2 frames with a button on each are enough. Much appreciated!

You should avoid the use of multiple JFrames, use modal dialogs instead. JOptionPane offers a ton of good, easy & flexible methods to do so.
Here's an example. When you click the button the dialog will appear on top of the JFrame. The main JFrame won't be clickable anymore, since JOptionPane.showMessageDialog() produces a modal window.
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Example {
public Example() {
JFrame frame = new JFrame();
JButton button = new JButton("Click me");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(frame, "I'm a dialog!");
}
});
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Example();
}
});
}
}
Output:

Related

Creating a XYChart on button click

The purpose of the program is to display chart on click of the button with given parameters.
calcButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//some calculations ...
XYChart chart = QuickChart.getChart("Graph", "X", "Y", "y(x)", expression.xData, expression.yData);
new SwingWrapper(chart).displayChart();
}
});
However displayChart uses invokeAndWait, which cannot be executed inside ActionListener which makes sense. What is the proper way to solve this issue?
I am using swing with XChart library
You have to create the frame and its content panel by yourself, note that using multiple frames in an application is in general a bad idea, so consider a different design for your UI (e.g. tabbed pane).
Sample code, only displayChart(...) is relevant for you but I always prefer to create a runnable example:
package test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import org.knowm.xchart.QuickChart;
import org.knowm.xchart.XChartPanel;
import org.knowm.xchart.XYChart;
public class TestChart {
public static void main(String[] args) {
double[] xData=new double[100];
double[] yData=new double[100];
for (int i=0;i<100;i++) {
yData[i]=i+1;
xData[i]=Math.log(i+1);
}
XYChart chart = QuickChart.getChart("Graph", "X", "Y", "y(x)", xData, yData);
JFrame frame=new JFrame("Main");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton clickMe=new JButton("Click me!");
clickMe.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
displayChart(frame, chart);
}
});
frame.setContentPane(clickMe);
frame.pack();
frame.setVisible(true);
}
public static void displayChart(JFrame owner, XYChart chart) {
XChartPanel<XYChart> panel=new XChartPanel<XYChart>(chart);
JDialog d=new JDialog(owner, "Chart");
d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
d.setContentPane(panel);
d.pack();
d.setLocationRelativeTo(owner);
d.setVisible(true);
}
}

Launch two different JAVA projects from the same GUI

I have two java projects. Both with runnable GUIs.
I would like to add a button in Project1 that when pressed opens Project2 application window and closes Project 1.
Since I am using MVC for both applications it would become too cluttered to just copy paste all the code from the first project into the other one, which is why I am looking for an easier alternative.
I have built a path between the two projects, but I can't seem to find a way to start the second application on a button press.
i made you simple example of how to close JFrame after clicking a button and open new one.It is better if you write us the code you are useing note: i am not using MVC here
package test;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test {
private JButton btn;
JFrame frame = new JFrame();
public Test(){
btn = new JButton("new window");
frame.setVisible(true);
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(btn);
frame.setLayout(new FlowLayout());
btn.addActionListener(new addButtonWatcher());
}
private class addButtonWatcher implements ActionListener{
#Override
public void actionPerformed(ActionEvent e) {
JFrame newWindow = new JFrame();
newWindow.setVisible(true);
newWindow.setSize(200,200);
frame.dispose();
}
}
public static void main(String[] args) {
Test t = new Test();
}
}

java joptionpane which automatically closes after few seconds [duplicate]

This question already has an answer here:
Calling one JFrame from another using Timer without any buttons
(1 answer)
Closed 8 years ago.
Help needed for making a popup which automatically closes after a few seconds. JOptionpane messages usually needs an input to close, so is there any other way to handle auto-closing popup in java. Please help. Thanks in advance.
If the option pane can be modeless, this might be one way to do it:
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.JOptionPane;
import javax.swing.Timer;
public class AutoCloseJOption {
private static final int TIME_VISIBLE = 3000;
public static void main(String[] args) {
final JFrame frame1 = new JFrame("My App");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setSize(100, 100);
frame1.setLocation(100, 100);
JButton button = new JButton("My Button");
frame1.getContentPane().add(button);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane pane = new JOptionPane("Message", JOptionPane.INFORMATION_MESSAGE);
JDialog dialog = pane.createDialog(null, "Title");
dialog.setModal(false);
dialog.setVisible(true);
new Timer(TIME_VISIBLE, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
}
}).start();
}
});
frame1.setVisible(true);
}
}
For this example, press the button and an option dialog will be visible for three seconds.

JOptionPane showing outside JFrame with GraphicsDevice

package javaapplication1;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class JavaApplication1 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setTitle("Frame");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
device.setFullScreenWindow(frame);
device.setDisplayMode(new DisplayMode(800, 600, 32, 60));
frame.setVisible(true);
JButton btn = new JButton();
btn.setText("Button");
JPanel panel = new JPanel();
panel.add(btn);
frame.add(panel);
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "JOptionPane");
}
});
}
}
When I click the button, the application which is set to full screen will go to taskbar/minimized, so I need to click it first in the taskbar before seeing the JOptionPane that I triggered. What do you think is the problem with this? I'd like it to run smoothly without being minimized or going to taskbar. Looking forward for your answers. Thanks in advance. Or is there any other alternative to this?
That code works for me, though you might try this variant with 2 changes.
It creates and shows the GUI on the EDT.
It uses the content pane of the frame as the parent of the JOptionPane
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JavaApplication1 {
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
final JFrame frame = new JFrame();
frame.setTitle("Frame");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
device.setFullScreenWindow(frame);
device.setDisplayMode(new DisplayMode(800, 600, 32, 60));
frame.setVisible(true);
JButton btn = new JButton();
btn.setText("Button");
JPanel panel = new JPanel();
panel.add(btn);
frame.add(panel);
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//JOptionPane.showMessageDialog(frame, "JOptionPane");
JOptionPane.showMessageDialog(frame.getContentPane(), "JOptionPane");
}
});
}
};
SwingUtilities.invokeLater(r);
}
}
Update
When I add the following lines to the beginning of the source seen above..
System.out.println(System.getProperty("java.version"));
System.out.println(System.getProperty("java.vm.version"));
..the output & result is as follows.
Running in 1.7
Result: Failure as described in the question.
1.7.0_09
23.5-b02
Running in 1.6
Result: Success with no unusual artifacts or behavior.
1.6.0
1.6.0-b105
Analysis
Note that other results from comments suggest the behavior changed some time between that early 1.6 version, and 1.6.0_25. It seems like a regression bug. The OP should check the bug database & if nothing likely shows up, lodge a new report.
JOptionPane.showInternalMessageDialog(frame.getContentPane(), "JOptionPane");

Hiding JFrame with anonymous ActionListener in a JButton

I have a welcome (or menu) window (JFrame) with some buttons (JButton) for each possible action. Each of these should launch a new window and hide the welcome window. I know I can do it with setVisible(false);. But I can't make it work yet.
This is one example of code I have:
_startBtn.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
System.out.println("_startBtn pressed");
// Code to hide this JFrame and initialize another
}
My question is, how can I do it using a anonymous class like this one?
Thanks in advance!
I am posting an example for you i hope it will help you.
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class windows_test {
JFrame login = null;
JFrame inner_frame = null;
public windows_test() {
login = new JFrame();
login.setBounds(10, 10, 300, 300);
login.setLayout(new BorderLayout());
JButton button = new JButton("Login");
login.add(button, BorderLayout.CENTER);
login.setVisible(true);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
if (inner_frame == null) {
inner_frame = new JFrame();
}
inner_frame.setLayout(new FlowLayout(FlowLayout.CENTER));
inner_frame.add(new JButton("inner frame"));
inner_frame.setVisible(true);
login.setVisible(false);
inner_frame.setBounds(10, 10, 300, 300);
}
});
}
}
I will recommend you to use jpanel instead of jframes but you have asked for frames so i have created it with them. Hope it will help you ask if i am wrong somewhere or you are not able to understand.

Categories

Resources