Hi everyone, I'm trying to create a program that you can use to communicate to other people with the same program. It connects to a Server program that gets your message an send it to everyone. I found 2 problems that I haven't been able to solve.
The first one is that,in NetBeans, the GUI looks OK, but when you run the program outside it, the GUI becomes horrible. (Screenshots are on the attachments)
The other, more important problem, is that I seem to be only able to connect to localhost, even if I put my IP (I looked it on whatismyip.com , is that okay? ), it apparently connects, but it doesn't work. What should I do?
If you need any more info, just ask.

Thanks in advance.

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.event.*;

/**
 *
 * @author Yan Couto
 */
public class AMB {

    JFrame f;
    JPanel p;
    JButton send;
    JTextArea log,text;
    JOptionPane opt;
    Tell tell;
    Thread listen;
    getMessage g = new getMessage();

    public static void main(String[] args) {
        new AMB();
    }

    public AMB () {
        GUI();
        String name = JOptionPane.showInputDialog("Enter your name:");

        try {
       Socket s = getSocket(4567);
       PrintWriter out = new PrintWriter(s.getOutputStream(),true);
       Scanner in = new Scanner(s.getInputStream());;
       listen = new Thread(new Listen(in, name));
       listen.start();
       tell = new Tell(out, name);
       }
       catch (Exception e ) {
           System.out.println("Oh fuck!\n");
       }
    }

    private void GUI ()
    {
        f = new JFrame ();
        f.setTitle("Amebensseger");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        p = new JPanel (new GridBagLayout());
        p.setSize(300,300);

        log = new JTextArea(20,50);
        log.setText("Welcome to AMB!\n");
        log.append(" :)\n");
        log.setEditable(false);
        log.setLineWrap(true);
        log.setWrapStyleWord(true);
        JScrollPane logscroll = new JScrollPane(log,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        setItem(p,logscroll,0,0,4,3,new Insets(2,2,3,2),GridBagConstraints.NORTHWEST,GridBagConstraints.BOTH);


        text = new JTextArea(8,40);
        text.setLineWrap(true);
        text.setWrapStyleWord(true);
        text.addKeyListener(g);
        setItem(p,text,0,3,3,1,new Insets(2,2,2,2),GridBagConstraints.SOUTH,GridBagConstraints.NONE);

        send = new JButton("Send");
        send.addActionListener(g);
        setItem(p,send,3,3,1,1,new Insets(1,1,1,1),GridBagConstraints.CENTER,GridBagConstraints.BOTH);

        f.add(p);
        f.setSize(570,480);
        //f.pack();
        f.setVisible(true);
    }

    private void setItem (JPanel p, JComponent c, int x, int y,int width, int height, Insets insets, int align,int fill )
    {
        GridBagConstraints gc = new GridBagConstraints ();
        gc.gridx = x;
        gc.gridy = y;
        gc.gridwidth = width;
        gc.gridheight = height;
        gc.insets = insets;
        gc.anchor = align;
        gc.fill = fill;
        p.add(c,gc);
    }

    private void setItem(JPanel p, JComponent c, int x, int y,int width, int height) {
        setItem(p,c,x,y,width,height,new Insets(0,0,0,0),GridBagConstraints.CENTER,GridBagConstraints.NONE);
    }
    private void setItem(JPanel p, JComponent c, int x, int y,int width, int height,int fill) {
        setItem(p,c,x,y,width,height,new Insets(0,0,0,0),GridBagConstraints.CENTER,fill);
    }

    private Socket getSocket(int port)
    {
       Socket s;
       String host;
       InetAddress ip;

       while(true)
       {
           host = JOptionPane.showInputDialog("What server do you want to connect to?");


           try{
               ip = InetAddress.getByName(host);
               System.out.println(ip);
               System.out.println(ip.getCanonicalHostName());
               System.out.println(ip.isReachable(1000));
               s = new Socket(ip, port);
               return s;
           }
           catch (UnknownHostException e){
               System.out.println("The host is unknown");
           }
           catch (IOException e){
               System.out.println("Network error");
           }
       }
   }


    public class Listen implements Runnable {
       Scanner in;
       String name;
      public Listen (Scanner in,String name)
       {
           this.in = in;
           this.name = name;
       }
      public void run ()
         {
             while(true)
             {
                 String n = in.nextLine();
                 String s = in.nextLine();
                 log.append(n + ":" + s + "\n");

             }

         }
}

    class Tell {

        PrintWriter out;
        String name;
       

       public Tell (PrintWriter out, String name)
       {
           this.out = out;
           this.name = name;
       }


       public void sendText(String s) {
               out.println(name);
               out.println(s);
               out.flush();
        }

}

    public class getMessage implements ActionListener, KeyListener {

        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == send) {
                String aux = text.getText();
                text.setText(null);
                tell.sendText(aux);
                text.requestFocus();
            }
        }

        public void keyTyped(KeyEvent e) {}

       
        public void keyPressed(KeyEvent e) {
            if(e.getKeyCode() == e.VK_ENTER) {
                send.doClick();
                try {
                Robot r = new Robot();
                r.keyPress(e.VK_BACK_SPACE);
                } catch (Exception ex) {}
                }
                } 
        
        public void keyReleased(KeyEvent e) {}
    }
     
                

}

Second problem sounds like a firewall problem and/or a router problem blocking and/or not forwarding inbound traffic.

pack

log.append(n + ":" + s + "\n");

into invokeLater method

pack

log.append(n + ":" + s + "\n");

into invokeLater method

Like this?

SwingUtilities.invokeLater(new Runnable () { public void run() { log.append(n + ":" + s + "\n"); } });

Because it didn't seem to work, or did I do it wrong?


Thanks! :)

Second problem sounds like a firewall problem and/or a router problem blocking and/or not forwarding inbound traffic.

Do you know how I can fix this?

Thanks! :)

firewall problem and/or a router problem ... Do you know how I can fix this?

Sorry man, that depends on exactly what hardware & software you are running in exactly what configuration. I'm not best person to ask. There are loads of excellent sites on the web dealing with network config issues like this - it shouldn't be hard to find some relevant instructions.
J

put for JTextArea setPrefferedSize for GridBag


..............

this is excelent example hoe the LayoutManager works...

JFrame - JPanel (BorderLayput(5,5)) // 5 create a gap between JComponents

put JTextArea to JPanel (without size, prefferedSize, no sizing for Center JCompoents, that is automatically)

myPanel.add(myTextArea, BorederLayout.CENTER);

create new JPanel with proper preferredSize

put here 2nd. JTextArea same as 1st. JtestArea,

set PreferredSize for JButton and put that to EAST or WEST

play with Preffered size for nice GUI outPut

then each JTextArea put into JSchrollPane


...............


why did you leave pack() Methods

f.setPreferredSize(new Dimension(570,480));
f.pack();
f.setVisible(true);

I was able to solve the GUI problem by adding the JTextArea that you can write on to a JScrollPane and then adding it to the panel.
But there's another problem. The only way I found to delete the new line that you create when you press Enter to sen the message was to create a Robot that presses backspace for you. But now, I don't know why, When you close the program, it starts deleting text if you are in some text editor. I can't see why this happen, because the Robot is only called when you press enter, an only once. Is there a way to solve it?

Thanks in avance.

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.