0

I have again a problem with the scrollpane in Java. I just want to combine a JTextArea with a ScrollPane. I did it as usually, but this time it isn't working.

Here is the part of code which matters (please let me know, if you need more):

rightWindow.add(rightWindowMaximumWidth, BorderLayout.CENTER); //does it matter if I call this at the beginning or at the end ?
...
rightUpTextArea         = new JTextArea();
rightUpScrollPane       = new JScrollPane(rightUpTextArea);
...
rightWindowMaximumWidth.add(rightUpScrollPane, "Up"); //Jpanel

I filled the TextArea with a lot of text, but there isn't a scrollpane. Maybe it is obvious, but I can't find the mistake.

Edit: here ist the complete code (the code frome abouve is in the methode setupRight() and setupRightOben()):

private void setupRightSide(){
    //rightWindow
    rightWindow             = new JPanel(new BorderLayout(10,10));
    rightWindowMaximumWidth = new JPanel();
    rightWindowSplit        = new BoxLayout(rightWindowMaximumWidth, BoxLayout.Y_AXIS);//"rightWindow" is the parent, not "windowContainer"!!!

    rightUp                 = new JPanel();
    rightUpTextArea         = new JTextArea();
    rightUpScrollPane       = new JScrollPane(rightUpTextArea);

    rightDownBoxPanel       = new JPanel();
    rightDownBox            = new BoxLayout(rightDownBoxPanel, BoxLayout.X_AXIS);//"rightDownBoxPanel" is the parent, not "rightWindow"!!!
    rightDownTextArea       = new JTextArea();
    rightDownTextAreaScrollPane = new JScrollPane(rightDownTextArea);
    rightDownButtomSend     = new JButton("Send");

    //rightUpScrollPane.setAutoscrolls(true);

    windowContainer.add(rightWindow, BorderLayout.CENTER);

    //rechte Seite
    rightWindow.setPreferredSize(new Dimension(250,250));
    rightWindow.setSize(new Dimension(424,503));  // habe ich durch datenausgabe mit der Konsole herrausgefundem
    rightWindow.add(rightWindowMaximumWidth, BorderLayout.CENTER);

    //container in "rechte Seite" mit maximaler width von 1000px
    rightWindowMaximumWidth.setLayout(rightWindowSplit);
    rightWindowMaximumWidth.setPreferredSize(new Dimension(rightWindow.getWidth()-10,1000));
    rightWindowMaximumWidth.setMaximumSize(new Dimension(1000,Integer.MAX_VALUE));

    rightWindowMaximumWidth.add(rightUpScrollPane, "Up");
    rightWindowMaximumWidth.add(Box.createRigidArea(new Dimension(0,10)));
    rightWindowMaximumWidth.add(rightDownBoxPanel, "Down");
    rightWindowMaximumWidth.add(Box.createRigidArea(new Dimension(0,10)));

    setupRechtsOben();
    setupRechtsUnten();

}

private void setupRechtsOben(){
    //obere Teil der rechten Seite
    rightUp.setPreferredSize(new Dimension(420 , 400));
    rightUp.setMaximumSize(new Dimension(420,400));
   // rightUp.add(rightUpScrollPane);

    //TextFeld rechts-oben
    rightUpTextArea.setBorder( new CompoundBorder(new javax.swing.border.LineBorder(Color.BLACK), new EmptyBorder(15, 15, 15, 15)));
    rightUpTextArea.setEditable(false);
    rightUpTextArea.setLineWrap(true);
    rightUpTextArea.setWrapStyleWord(true);
    rightUpTextArea.setBackground(Color.lightGray);
    rightUpTextArea.setPreferredSize(new Dimension(420,350));//-2 damit der rand zu sehen ist
    rightUpTextArea.setMaximumSize(rightWindowMaximumWidth.getMaximumSize());
    rightUpTextArea.setText("d\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\n"); //to fill it with text


}

private void setupRechtsUnten(){
    //untere Teil der rechten Seite (vertikales BoyLayout)
    rightDownBoxPanel.setLayout(rightDownBox);
    rightDownBoxPanel.add(rightDownTextAreaScrollPane, "TextField");
    rightDownBoxPanel.add(Box.createRigidArea(new Dimension(10,0)));
    rightDownBoxPanel.add(rightDownButtomSend, "SendButton");
    rightDownBoxPanel.setMaximumSize(new Dimension(1000,100));
    rightDownBoxPanel.setPreferredSize(new Dimension(1000 ,100));


    //TextEingabe im BoxLayout
    rightDownTextArea.setBorder(new javax.swing.border.LineBorder(Color.BLACK));
    //Damit es in der nächsten Zeile weitergeht, wenn diese voll ist
    rightDownTextArea.setLineWrap(true);
    rightDownTextArea.setWrapStyleWord(true);

    //SendButton im BoxLayout
    rightDownButtomSend.setSize(new Dimension(100, 50));
    rightDownButtomSend.setPreferredSize(new Dimension(100, 50));
    rightDownButtomSend.setMaximumSize(new Dimension(100, 50));
    rightDownButtomSend.setFont(new Font("Arial", Font.BOLD, 14));
    rightDownButtomSend.setAlignmentX(frame.CENTER_ALIGNMENT);

}
Robert
  • 7
  • 6
  • 1
    I think you can't find it because you are looking here. It doesn't look like this code contains the mistake, either post it all or do a minimum program and you'll probably find the problem yourself. – arcy Jan 17 '16 at 20:01
  • I added more code above. – Robert Jan 17 '16 at 20:19

2 Answers2

0
    // Below Code is simple Demonstration about How to combine a JTextArea with a ScrollPane.
// I wish this code logic solve your problem.

import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Tutorial extends JFrame {

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

    public Tutorial() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        getContentPane().setLayout(null);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(124, 84, 171, 106);
        getContentPane().add(scrollPane);

        JTextArea textArea = new JTextArea();
        textArea.setFont(new Font("Monospaced", Font.BOLD, 15));
        scrollPane.setViewportView(textArea);
        setVisible(true);
    }
}
Sadhan Sarker
  • 129
  • 3
  • 11
0

The problem is (still) not in the code you've posted, either the original code nor the extra code. I created a class which leaves all your code the way it is, makes instance variables of all the ones that don't have declarations for, and instantiates the frame that your code uses but doesn't initialize. It creates a scrollPane around a text area just fine. I leave it here for your study if you need it; the problem is elsewhere.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;


public class PanelPlay
{
  JFrame windowContainer; 
  JPanel rightWindow;
  JPanel rightWindowMaximumWidth;
  private BoxLayout rightWindowSplit;
  private JPanel rightUp;
  private JTextArea rightUpTextArea;
  private JScrollPane rightUpScrollPane;
  private JPanel rightDownBoxPanel;
  private BoxLayout rightDownBox;
  private JTextArea rightDownTextArea;
  private JScrollPane rightDownTextAreaScrollPane;
  private JButton rightDownButtomSend;

  public static void main(String ... arguments)
  {
    PanelPlay pp = new PanelPlay();
    pp.go();
  }

  public void go()
  {
    windowContainer = new JFrame();
    setupRightSide();
    windowContainer.pack();
    windowContainer.setVisible(true);
  }

  private void setupRightSide(){
    //rightWindow
    rightWindow             = new JPanel(new BorderLayout(10,10));
    rightWindowMaximumWidth = new JPanel();
    rightWindowSplit        = new BoxLayout(rightWindowMaximumWidth, BoxLayout.Y_AXIS);//"rightWindow" is the parent, not "windowContainer"!!!

    rightUp                 = new JPanel();
    rightUpTextArea         = new JTextArea();
    rightUpScrollPane       = new JScrollPane(rightUpTextArea);

    rightDownBoxPanel       = new JPanel();
    rightDownBox            = new BoxLayout(rightDownBoxPanel, BoxLayout.X_AXIS);//"rightDownBoxPanel" is the parent, not "rightWindow"!!!
    rightDownTextArea       = new JTextArea();
    rightDownTextAreaScrollPane = new JScrollPane(rightDownTextArea);
    rightDownButtomSend     = new JButton("Send");

    //rightUpScrollPane.setAutoscrolls(true);

    windowContainer.add(rightWindow, BorderLayout.CENTER);

    //rechte Seite
    rightWindow.setPreferredSize(new Dimension(250,250));
    rightWindow.setSize(new Dimension(424,503));  // habe ich durch datenausgabe mit der Konsole herrausgefundem
    rightWindow.add(rightWindowMaximumWidth, BorderLayout.CENTER);

    //container in "rechte Seite" mit maximaler width von 1000px
    rightWindowMaximumWidth.setLayout(rightWindowSplit);
    rightWindowMaximumWidth.setPreferredSize(new Dimension(rightWindow.getWidth()-10,1000));
    rightWindowMaximumWidth.setMaximumSize(new Dimension(1000,Integer.MAX_VALUE));

    rightWindowMaximumWidth.add(rightUpScrollPane, "Up");
    rightWindowMaximumWidth.add(Box.createRigidArea(new Dimension(0,10)));
    rightWindowMaximumWidth.add(rightDownBoxPanel, "Down");
    rightWindowMaximumWidth.add(Box.createRigidArea(new Dimension(0,10)));

    setupRechtsOben();
    setupRechtsUnten();

}

private void setupRechtsOben(){
    //obere Teil der rechten Seite
    rightUp.setPreferredSize(new Dimension(420 , 400));
    rightUp.setMaximumSize(new Dimension(420,400));
   // rightUp.add(rightUpScrollPane);

    //TextFeld rechts-oben
    rightUpTextArea.setBorder( new CompoundBorder(new javax.swing.border.LineBorder(Color.BLACK), new EmptyBorder(15, 15, 15, 15)));
    rightUpTextArea.setEditable(false);
    rightUpTextArea.setLineWrap(true);
    rightUpTextArea.setWrapStyleWord(true);
    rightUpTextArea.setBackground(Color.lightGray);
    rightUpTextArea.setPreferredSize(new Dimension(420,350));//-2 damit der rand zu sehen ist
    rightUpTextArea.setMaximumSize(rightWindowMaximumWidth.getMaximumSize());
    rightUpTextArea.setText("d\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\n"); //to fill it with text


}

private void setupRechtsUnten(){
    //untere Teil der rechten Seite (vertikales BoyLayout)
    rightDownBoxPanel.setLayout(rightDownBox);
    rightDownBoxPanel.add(rightDownTextAreaScrollPane, "TextField");
    rightDownBoxPanel.add(Box.createRigidArea(new Dimension(10,0)));
    rightDownBoxPanel.add(rightDownButtomSend, "SendButton");
    rightDownBoxPanel.setMaximumSize(new Dimension(1000,100));
    rightDownBoxPanel.setPreferredSize(new Dimension(1000 ,100));


    //TextEingabe im BoxLayout
    rightDownTextArea.setBorder(new javax.swing.border.LineBorder(Color.BLACK));
    //Damit es in der nächsten Zeile weitergeht, wenn diese voll ist
    rightDownTextArea.setLineWrap(true);
    rightDownTextArea.setWrapStyleWord(true);

    //SendButton im BoxLayout
    rightDownButtomSend.setSize(new Dimension(100, 50));
    rightDownButtomSend.setPreferredSize(new Dimension(100, 50));
    rightDownButtomSend.setMaximumSize(new Dimension(100, 50));
    rightDownButtomSend.setFont(new Font("Arial", Font.BOLD, 14));
    rightDownButtomSend.setAlignmentX(JFrame.CENTER_ALIGNMENT);

}
}

Unlike your code, you can compile this and run it, and see the scrollPane created.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • Please see [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi). – user1803551 Jan 18 '16 at 00:51
  • No thanks. It doesn't have anything to do with the question at hand. – arcy Jan 18 '16 at 03:03
  • Thank you. This is exacly what I wanted. I tried your code and it works. – Robert Jan 18 '16 at 16:06
  • @user1803551 That wasn't my question but thank you too. I had no idea about it. Have learned something again. – Robert Jan 18 '16 at 16:21
  • @Robert I wasn't commenting on your question, I was commenting on the misuse of Swing code in the answer. It's an "FYI" if you will. – user1803551 Jan 18 '16 at 16:24
  • The part of the answer that contains any setXSize() method calls is copied from the original post, so you *ARE* commenting on OP's question. I find it annoying when the "Swing police" swoop in with irrelevant and unnecessary "corrections" as though someone has committed some gross indecency. It's just noise, it is off-topic, it doesn't help anyone. Please don't litter. – arcy Jan 18 '16 at 18:07
  • I found my mistake: I just forgot "frame.pack()", that was all. I am new to swing and never saw it before. So thanks to arcy for your code. – Robert Jan 18 '16 at 18:21
  • Edit: And I removed "rightUpTextArea.setPreferredSize(new Dimension(420,350));". Otherwise the text-area would be limeted. – Robert Jan 18 '16 at 18:49