Reputation: 678
I am trying to write application which will create new window when pressing button, show current window count and close window and thread on closing the window.
So basically, functionality is like this (and some of them are working):
Here is my code:
package projectpackage;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
class MyMouseEvent extends MouseAdapter
{
public void mousePressed(MouseEvent me)
{
MyWindowThread.incrementMyWindowThreadCount();
MyWindowThread obj1 = new MyWindowThread();
Thread thr1 = new Thread(obj1);
thr1.start();
}
}
class MyWindowThread extends JFrame implements Runnable
{
private int height;
private int width;
private static int MyWindowThreadCount = 1;
public static int getMyWindowThreadCount()
{
return MyWindowThreadCount;
}
public static void incrementMyWindowThreadCount()
{
MyWindowThreadCount++;
}
public static void decrementMyWindowThreadCount()
{
MyWindowThreadCount--;
}
public MyWindowThread()
{
super("Frame "+getMyWindowThreadCount());
this.setHeight(300);
this.setWidth(400);
this.setBounds(100,100,getWidth(),getHeight());
this.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
MyWindowThread.decrementMyWindowThreadCount();
//Thread.currentThread().interrupt();
return;
}
}
);
JPanel panel1 = new JPanel();
JButton button1 = new JButton("New window");
JButton button2 = new JButton("Count running windows");
this.getContentPane().add(panel1);
panel1.add(button1);
panel1.add(button2);
button1.addMouseListener(new MyMouseEvent());
button2.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
javax.swing.JOptionPane.showMessageDialog(null,"Window count = " + MyWindowThread.getMyWindowThreadCount());
}
}
);
this.setVisible(true); // show frame
}
public int getHeight()
{
return this.height;
}
public void setHeight(int h)
{
this.height = h;
}
public int getWidth()
{
return this.width;
}
public void setWidth(int w)
{
this.width = w;
}
public void run()
{
}
}
public class MyApp
{
public static void main(String[] args)
{
// Creating objects START
MyWindowThread obj1 = new MyWindowThread();
Thread thr1 = new Thread(obj1);
thr1.start();
}
}
manifest.mf:
Manifest-Version: 1.0 Main-Class: projectpackage.MyApp
I hope you know how to compile and run Java application in console. In case you don't, here it is:
(navigate in CLI to directory with "projectpackage" directory inside. *.java file must be inside "projectpackage" directory)
javac projectpackage\*.java jar cfm MyApp.jar manifest.mf projectpackage\* java -jar MyApp.jar
Can anyone enlighten me what am I doing wrong or just what I need to do to make the code work? Or am I just having fundamental misconception?
Upvotes: 0
Views: 2174
Reputation: 5930
If you simply want your application to exit when the JFrame is closed, then you are going about this all wrong. Get rid of all the Thread objects and Runnables. Swing applications run in the event dispatch thread (EDT). You should only use background threads if you need to do something like disk or network I/O.
Create/show the JFrame in the EDT. If you set the default close operation on the JFrame to DISPOSE_ON_CLOSE, then when all of the windows have been closed, the main thread will terminate normally.
public class MyApp {
public static void main( String[] args ) {
EventQueue.invokeLater( new Runnable() {
public void run() {
MyWindow w = new MyWindow();
w.setVisible( true );
}
} );
}
private static class MyWindow extends JFrame {
// Simplified reference counting
private static AtomicInteger frameCount = new AtomicInteger( 0 );
public MyWindow() {
super("Frame " + frameCount.incrementAndGet() );
this.setSize( 400, 300 );
this.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
JButton button = new JButton("New Window");
button.addActionListener( new ActionListener() {
@Override
public void actionPerformed( ActionEvent e ) {
MyWindow w = new MyWindow();
w.setVisible( true );
}
} );
this.getContentPane().add( button );
// ignore reference count button for now
}
}
}
Upvotes: 1
Reputation: 6908
JFrame frame = new JFrame();
This is your problem, you extend the JFrame-class but try to use a new object.
You should replace this line by super();
or super("Frame " + getMyThreadCount());
if you want so.
Edit: Another issue to mention: you named your class MyThread, this is a quite irritating class-name for a window ;)
Edit 2: Few other things:
public void run()
has no content, so why does MyThread
implement Runnable
?Upvotes: 2