Home > Software design >  Java swing jtextarea on a tab does not fill out the tab, cannot get it to fill even with scrollbar a
Java swing jtextarea on a tab does not fill out the tab, cannot get it to fill even with scrollbar a

Time:02-14

So I'm using the Oracle tabbed example to create a Java swing application to help retrieve quick data from an internal database for a very small company and I'm very new at Java (decent scripting, though). My problem is that retrieved data from the database goes outside tab boundariesenter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                // Replace this with your text retrieval process
                List<String> text = new ArrayList<String>(128);
                try (BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/resources/StarWarANewHope.txt")))) {
                    String line = null;
                    while ((line = br.readLine()) != null) {
                        text.add(line);
                    }
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }

                // Use this to format the results for the text area
                StringJoiner joiner = new StringJoiner("\n");
                for (String line : text) {
                    joiner.add(line);
                }

                JTabbedPane tabbedPane = new JTabbedPane();
                tabbedPane.addTab("Customers", null, new TestPane(joiner.toString()), "Displays customer details");

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(tabbedPane);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane(String text) {
            JTextArea filler = new JTextArea(text, 25, 25);
            filler.setWrapStyleWord(true);
            filler.setLineWrap(true);
            JScrollPane scroll = new JScrollPane(filler);
            //filler.setHorizontalAlignment(JTextArea.CENTER);
            setLayout(new BorderLayout());
            //panel.add(filler);
            add(scroll);
        }
    }

}

If you continue to have issues, consider providing a minimal reproducible example, it takes out the guess work and generally results in better answers for more details

  • Related