the following code
I want to press done buttone to close JFrame without exit the application

----------------------------------------------------------------------------------

package examples.SecretaryAgent;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Display extends JFrame
{
	private JCheckBox _first_Interval;
    private JCheckBox _second_Interval;
	private JButton _done_Button;
	
	
	
	public Display()
	{
		
		setLayout(new FlowLayout());
		_first_Interval=new JCheckBox("First Interval");
		_second_Interval=new JCheckBox("Second Interval");
		_done_Button=new JButton("Done");
		
		add(_first_Interval);
		add(_second_Interval);
		add(_done_Button);
		
		CheckBoxHandler handler=new CheckBoxHandler();
		ButtonHandler handler2 =new ButtonHandler();
		_first_Interval.addItemListener(handler);
		_second_Interval.addItemListener(handler);
		_done_Button.addActionListener(handler2);
		
	}
	private class CheckBoxHandler implements ItemListener 
	{
		public void itemStateChanged(ItemEvent e)
		{
			
			    			
			  
			  
			  
		}
	}
	private class ButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent event1)
		{
			if(event1.getSource()==_done_Button)
            {
				//SecretaryAgent._Dialoge=true;
				//System.out.println("At done buttone handling");
				//System.exit(0);
				//JFrame.CLOSE;
			   
			}			
		}
	
	}
}

package examples.SecretaryAgent;
import com.ibm.aglet.*;
import com.ibm.aglet.event.*;
import java.net.*;
import javax.swing.*;
public class SecretaryAgent extends Aglet
{
	public static boolean _Dialoge=false;
	private URL destination;
	String strDest=new String("atp://OMAR-PC:1024");
	boolean remote=false;
	
	public void onCreation(Object o)
	{
		
		addMobilityListener
		(
			new MobilityAdapter()
			{
				public void onDispatching(MobilityEvent e)
				{
					System.out.println("I am at the origin now");
					setText("I am at the origin");
					
					//waitMessage(2*1000);
					
				}
				public void onArrival(MobilityEvent e)
				{
					remote=true;
					setText("I am at the destination now");
					//setText("I am at Remote");
					Dialoge();
					setText("After Dialoge()");
					
				}
			}
	    );
	}
	
	public void Dialoge()
	{
		Display x=new Display();
		//x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		x.setSize(350,100);
		x.setVisible(true);		
	}
	public void run()
	{
		if(remote==false)
		{
			System.out.println("The original aglet runs here");
			try
			{
				destination=new URL (strDest);
				dispatch(destination);
			
			}
			catch (Exception e)
			{
				System.out.println(e.getMessage());
			}
		}
		else if(remote==true && SecretaryAgent._Dialoge==true)
		{
			System.out.println(" The aglets runs at"+destination);
			//waitMessage(5*1000);
			dispose();
		}
	}
}

Dani AI

Generated

Short summary tied to the thread: 's suggestion to hide the window is valid for keeping the JVM alive, and 's concern about "two different threads" is the right thing to watch for. The important additions are (1) which close action to use so the JVM is not terminated, and (2) how Swing must be created/changed on the Event Dispatch Thread (EDT) to avoid subtle bugs when an Aglet or other background thread interacts with the UI.

Create and show the frame on the EDT and pick a non-exiting close operation. For example:

SwingUtilities.invokeLater(new Runnable() {
  public void run() {
    Display d = new Display();
    d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    d.pack();
    d.setVisible(true);
  }
});

Key differences to keep in mind: hiding a frame (the approach noted by ) removes it from view but keeps its native resources and object graph; calling dispose() frees native resources and allows the window to be garbage-collected; EXIT_ON_CLOSE will terminate the JVM (avoid that when the application must keep running). All window state changes and calls to dispose() or setVisible(false) must occur on the EDT to be thread-safe.

When integrating with an Aglet thread, do not perform Swing construction or manipulation directly from agent lifecycle callbacks. Post UI work to the EDT (as above). If the agent needs a modal interaction, prefer a modal JDialog created on the EDT so the agent can wait for a result without racing the UI thread. For longer background work, use SwingWorker or other background threads and publish results back to the EDT to update the UI.

Troubleshooting tips: if the app appears to hang after closing the window, look for non-daemon background threads (agent threads), not a wrong close-operation; if events behave oddly, check that all Swing updates happen on the EDT.

Recommended Answers

All 3 Replies

First of all always use Code tags.
If you only want to hide your frame and dont want to close your application you can use frame.setVisible(false) in your done button ActionListener so that you application wont exit only the frame will be hidden

first of all I am sorry and next time I'll use code tag
secon thank you I used it and it work properly thank you

First of all always use Code tags.
If you only want to hide your frame and dont want to close your application you can use frame.setVisible(false) in your done button ActionListener so that you application wont exit only the frame will be hidden

first of all I am sorry and next time I'll use code tag
secon thank you I used it and it work properly thank you

I can use JFrame object inside the aglet class But inherited JFrame class means to have two different threads , But use only a JFrame object inside aglet thread may solve the threading problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.