1
package info.lxgg;

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

public class accPage implements ActionListener{
    
    static private final String newLine = "\n";
    public static String dateiname = "daten/datei.txt";
    public static String UserID;

    JFrame frame = new JFrame("Infos");
    JButton save = new JButton();
    JButton removeAll = new JButton();
    
    static JTextArea openedText = new JTextArea();

    public accPage() {
        frame.setSize(500, 500);
        frame.getContentPane().setBackground(new Color(30, 25, 25));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setLayout(null);

        openedText.setLineWrap(true);
        openedText.setWrapStyleWord(true);
        openedText.setLayout(null);
        openedText.setBounds(0, 0, 485, 400);

        removeAll.setBounds(100, 410, 100, 40);
        removeAll.setFocusable(false);
        removeAll.setBorderPainted(false);
        removeAll.setText("REMOVE");
        removeAll.addActionListener(this);
        removeAll.setBackground(new Color(200, 200, 200));
        removeAll.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                removeAll.setBackground(new Color(150, 150, 150));
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                removeAll.setBackground(new Color(200, 200, 200));
            }
        });
        
        save.setBounds(10, 410, 80, 40);
        save.setFocusable(false);
        save.setBorderPainted(false);
        save.setText("SAVE");
        save.addActionListener(this);
        save.setBackground(new Color(200, 200, 200));
        save.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                save.setBackground(new Color(150, 150, 150));
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                save.setBackground(new Color(200, 200, 200));
            }
        });

        frame.add(removeAll);
        frame.add(save);
        frame.add(openedText);
        
        File file = new File("daten");
        File info = new File("daten/datei.txt");
        
        if(!file.exists()) {
            file.mkdirs();
        }
        
        if(!info.exists()) {
            try {
                info.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } 
        ladeDatei(dateiname);
    }
    
    public static void ladeDatei(String datName) {
        File file = new File(datName);

        if (!file.canRead() || !file.isFile())
            System.exit(0);

        FileReader fr = null;
        int c;
        StringBuffer buff = new StringBuffer();
        try {
            fr = new FileReader(file);
            while ((c = fr.read()) != -1) {
                buff.append((char) c);
            }
            fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        openedText.setText(buff.toString());
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == save) {
            try {
                FileWriter writer = new FileWriter(dateiname);
                writer.write(openedText.getText());
                writer.flush();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        if(e.getSource() == removeAll) {
            try {
                FileWriter writer = new FileWriter(dateiname);
                openedText.setText("");
                writer.write(openedText.getText());
                writer.flush();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
}

I want to add a JScrollBar to openedText but I can't do it. Can someone please help me because I've been sitting at it for several hours and can't get it.

I've already tried it with JScrollPane and JScrollBar without success, so I would find it nice if someone can help.

2
  • You've tried: adding openedText to a new JScrollPane and adding the scroll pane to the frame?
    – cwittah
    Apr 13, 2021 at 14:47
  • 2
    Don't use a null layout. Don't use setBounds(...). Swing was designed to be used with layout managers and scrollbars will only work when layout managers are used. See: stackoverflow.com/questions/5880169/…
    – camickr
    Apr 13, 2021 at 15:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.