0

There's something that I don't understand. My code does not like JScrollBar apparently. I add it and I cannot scroll horizontally nor vertically.

Here's what it looks like:

Keep in mind that I'm new and I'm still working on it, so I'm sorry if it was something really obvious and easily avoidable.

public ChangeLog() {

    //Init.
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    JTextArea textarea = new JTextArea();
    JScrollPane scrollpane = new JScrollPane(textarea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    //Text Stuff
    textarea.setFont(textarea.getFont().deriveFont(16f));
    textarea.setText("Change Log: \n V1.0(A): Original encoder \n V1.0(B): Original decoder \n V1.1: Combination of both encoder and decoder \n V1.2: Added a heavier encoding & decoding system \n V1.3: Added an icon \n V1.4: Created an 'Info' page \n V1.5: Added a 'Change Log' page to the 'Info' page \n "
            + "V1.6: Removed the 'Change Log' \n V1.7: Added a 'Change Log' but was not implemented \n V1.8: Added a the 'Change Log' button \n V1.9: Added horizontal and vertical scroll bars to the 'Change Log'");
    textarea.setForeground(Color.BLACK);
    Dimension d = new Dimension(250, 275);
    textarea.setPreferredSize(d);

    //Other Stuff
    scrollpane.setViewportView(textarea);
    scrollpane.getPreferredSize();

    //Layout
    panel.setLayout(null);
    scrollpane.setBounds(new Rectangle(new Point(20, 20), scrollpane.getPreferredSize()));
    textarea.setBounds(new Rectangle(new Point(20, 23), textarea.getPreferredSize()));

    //Frame Stuff
    frame.setAlwaysOnTop(true);
    frame.setSize(300, 350);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setVisible(true);
    frame.setResizable(false);

    //Panel Stuff
    frame.add(panel);
    panel.setSize(frame.getSize());
    panel.setBackground(Color.BLUE);
    panel.add(textarea);
    panel.add(scrollpane);
} }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Attaxika
  • 58
  • 8
  • you don't need to add the `textarea ` only add the `scrollpane` see this [how add scrollPane in text Area](https://stackoverflow.com/a/8849094/5855946) – Ganesh Patel Aug 08 '17 at 04:54
  • 1
    1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Aug 08 '17 at 07:09
  • .. 3) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) 4) It would probably work better (for the user) if the lines of the text area were instead displayed as entries in a `JList`. 5) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 6) *"Keep in mind that I'm new"* That code snippet has so many bad practices that I stopped counting them. ... – Andrew Thompson Aug 08 '17 at 07:15
  • .. it seems the code was constructed from random bits of code found lying around the interwebz, without ever trying to understand what each code line does, or how to use them properly. The kind of information required for that is available in the [Java API Docs](https://docs.oracle.com/javase/8/docs/api/) (latest are best) and *more importantly* the [official tutorials](https://docs.oracle.com/javase/tutorial/uiswing/index.html) on Swing GUIs. Bookmark both those links and use them as the first source of information. – Andrew Thompson Aug 08 '17 at 07:21
  • @AndrewThompson I understand what I wrote and how it works partially, it wasn't just taken from random places off the internet, I read how other users did it and I wanted to try it out as I thought it would be fun. Admittedly where I got my information was not the best source obviously seeing from what everyone here is telling me, but stuff like "That code snippet had so many bad practices that I stopped counting them" doesn't mean anything to me. I'm sorry if I somehow offended you that you had to read my code, but as I said, I'm a beginner, it was a fun project, and I'll start differently. – Attaxika Aug 09 '17 at 05:50
  • @AndrewThompson I'm not good at Java, so I apologize for my bad code. It was a short, fun project that I thought would teach me some things. I'll read up on the articles that you referenced, so thank you. – Attaxika Aug 09 '17 at 05:51
  • *"I'm sorry if I somehow offended you.."* No! And what I said was not intended to offend either, just warn and make a point. Comments are too short to go into detail on code (heck, it took 3 comments to cover the even the broad story of what I was trying to communicate!). *"I'll read up on the articles that you referenced"* Good stuff! They contain more good tips than comments can convey. :) – Andrew Thompson Aug 09 '17 at 06:00

2 Answers2

1
Dimension d = new Dimension(250, 275);
textarea.setPreferredSize(d);

Don't hardcode a size for the text area. The size of the text area will change dynamically as text is added/removed and scrollbars will appear/disappear as required.

JTextArea textarea = new JTextArea();

Don't create the text area with no parameters. Instead, when you create the text area use something like:

JTextArea textarea = new JTextArea(5, 20);

to suggest a default size of the text area. Then when you have more than 5 lines of text the scrollbar will appear.

So I'm a relatively new Java developer

Start by reading the Swing Tutorial for Swing basics. There is a section on How to Use Text Areas to get you started.

panel.setLayout(null);
scrollpane.setBounds(...)

Don't a null layout. Don't use setBounds(). Swing was designed to be used with layout managers. See the above tutorial for working examples.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Removing the dimension and the "textarea.setPreferredSize(d);" removed the scrollbars, the addition of the "5, 20" had no noticeable effect, but changing it to "5, 10" or "5, 5" showed this: https://gyazo.com/fe8c1842fe63948ecbd47bb3896122f1 – Attaxika Aug 08 '17 at 02:01
  • I already told you that "working" scrollbars will only appear when you add text. You have poorly structured code with all the null layout and setSize(..) and setBounds(...) statements. Get rid of them all. You also have over problems like making the frame visible before adding components to it. I don't have time to critique every line of code. Read the tutorial and download the working example and modify the example. Your code will be better structured and easier to learn from. – camickr Aug 08 '17 at 02:07
1

I have created a working solution. Made some changes also.

public TestClass() {

        //Init.
        JFrame frame = new JFrame();
        JPanel panel = new JPanel(new BorderLayout());
        JTextArea textarea = new JTextArea();
        JScrollPane scrollpane = new JScrollPane(textarea);
        panel.add(scrollpane, BorderLayout.CENTER);



        //Text Stuff
        textarea.setFont(textarea.getFont().deriveFont(16f));
        textarea.setText("Change Log: \n V1.0(A): Original encoder \n V1.0(B): Original decoder \n V1.1: Combination of both encoder and decoder \n V1.2: Added a heavier encoding & decoding system \n V1.3: Added an icon \n V1.4: Created an 'Info' page \n V1.5: Added a 'Change Log' page to the 'Info' page \n "
                + "V1.6: Removed the 'Change Log' \n V1.7: Added a 'Change Log' but was not implemented \n V1.8: Added a the 'Change Log' button \n V1.9: Added horizontal and vertical scroll bars to the 'Change Log'");
        textarea.setForeground(Color.BLACK);
        //Dimension d = new Dimension(250, 275);
        //textarea.setPreferredSize(d);


        //Other Stuff
        scrollpane.setViewportView(textarea);
        scrollpane.getPreferredSize();




        //Layout
        //scrollpane.setBounds(new Rectangle(new Point(20, 20), scrollpane.getPreferredSize()));
        //textarea.setBounds(new Rectangle(new Point(20, 23), textarea.getPreferredSize()));

        //Listeners



        //Frame Stuff
        frame.setAlwaysOnTop(true);
        frame.setSize(300, 350);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
        frame.setResizable(false);


        //Panel Stuff
        frame.add(panel);
        panel.setSize(frame.getSize());
        panel.setBackground(Color.BLUE);
        panel.add(scrollpane);
    }

Also when swing better works with the layout managers and null layout will leads to inconsistent look on different screen types.

Let me know if anything more required. And yes everybody starts from scratch. I am still learning. You will too get many things. Just keep the hunger of learning. :-)

Aman
  • 735
  • 1
  • 6
  • 19