Results 1 to 8 of 8
  1. #1
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default [SOLVED] optionwindow

    Hello I have created an optionwindow for my chat program, but if i open the option window and click the cross in the right corner, not only the optionwindow terminates but also the chatprogram. Can someone tell what i can do about it, and are there other ways to make a kind of optionwindow?

    thanks keffie91

    this is the source:

    ChatProgram.java
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.border.*;
    import java.net.*;
    import java.io.*;
    public class ChatProgram extends JFrame implements ActionListener{
        JMenuBar mbar = new JMenuBar();
        JMenu options = new JMenu("Options");
        JMenuItem dcm = new JMenuItem("Disconnect");
        JMenuItem optm = new JMenuItem("Options");
        JPanel toppanel = new JPanel();    
        JButton disconnect = new JButton("Disconnect"); // disconnectbutton
        JTextField username = new JTextField("Anonymous"); // username
        JLabel usertxt = new JLabel("Username:"); // username label
        JTextArea mess = new JTextArea(); // display the messages
        JTextField write = new JTextField(); // write a new message
        MulticastSocket so; // MulticastSocket
        InetAddress iadr; // multicast adress
        int port; //port to listen to
            
        public ChatProgram(String groupAddr, int portNo) throws IOException{
            // The parameters
            iadr = InetAddress.getByName(groupAddr);
            port = portNo;
            
            // create socket and start communication
            so = new MulticastSocket(port);
            so.joinGroup(iadr);
            new Receiver(so, mess);
            sendMessage("Connected");
            
            setJMenuBar(mbar);
            mbar.add(options);
            options.add(optm);
            options.add(dcm);
            getContentPane().setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
            getContentPane().add(toppanel);
            getContentPane().add(disconnect);
            getContentPane().add(mess);
            getContentPane().add(write);
            
            toppanel.setLayout(new GridLayout(1,2));
            toppanel.add(usertxt);toppanel.add(username);
            
            mess.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
            mess.setEditable(false);
            
            write.addActionListener(this);
            disconnect.addActionListener(this);
            dcm.addActionListener(this);
            optm.addActionListener(this);
            setVisible(true);
            setResizable(false);
            setSize(500,400);
            setTitle("ChatProgram by Dennis Koole");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
        
        public void actionPerformed(ActionEvent e){
            if(e.getSource() == optm){
                // open a new OptionWindow
                new OptionWindow();
            }
            if(e.getSource() == write){
                // The user has written the message
                sendMessage(write.getText());
                write.setText("");
            }
            if(e.getSource() == disconnect || e.getSource() == dcm){
                // The user has pressed the disconnect button
                sendMessage("Disconnected");
                try{
                    so.leaveGroup(iadr);
                }
                catch(IOException ie){
                    mess.append("Cannot disconnect \n");
                }
                so.close();
                dispose();
                System.exit(0);
            }
        }
        
        private void sendMessage(String s){
            //convert the message to an array of bytes
            byte[] data = ( username.getText() + ": " + s).getBytes();
            //create and send the datagram packet
            DatagramPacket packet =
            new DatagramPacket(data, data.length, iadr, port);
            try{so.send(packet);}catch(IOException e){ mess.append("something went wrong");}
            
        }
        
        
        public static void main(String[] args) throws IOException {
            JFrame.setDefaultLookAndFeelDecorated(true);
            new ChatProgram("235.110.110.1",16645);
        }
    
    }
    
    class Receiver implements Runnable{
        Thread activity = new Thread(this);
        MulticastSocket so;
        JTextArea txt;
        
        //constructor
        Receiver(MulticastSocket sock, JTextArea txtAr){
            so = sock;
            txt = txtAr;
            activity.start();
        }
        public void run(){
            byte[] data = new byte[1024];
            while (true)
                try{
                    //create datagram for reception
                  DatagramPacket packet = new DatagramPacket(data, data.length);
                    
                  //wait for next message
                  so.receive(packet);
                  
                  // convert the message to a string
                  String mess = new String(data, 0, packet.getLength());
                      txt.append(mess + "\n");  // dusplay the message
                
                }
                catch(IOException e){
                    break; // occurs when so is closed
                }
        }
    }
    OptionWindow.java
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    public class OptionWindow extends JFrame {
        //JPanels to add the other components to
        JPanel p1 = new JPanel(); 
        JPanel p2 = new JPanel(); 
        JPanel p3 = new JPanel(); 
        JPanel p4 = new JPanel(); 
        JPanel p5 = new JPanel(); 
        public OptionWindow(){
            Container c = getContentPane();
            c.setLayout(new GridLayout(5,2));
            c.add(p1);c.add(p2);c.add(p3);c.add(p4);c.add(p5);
            
            
            setVisible(true);
            setSize(200,300);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
        public static void main(String[] args){
            new OptionWindow();
        }
    }
    Last edited by keffie91; 12-16-2008 at 04:49 PM.
    Never give up! ;)

  2. #2
    uncommon is offline Member
    Join Date
    Dec 2008
    Posts
    30
    Rep Power
    0

    Default

    Comment out line 18 from OptionWindow.java and it will work.

    Java Code:
            //setDefaultCloseOperation(EXIT_ON_CLOSE);

  3. #3
    uncommon is offline Member
    Join Date
    Dec 2008
    Posts
    30
    Rep Power
    0

    Default

    Oh you can also take out the main method from OptionWindow.java.

    It doesn't seem to have a purpose unless it's just for testing

  4. #4
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default

    But i have found another solution: JDialog :)

    What is the best solution you think?

    thanks

    keffie91
    Never give up! ;)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yes JDialog is fine here. Either this or a JOptionPane. Whatever you do, don't use a JFrame here.

  6. #6
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default

    Thanks

    keffie91
    Never give up! ;)

  7. #7
    uncommon is offline Member
    Join Date
    Dec 2008
    Posts
    30
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Yes JDialog is fine here. Either this or a JOptionPane. Whatever you do, don't use a JFrame here.
    why not JFrame?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    In my view, he's asking for a window with dialog behavior. A JDialog (IMO) is a better tool for the job than a JFrame.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •