-1

This code's purpose [Open Java file, than content of that Java file will display at new Tab] I also try to make compile func but I need to solve this problem at here first.

My problem is when I try to open new Java file, then last content of Java file I opened accumulate to new content of Java file. I don't know why, is there a way to solve this?

package term_project_GUI;

import javax.swing.*;
import javax.swing.filechooser.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class JavaIDE_UI extends JFrame {
    Container contentPane;

    JTabbedPane tp;
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JTextArea ta = new JTextArea(15, 30);
    JScrollPane sp = new JScrollPane(ta);

    JavaIDE_UI() {
        setTitle("JavaIDE");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        createMenu();

        contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());
        tp = new JTabbedPane();
        contentPane.add(tp, BorderLayout.NORTH);
        contentPane.add(p2, BorderLayout.SOUTH);

        setSize(400, 500);
        setVisible(true);
    }

    void createMenu() {
        JMenuBar mb = new JMenuBar();
        JMenu File = new JMenu("File");
        JMenu Run = new JMenu("Run");
        JMenuItem Open = new JMenuItem("Open");
        JMenuItem Close = new JMenuItem("Close");
        JMenuItem Save = new JMenuItem("Save");
        JMenuItem SaveAs = new JMenuItem("Save As");
        JMenuItem Quit = new JMenuItem("Quit");
        JMenuItem Compile = new JMenuItem("Compile");

    Open.addActionListener(new OpenActionListener_open());

    File.add(Open);
    File.add(Close);
    File.add(Save);
    File.add(SaveAs);
    File.add(Quit);

    Run.add(Compile);

    mb.add(File);
    mb.add(Run);

    setJMenuBar(mb);
}

class OpenActionListener_open implements ActionListener {
    JFileChooser chooser;
    JTextArea ta = new JTextArea(15, 30);

    OpenActionListener_open() {
        chooser = new JFileChooser();
    }

    public void actionPerformed(ActionEvent e) {
        FileNameExtensionFilter filter = new FileNameExtensionFilter(".java files", "java");
        chooser.setFileFilter(filter);

        int ret = chooser.showOpenDialog(null);
        if (ret != JFileChooser.APPROVE_OPTION) {
            JOptionPane.showMessageDialog(null, "File Unchoose.", "Warning", JOptionPane.WARNING_MESSAGE);
            return;
        }


        String fileName = chooser.getSelectedFile().getName();
        tp.addTab(fileName, new JScrollPane(ta));

        String filePath = chooser.getSelectedFile().getPath();
        try {
            BufferedReader reader = new BufferedReader(new FileReader(filePath));
            String data = "";
            while ((data = reader.readLine()) != null) {
                ta.append(data+"\n");
            }
            reader.close();
        } catch (IOException e1) {
        }


    }
}

public static void main(String[] args) {
    new JavaIDE_UI();
}
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Taker829
  • 1
  • 1

1 Answers1

1

You're creating two instance of JTextArea, one is an instance field of JavaIDE_UI and the other is an instance field of OpenActionListener_open.

When the actionPerformed method is called, it's appending the text to the existing JTextArea (of the OpenActionListener_open), a process which will also remove it from it's current tab/scroll pane and move it to the new one you've creating

So, instead, create a new JTextArea for each tab...

class OpenActionListener_open implements ActionListener {

    JFileChooser chooser;

    OpenActionListener_open() {
        chooser = new JFileChooser();
    }

    public void actionPerformed(ActionEvent e) {
        FileNameExtensionFilter filter = new FileNameExtensionFilter(".java files", "java");
        chooser.setFileFilter(filter);

        int ret = chooser.showOpenDialog(null);
        if (ret != JFileChooser.APPROVE_OPTION) {
            JOptionPane.showMessageDialog(null, "File Unchoose.", "Warning", JOptionPane.WARNING_MESSAGE);
            return;
        }

        String fileName = chooser.getSelectedFile().getName();
        JTextArea ta = new JTextArea();
        JScrollPane sp = new JScrollPane(ta);
        tp.addTab(fileName, sp);

        String filePath = chooser.getSelectedFile().getPath();
        try {
            BufferedReader reader = new BufferedReader(new FileReader(filePath));
            String data = "";
            while ((data = reader.readLine()) != null) {
                ta.append(data + "\n");
            }
            reader.close();
        } catch (IOException e1) {
        }
        tp.setSelectedComponent(sp);

    }
}

This example will also select the tab so you can see the newly opened file.

Also, I would change to contentPane.add(tp, BorderLayout.NORTH); contentPane.add(tp);, otherwise you're going to have some weird layout issues

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • so much thank you for helping me. my goal is to code JavaIDE using GUI. So at last, compile the java file I had printed at North pane. I set BorderLayout as North because of that. – Taker829 Dec 16 '17 at 07:33
  • I can't type enough how I'm appreciating you for this. Thank you very much – Taker829 Dec 16 '17 at 07:34
  • I had some weird layout issues when it was placed in the north position – MadProgrammer Dec 16 '17 at 07:34
  • Yeah, it's not weird you think as that. I'm sorry for forgotten explaining full issue. I thought what I wrote is enough for asking question about tab issue. – Taker829 Dec 16 '17 at 07:43