Jump to content

JTextArea question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
seanbp

seanbp

    Newbie

  • Members
  • Pip
  • 7 posts
Why do I need JScrollPane to get JTextArea's layout to behave properly?

    private JPanel Panel () {

        JPanel panel = new JPanel();

        Font font = new Font("Arial", Font.BOLD, 18);

        panel.setLayout(new BorderLayout());

        inText = new JTextArea();

        inText.setEditable(false);

        inText.setFont(font);

        inText.setLineWrap(true);

        inText.setWrapStyleWord(true);

        JScrollPane inTextScroller = new JScrollPane(inText, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        panel.add(inTextScroller, BorderLayout.CENTER);


        outText = new JTextArea(2, 1);

        outText.addKeyListener(listen);

        outText.setFont(font);

        outText.setLineWrap(true);

        outText.setWrapStyleWord(true);

        JScrollPane outTextScroller = new JScrollPane(outText, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        panel.add(outTextScroller, BorderLayout.SOUTH);

        return panel;

    }


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
What's wrong with the layout without scrollpanes?

#3
seanbp

seanbp

    Newbie

  • Members
  • Pip
  • 7 posts
The dimensions are all wrong; it gets way too large. I need to resize my frame just to see the other text area.

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
They are pretty normal with me:
Attached File  2hs421t..jpg   20.57K   46 downloads

#5
seanbp

seanbp

    Newbie

  • Members
  • Pip
  • 7 posts
THIS is is buggy code... THAT was using a scroller. sorry for the confusion.
    private JPanel panel () {

        JPanel panel = new JPanel();

        panel.setLayout(new BorderLayout());

        Font font = new Font("Arial", Font.BOLD, 12);

        Listen listen = new Listen();


        inText = new JTextArea();

        inText.setEditable(false);

        inText.setFont(font);

        inText.setLineWrap(true);

        inText.setWrapStyleWord(true);

        JScrollPane inTextScroller = new JScrollPane(inText, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        panel.add(inTextScroller, BorderLayout.CENTER);


        outText = new JTextArea(2, 1);

        outText.addKeyListener(listen);

        outText.setFont(font);

        outText.setLineWrap(true);

        outText.setWrapStyleWord(true);

        //JScrollPane outTextScroller = new JScrollPane(outText, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        panel.add(outText, BorderLayout.SOUTH);


        return panel;

    }


#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
That looks like Attached File  Test2.JPG   11.85K   173 downloads...

Tested with:

public static void main(String[] args) {    
        JFrame frame = new JFrame("test");
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        Font font = new Font("Arial", Font.BOLD, 12);


        JTextArea inText = new JTextArea();
        inText.setEditable(false);
        inText.setFont(font);
        inText.setLineWrap(true);
        inText.setWrapStyleWord(true);
        inText.setText("test text");
        inText.setBackground(Color.cyan);
        JScrollPane inTextScroller = new JScrollPane(inText, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        panel.add(inTextScroller, BorderLayout.CENTER);

        JTextArea outText = new JTextArea(2, 1);
        outText.setFont(font);
        outText.setLineWrap(true);
        outText.setWrapStyleWord(true);
        //JScrollPane outTextScroller = new JScrollPane(outText, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        panel.add(outText, BorderLayout.SOUTH);

        
        
        
        
        frame.add(panel);
        frame.setSize(800,600);
        frame.setVisible(true);
        
    }


#7
seanbp

seanbp

    Newbie

  • Members
  • Pip
  • 7 posts
must be unique to my os or probably the environment. thanks for the help.

#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Java should be OS-independant so OS shouldn't really matter. Perhaps your screen DPI can mess up layout if it's set to something different than 96DPI..

#9
seanbp

seanbp

    Newbie

  • Members
  • Pip
  • 7 posts
DPI is 96.
You're right, it should be independent. I'm using OpenJDK. I've attached some screenshots...

The second text area resizes to it's content and overlaps the other text area. Perhaps the two are incompatible with each-other with respect to the layout.