GUI in java swings

I have this code which works properly on eclipse.The query I have is mentioned below

class clienttime_gui
{
    public static void main(String args[]) throws Exception
    {   
        JFrame frame=new JFrame("Add IP");
        JTextField textbox;
        JButton button;
        JLabel label;
        frame.setLayout(null);
        textbox = new JTextField();
        textbox.setBounds(100,20,150,20); 
        label = new JLabel("Add IP");
        label.setBounds(50, 20, 100, 20);
        button=new JButton("Submit");
        button.setBounds(250,20,100,20);
        frame.add(textbox);
        frame.add(label);
        frame.add(button);
        final String x=textbox.getText();
       frame.setSize(400,100);
        frame.setVisible(true);  
        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 1) {
                    try{

                    InetAddress locIP = InetAddress.getByName(x);
                    //InetAddress locIP = InetAddress.getByName("14.139.60.104");
                    Socket soc= new Socket(locIP,7681);
                    BufferedReader in=new BufferedReader(new InputStreamReader(soc.getInputStream()));
                    String y= null;
                    while((y = in.readLine()) != null){
                    System.out.println(y);
                    }
                    }catch(Exception e1){}
                }
            }
        });       
   }
}

This is the UI I have enter image description here

Right now I am getting the output in the eclipse console but I want to get it into the frame,How should I do that?

Upvotes: 0

Views: 148

Answers (4)

Leo Pflug
Leo Pflug

Reputation: 544

Add a JLabel that has no text at beginning and edit it everytime a new ip is submitted.

Upvotes: 1

subash
subash

Reputation: 3140

try this

class clienttime_gui {

    public static void main(String args[]) throws Exception {
        JFrame frame = new JFrame("Add IP");
        JTextField textbox;
        JButton button;
        JLabel label;
        final List list = new List();
        frame.setLayout(null);
        textbox = new JTextField();
        textbox.setBounds(100, 20, 150, 20);
        label = new JLabel("Add IP");
        label.setBounds(50, 20, 100, 20);
        button = new JButton("Submit");
        button.setBounds(250, 20, 100, 20);
        list.setBounds(50, 50, 350, 200);
        frame.add(textbox);
        frame.add(label);
        frame.add(button);
        final String x = textbox.getText();
        frame.setSize(400, 100);
        frame.setVisible(true);
        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 1) {
                    try {

                        InetAddress locIP = InetAddress.getByName(x);
                        //InetAddress locIP = InetAddress.getByName("14.139.60.104");
                        Socket soc = new Socket(locIP, 7681);
                        BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
                        String y = null;
                        while ((y = in.readLine()) != null) {
                            list.add(y);
                        }
                    } catch (Exception e1) {
                    }
                }
            }
        });
    }
}

Upvotes: 1

Masudul
Masudul

Reputation: 21961

Declare a JLabel and call setText, instead of System.out.println

 label.setText(y);

class clienttime_gui
{
static JLabel output=new JLabel();// Declare JLabel here.

public static void main(String args[]) throws Exception
{   
    JFrame frame=new JFrame("Add IP");
    JTextField textbox;
    JButton button;
    JLabel label;

    frame.setLayout(null);
    textbox = new JTextField();
    textbox.setBounds(100,20,150,20); 
    label = new JLabel("Add IP");
    label.setBounds(50, 20, 100, 20);
    button=new JButton("Submit");
    button.setBounds(250,20,100,20);
    frame.add(textbox);
    frame.add(label);
    frame.add(output);
    frame.add(button);
    final String x=textbox.getText();
   frame.setSize(400,100);
    frame.setVisible(true);  
    button.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 1) {
                try{

                InetAddress locIP = InetAddress.getByName(x);
                //InetAddress locIP = 
                         InetAddress.getByName("14.139.60.104");
                Socket soc= new Socket(locIP,7681);
                BufferedReader in=new BufferedReader(
                   new InputStreamReader(soc.getInputStream()));
                String y= null;
                while((y = in.readLine()) != null){
               //         System.out.println(y);
                  output.setText(y);
                }
                }catch(Exception e1){}
            }
        }
    });       
 }
 }

Upvotes: 1

tokhi
tokhi

Reputation: 21618

You can use setText("youString").. Try to learn the swing basics. here you can find a tutorial.

Upvotes: 1

Related Questions