0

I am trying to create a GUI for a client application. BufferedReader's .readLine() is acting as an input stream. The problem is that it prints newlines and carriage-returns perfectly in the console, but once I try to append it in a TextArea it just seems to rewrite the previous string. Here's the example code

 public class AirlinesClient extends JFrame implements ActionListener {
    public static BufferedReader socketIn = null;
    public static PrintWriter socketOut = null;
    public static Socket connection = null;
    public static String breakline;
    public static String command;
    public AirlinesClient() throws UnknownHostException, IOException{
        JPanel schPanel = new JPanel();
        JButton Schedules  = new JButton ("Flight Schedules");
        JButton Pilots  = new JButton ("Pilots' shifts");
//mainFrame
          super.setSize(300, 400);
          setLocationByPlatform(true);
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          setVisible(true);
//adding ScheduleButton's panel to the root frame
          schPanel.setLayout(null);
          getContentPane().add(schPanel);
//Adding Schedule button
          Schedules.setBounds(75, 10, 150, 50);
          Schedules.setVisible(true);
          schPanel.add(Schedules);
//Adding action on click
          Schedules.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {      
                  JTextArea area = new JTextArea(20,20);
                  JFrame scheduleFrame = new JFrame("Information");
                  scheduleFrame.setSize(800,400);
                  scheduleFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                  scheduleFrame.setLocationRelativeTo(null);
                  scheduleFrame.add(area);                               
                  scheduleFrame.setVisible(true);   
                  socketOut.flush();
                  socketOut.println("1");
                  area.append(breakline);

            }
        });

//Adding pilots' shifts       
          Pilots.setBounds(75, 70, 150, 50);
          Pilots.setVisible(true);
          schPanel.add(Pilots);

    }

    public static void main(String[] args) throws UnknownHostException, IOException {

        new AirlinesClient();

        int port = 1234;
        String host = "localhost";
        connection = new Socket(host,port);
        socketIn = new BufferedReader(new      InputStreamReader(connection.getInputStream()));
        socketOut = new PrintWriter(connection.getOutputStream(),true);

        try{    
        System.out.println("Successfully connected to the server!");
do{         
//This prints the output in the console and it ends when the server sends $$$
            while (!(breakline =socketIn.readLine()).equals("$$$")){
                System.out.println(breakline);
            }
//Command is input from the user keyboard but for testing purposes I'm only using
//a single socketOut command(from the actionlistener) to send commands      
        }while(command!="0");
        System.out.println("Closing connection to server!");
    }catch(IOException e){
        e.printStackTrace();
    } finally{
           try{
               if(socketIn!=null) socketIn.close();
               if(socketOut!=null) socketOut.close();
               if(connection!=null) connection.close();
             }
             catch(IOException e){
               System.err.println("Socket could not be closed!");
             }
           }

    }
    @Override
    public void actionPerformed(ActionEvent arg0) {

    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! .. – Andrew Thompson May 14 '15 at 01:57
  • .. 3) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson May 14 '15 at 01:57

0 Answers0