The script doesn't terminate after closing a created plot with JMathPlot - java

Although I am a beginner in java, I am trying to plot some data I have using JMathPlot.
Every thing went well, but the script didn't terminate after I closed the window of the plot.
Could any body tells me how to force the script to terminate when I close the window of the plot.
I use a debian machine(32-bit), IDE = eclipse, java-version = 1,6
and here is the code I'm using for plotting:
Plot2DPanel plot = new Plot2DPanel();
plot.addLinePlot("test", x, y);
JFrame frame = new JFrame("A test panel");
frame.setContentPane(plot);
frame.setVisible(true);
Thank you in advance

You termainate the process when closing the frame, not the panel.
This this:
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Also see JFrame Exit on close Java

Sure!
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0)
}
});
What that does is, waits until the close button is pressed, then runs System.exit(0) which simply terminates your program.
Note: I wrote this on iPhone, so watch for syntax errors :)

Related

Java JFrame freezing

I have this simple java application using swing. However the moment I execute the program the frame will appear but I won't be able to click anywhere and the button only appears after a few seconds. I am very new to javas Swing library so I could be missing something, however from what I've read this code shouldn't be causing any issues.
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.FlowLayout;
import javax.swing.*;
public class Game {
public static void main(String[] args) {
JFrame frame = new JFrame("JFrame Example");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JLabel label = new JLabel("This is a label!");
JButton button = new JButton();
button.setText("Press me");
panel.add(label);
panel.add(button);
frame.add(panel);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
EDIT
So I reinstalled my jdk but discovered this: if my browser is running when I execute the above code it will hang and freeze, however if I close my browser it runs as expected. But I can't start my browser until I kill my application so it seems something is wrong with my install as these shouldn't be using the same resources?
EDIT
Solved my problem: had multiple versions of jdk and jre install so unstalled both and then installed latest versions and everything seems to be working!
You need to create UI under UI dispatching thread:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// code to create UI or call to function that creates the UI
}
});
Java 8 style:
SwingUtilities.invokeLater(()->{
// Your UI code
});
If you don't use that idiom then the UI can't be partially created in inconsistent state, see Creating UI in swing:
almost all code that creates or interacts with Swing components must
run on the event dispatch thread
You can also use invokeAndWait is you need to block until the UI is created.

Why is Java crashing on even the simplest Swing program?

For seemingly no reason, Java has started crashing whenever I try to run anything with Swing. If I run the HelloWorld program, for example, just to test things, it'll run perfectly fine. But if I try to run a program with a JFrame, no matter how simple (I even tried the JFrame demo example on Java's website just so I could get a bare-bones JFrame program), I always get the "Java SE Platform Binary has stopped working" error.
Could this be related to a graphics driver update? I'd think not but you never know.
This is the demo I tried:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FrameTest {
public static void main(String[] args) {
JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel emptyLabel = new JLabel("");
emptyLabel.setPreferredSize(new Dimension(175, 100));
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
You should always use SwingUtilities.invokeLater() when dealing with Swing from main() or from any thread that isn't EDT.
https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
Still I am not allowed to post a comment. That is why I answer even for simple things.
Go to Environment variables.
Under the System Variables, click on new.
In the variable name, enter _JAVA_OPTIONS
In the Variable Value, enter -Xmx256M
Press OK and Check if it works now.

Swing Application Window not responding on Mac

I'm trying to get a basic Swing Application to run on my Mac OS X 10.8.2 (Java version 1.6.0_37) machine, and every time I try to run it from Eclipse, the frame appears, but I can't interact with it.
I've tried to start from a basic, clean slate where I create a new Swing Application Window project in Eclipse (WindowBuilder->Swing Designer->Application Window). This generates the following skeleton code:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
public class Test {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test window = new Test();
window.frame.setVisible(true);
window.frame.pack();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Test() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnPress = new JButton("Press");
frame.getContentPane().add(btnPress, BorderLayout.CENTER);
}
}
Everything seems to be fine, but when I run this from Eclipse, the frame doesn't let me interact with any components (in my non-example code, there are buttons and tabs).
Also, in the console, I see things like:
2012-11-09 14:30:27.624 java[8107:707] [Java CocoaComponent compatibility mode]: Enabled
2012-11-09 14:30:27.626 java[8107:707] [Java CocoaComponent compatibility mode]: Setting timeout for SWT to 0.100000
Is there some Mac-specific setting that I have to change? (I'm using the latest default Mac JRE)
The program runs fine on my machine under OSX, but it could be the missing
window.frame.pack();
Have you tried this?
Test window = new Test();
window.frame.pack();
window.frame.setVisible(true);
I had the same problem when using the DJ Swing library in my application (it uses SWT). Interestingly, the problem occurred even though I didn't initialise DJ Swing explicitly. It now works because I have added DJ Swing initialisation:
public class SwingAppTest {
public static void main(String[] args) {
NativeInterface.open();
UIUtils.setPreferredLookAndFeel();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Example");
frame.getContentPane().setLayout(new BorderLayout());
frame.setPreferredSize(new Dimension(400, 200));
frame.setBounds(0,0,200,200);
frame.setTitle("blah");
JButton blah = new JButton("blah");
blah.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("button clicked");
}
});
frame.getContentPane().add(blah, BorderLayout.CENTER);
frame.setVisible(true);
}
});
NativeInterface.runEventPump();
}
}
Apple is no longer supporting Java in is operating systems after 10.6:
Apple not committing to Java support in Mac OS X 10.7
seems like missing or incompatible library files in the JRE.
that is weird. I tried your sample (no linux) and don't see any problem there.
So as Wayne mentioned, might be macos issue.
Btw, what is your java version you're using?
On the other hand problem might be in the code you didn't share with us => hard to guess :)
EDIT: OK, so it seems you're playing "guess what I have in my code" game with us :) As my assumption is that code not shown causes problem.
It reminds me of some of these Poiroit/Agatha Christie detective stories, where just a little detail might have significant impact on the reality.
This is the reason for my theory:
code shown uses Java swing library (import javax.swing. ...) + Awt (import java.awt. ...) - this combonation is a common use case, however
the error message you shared shows SWT library error (Setting timeout for SWT to 0.100000)
So to me it seems like you're mixing things that should never be mixed.
As Swing is java UI library that is completely OS independent (originally Sun made) that is built on top of Awt. However SWT is completely different java UI library which is kind of mix of native calls with java on it (originally IBM made).
Therefor I'd suggest to double check your code and make sure that if you use JFrame, the only library components you have there are from Swing (javax.swing....) / Awt (java.awt. ...).
I've been knocking my head against a wall for days trying to get this working and finally found the answer:
'It's solved now - it was just a case of removing swt.jar from the project dependencies.'
Hey presto!!!!
In my case, I was trying to code a game where I used jPanel. I needed to override the paint method and added pack(); to the main and it finally showed me what I was trying to draw.

Why my Swing based GUI application is not responding?

I am trying to create my first GUI application using (Java + Eclipse + Swing). This is my code:
import java.awt.*;
import javax.swing.*;
public class HelloWorldSwing extends JFrame {
JTextArea m_resultArea = new JTextArea(6, 30);
//====================================================== constructor
public HelloWorldSwing() {
//... Set initial text, scrolling, and border.
m_resultArea.setText("Enter more text to see scrollbars");
JScrollPane scrollingArea = new JScrollPane(m_resultArea);
scrollingArea.setBorder(BorderFactory.createEmptyBorder(10,5,10,5));
// Get the content pane, set layout, add to center
Container content = this.getContentPane();
content.setLayout(new BorderLayout());
content.add(scrollingArea, BorderLayout.CENTER);
this.pack();
}
//============================================================= main
public static void main(String[] args) {
JFrame win = new HelloWorldSwing();
win.setTitle("TextAreaDemo");
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setVisible(true);
}
}
The code was taken from here.
When I run the application from Eclipse the expected window appears (So, it's good. I see what I want to see). However, when I try to close the window or try to write something in the text area the program freezes. The OS writes me that program is not responding (I try it on Ubuntu).
Can anybody help me to find the reason of the problem?
Thank you in advance for any help.
I'm sure this doesn't have to do with the code, as others have found the code runs just fine on their machines - which points to a machine specific issue. From within Eclipse, make sure it is setup to use the expected JDK/JRE. However, before worrying about how Eclipse is handling your situation, I'd run things by hand first - especially since you've got a very simple class.
I would check to ensure that you're using the expected compiler and runtime. On Linux:
which javac
which java
If they're both what you expect, do the following:
javac HelloWorldSwing.java
java HelloWorldSwing
If you get a similar problem, then you know it's not the Eclipse configuration and it's something else. If you're not using the latest JDK, upgrade to the latest. If you're already at the latest, it could be a display driver. Do other JAVA swing programs work on this computer? I'm sure you could find some on the net, download an app already packaged as a jar and try running it.
did you try using the eventdispatcherthread to view the JFrame?
something like:
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
createAndViewJFrame();
}
});
}
public void createAndViewJFrame(){
JFrame win = new HelloWorldSwing();
win.setTitle("TextAreaDemo");
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setVisible(true);
}
then your frame would be shown by the swing dispatcher thread.
hope it helped, although im just guessing...
Update: as commenters pointed you i f**ed up the invokeLater() call. I just edited this post to correct that. Thanx go to yishai & willcodejavaforfood for pointing it out!
frank
You need to catch the exit event and
respond with a System.exit( 0 );
You should be able to find that in
most swing examples online.
wrong stuff... sorry... coffee... argh....

Creating an offscreen frame in Java (or: how to avoid a blank menu on a Mac, when all application windows are closed)?

I am making a Mac application, and I want my menu bar to look right.
Any Mac user knows the menu bar should be in the top screen menu. Setting apple.laf.useScreenMenuBar to true in the property list file gets rid of the in-frame menu bars and moves the menu bar of the current focused window to the screen menu.
However, when all windows are hidden or when there are no windows, there are no menu bars to move to the top, and you just get a blank menu. I heard a solution to this was to create an offscreen window that is focused when no others are. The only purpose of it would be its menu, so that it could fill in when the others are gone.
However, I've been getting loads of problems. I can't seem to move the window off the screen because Macs won't let you set the coordinates to something past the size of the screen; it just cuts it off and positions it at the edge instead. Is there something else I have to do to make an offscreen window?
You should definitely consider WizardOfOdds' very helpful answer. Using "The Application Menu" correctly will help, and it's easy to set up a minimal Info.plist to get started. A persistent File menu will allow your application to open a new window when others are closed. This answer links to a simple example.
Although Apple's Human Interface Guidelines are an excellent guide to what your users will expect, you can certainly experiment with the approach you suggested in your question. In particular, you might try setLocation(Short.MIN_VALUE, Short.MIN_VALUE) on the invisible window. In addition, you might want to respond to a WindowEvent in some special way if it signals the close of the last visible window.
Addendum: When your listener sees the last visible window close, create a new, empty application window. Alternatively, move the invisible window onscreen and make it visible until the user decides how to proceed.
Addendum: Mac OS X helpfully prevents a visible window form being moved offscreen, but it's easy to put an invisible window in limbo, as shown below.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
public class FrameTest extends JFrame {
private static FrameTest marco;
private static FrameTest polo;
private static class MyPanel extends JPanel {
public MyPanel() {
super(true);
final JToggleButton b = new JToggleButton("Test");
b.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent e) {
if (b.isSelected()) {
polo.setLocation(100, 100);
polo.setVisible(true);
}
else {
polo.setVisible(false);
polo.setLocation(Short.MIN_VALUE, Short.MIN_VALUE);
}
}
});
this.add(b);
}
}
public FrameTest(String title) {
super(title);
this.setLayout(new BorderLayout());
this.add(new MyPanel());
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
marco = new FrameTest("Marco");
marco.setLocationRelativeTo(null);
marco.setVisible(true);
polo = new FrameTest("Polo");
polo.setLocation(Short.MIN_VALUE, Short.MIN_VALUE);
}
});
}
}
I know that this post is quite old, anyway, I had the same problem and found the solution. Actually it's quite simple. Just don't add the JMenuBar to your main frame when running on mac os x, but to your application using
com.apple.eawt.Application.getApplication().setDefaultMenuBar(menuBar);
Now the MenuBar is still displayed even if you set all frame's visibility to false.
Not a direct solution, but I think some create a 1-pixel window instead. That yields complaints though, like one described at Super User: Chaotic behavior of a dead pixel on my iMac 24"...
First a note: your question seems really to be "How to have a Window menu following the Apple Human Interface Guidelines" and not "creating an offscreen frame in Java", which seems like a monstrous hack.
I suggest checking Apple's "Mac OS X Integration for Java", which, under "Window menu", shows apparently exactly what you're trying to achieve:
Apple Human Interface Guidelines suggests that all Mac OS X applications should provide a Window menu to keep track of all currently open windows. A Window menu should contain a list of windows, with a checkmark next to the active window.
This is a comment to trashgod's answer. It's too big so I have to move it here >.<
More problems!
I'm getting 2 errors:
line 23: The method itemStateChanged(ItemEvent) of type new ItemListener(){} must override a superclass method
line 50:The method run() of type new Runnable(){} must override a superclass method
Any help? I've never encountered this before. I don't know what it means.
EDIT: I have more problems! I need the offscreen window to be visible while it is offscreen for it to produce a menu to move to the screen menu bar. When I use the code
offScreen = new JFrame("WTF?! You can see me?");
offScreen.setSize(400,300);
offScreen.setLocation(0, java.awt.Toolkit.getDefaultToolkit().getScreenSize().height+50);
System.out.println(offScreen.getLocation());
offScreen.setVisible(true);
System.out.println(offScreen.getLocation());
I get the output:
java.awt.Point[x=0,y=1100]
java.awt.Point[x=0,y=961]
It moves it back once it's made visible again!
I've searched for ages and I can't find anything.
This code works in Java 7:
if( isMac ) {
//This creates an invisible frame so that we always have a menu bar visible
JFrame menuFrame = new JFrame();
menuFrame.setUndecorated( true );
menuFrame.setJMenuBar( defaultMenuBar );
AWTUtilities.setWindowOpaque( menuFrame, false );
menuFrame.setBounds( 0,0,1,1 );
menuFrame.setVisible( true );
}
Just call this before you open any other windows, and it will stay in the background and automatically become the focused window when others are closed. You can still use the com.apple.eawt.Application.getApplication().setDefaultMenuBar(menuBar) method in your application so that you don't need to call setJMenuBar() on each JFrame.

Categories