0
public static void main(String[] args) throws IOException {
    //cevir("ornek3.txt");
    JFrame frame=new JFrame("Print");
    JPanel input=new JPanel();
    JPanel output=new JPanel(); output.setBackground(Color.black);
    final JTextArea ita = new JTextArea(30, 40);
    JScrollPane ijp = new JScrollPane(ita);
    JTextArea ota = new JTextArea(30, 40);
    JScrollPane ojp = new JScrollPane(ota);
    JButton buton=new JButton("Print");

    frame.setLayout(new FlowLayout());
    buton.setSize(50, 20);
    input.setBounds(0,0,500, 500);
    output.setBounds(500, 0, 500, 450);
    frame.setBounds(100, 50, 1000, 500);


    input.add(ijp, BorderLayout.CENTER);
    output.add(ojp, BorderLayout.EAST);
    input.add(buton, BorderLayout.SOUTH);
    frame.add(input);
    frame.add(output);

    buton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            for(String line: ita.getText().split("\\n"));
                System.out.println(line);


        }
    });


    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

This is my code and i want to get the text that i wrote while the program running and print it to the console. Is it possible with JtextArea. This code prints null when i clicked the button to the console even if i write something to the textarea.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
How_To
  • 27
  • 2
  • 10
  • Your code does not compile. Why did you put a `;` in `for(String line: ita.getText().split("\\n"));` ? Is it possible you have a static variable named `line` that gets printed instead of the actual content of the `JTextArea` ? – ortis Sep 26 '14 at 14:54
  • 2
    Other than the bug @ortis mentions, your code works fine. Note that I'd avoid using null layouts and setbounds, would favor use of layout managers and would avoid putting everything in the static domain, but that can be fixed later. – Hovercraft Full Of Eels Sep 26 '14 at 14:57
  • Thanks a lot and sorry for such a simple question. @HovercraftFullOfEels what can i use instead of setbounds? I'm new at Java and i think my codes are weak. – How_To Sep 26 '14 at 15:11
  • To know how to lay out your GUI, we would need to know more about your desired GUI structure. Also what is the purpose of the 2nd JTextArea, the one for output? – Hovercraft Full Of Eels Sep 26 '14 at 21:42
  • There will be some changes on the input text that we copy or write there, and the changed text will be written the output area. This is an uncompleted code. – How_To Sep 28 '14 at 09:22

1 Answers1

1

You have use the JtextArea#append method.

        public void actionPerformed(ActionEvent e) {

            for(String line: ita.getText().split("\\n"))
               ota.append(line);


        }

Also variables used inside the methods inner class should be final, so make ota as final

    final JTextArea ota = new JTextArea(30, 40);
ares
  • 4,283
  • 6
  • 32
  • 63