Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Make a messageDialog Stay on top without a parent & to free memory

807589Oct 17 2008 — edited Oct 19 2008
Hi there got 3 questions.

How do i make a dialogbox stay on top of all other windows without a parent. Currently testing JOptionPane but if there any simpler way i could use em to.
JOptionPane.showMessageDialog(null,"success","server shutdown",JOptionPane.PLAIN_MESSAGE);
Note that the "null" is where the parent component should be.
I could create a JFrame set it to always on top and put a button into it but im looking for a "cleaner" way.

How do i free memory if lets say that I have a server that has a list of threads or something.
As the clients disconnect the threads has to be stopped and removed from memory. Ive solved the stop part, not very sure about the freeing memory part.

Or even more simplair like i have a code.
Int i=10;
free(i);
i<-----becomes null or worse.
In my server code theres 2 functions that are deprecetad. The clientlist.add(); & this.destroy(); (marked as : //deprecated). What should i replace them with?
//ColacX
//Version 1.0
//21:59 2008-10-17

import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;
import java.util.Vector;

public class Server implements Runnable{
    private int port;
    private ServerSocket serversocket;
    private Vector clientlist=new Vector();
    private boolean listening=true;
    
    private Serverthread serverthread;
    private Socket clientsocket;
    
    public Server(int port){
        this.port=port;
        
        try{
            serversocket=new ServerSocket(port);
            System.out.println("server: started on port:"+port);
            new Thread(this).start();
        }
        catch(IOException ex){
            System.out.println("server: couldnt start on port:"+port);
        }
    }
    
    public void run(){
        while(listening==true){
            try{
                System.out.println("server: waiting for client");
                
                clientsocket=serversocket.accept();
                System.out.println("server: "+clientsocket.toString());
                serverthread=new Serverthread(this,clientsocket);
                clientlist.add(serverthread); //Add() Deprecated
                System.out.println("server: client accepted");
            }
            catch(IOException ex){
                System.out.println(ex);
                System.out.println("server: couldnt accept client");
            }
        }
    }
    
    public void closeServer()throws IOException{
        listening=false;
        serversocket.close();
    }
    
    public static void main(String args[]){
        int port;
        if(args.length==0){
            port=1001;
        }
        else{
            port=Integer.parseInt(args[0]);
        }    

        Server s1=new Server(port);
        JOptionPane.showMessageDialog(null,"press ok to begin server shutdown","Server started on port:"+port,JOptionPane.PLAIN_MESSAGE);
        try{
            s1.closeServer();
            System.out.println("server shutdown success");
            JOptionPane.showMessageDialog(null,"success","server shutdown",JOptionPane.PLAIN_MESSAGE);
        }
        catch(java.io.IOException ex){
            System.out.println("server shutdown failure");
            JOptionPane.showMessageDialog(null,"failure","server shutdown",JOptionPane.PLAIN_MESSAGE);
        }
    }
}
//ColacX
//Version 1.0
//21:59 2008-10-17

import java.io.*;
import java.net.*;

public class Serverthread extends Thread{
    private Server server;
    private Socket clientsocket;
    private ObjectInputStream ois;
    private ObjectOutputStream oos;
    private boolean listening=true;

    public Serverthread(Server server,Socket clientsocket) {
        this.server=server;
        this.clientsocket=clientsocket;
        
        try{
            ois=new ObjectInputStream(clientsocket.getInputStream());
            oos=new ObjectOutputStream(clientsocket.getOutputStream());
            System.out.println("serverthread: iostreams created");
        }
        catch(IOException ex){
            System.out.println("serverthread: couldnt create iostreams");
        }
        this.start();
    }
    public void sendMessage(String message)throws IOException{
        oos.writeObject(message);
    }
    public String getMessage()throws ClassNotFoundException,IOException{
        return (String)ois.readObject();
    }
    public void DisconnectionProtocoll(){
        System.out.println("serverthread: disconnected"+clientsocket.toString());
        listening=false;
        this.destroy(); //Deprecated
    }
    public void run(){
        while(listening){
            try{
                System.out.println("serverthread: "+getMessage());
                sendMessage("serverthread: Server recieved message");
                listening=false;
            }
            catch(ClassNotFoundException ex){
                System.out.println("serverthread: "+ex+clientsocket.toString());
                DisconnectionProtocoll();
            }
            catch(IOException ex){
                DisconnectionProtocoll();
            }
        }
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 16 2008
Added on Oct 17 2008
9 comments
254 views