1

Here is my code:

final JTextArea textArea = new JTextArea();
textArea.setFont(new Font("MS UI Gothic", Font.PLAIN, 13));
textArea.setLineWrap(true);
textArea.setBounds(77, 310, 474, 136);
//contentPane.add(textArea); (edited...still the same problem persists..)


JScrollPane sbrText = new JScrollPane(textArea);
sbrText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);  
contentPane.add(sbrText);

When ever I try with this,the text area is not visible..(I am using Eclipse's Window Builder plugin and layout as "Absolute Layout")..

voidMainReturn
  • 3,339
  • 6
  • 38
  • 66
Code Zero
  • 113
  • 2
  • 9
  • 2
    You don't need to add the `textArea` to your `contentPane` because it's already included in the `sbrText` afterwards. See [this tutorial](http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html) for more information. – Michael Lang Jul 06 '13 at 10:56
  • @MichaelLang: Yup....tried that too mate...no luck..!! – Code Zero Jul 06 '13 at 10:59
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jul 06 '13 at 11:49
  • @AndrewThompson : Here is my full code.. https://friendco.de/ui#!/workspace/user/soumava/java-spring-sample/master/employeeloggedin.java – Code Zero Jul 06 '13 at 17:34
  • *"Here is my full code.."* Not interested. Where is that SSCCE? Post it *here*.. – Andrew Thompson Jul 07 '13 at 01:41
  • @AndrewThompson: The above link that I provided does contain the full compilable correct example...!!! Here is a screenshot: http://oi40.tinypic.com/11jyum0.jpg – Code Zero Jul 07 '13 at 06:19
  • *"does contain the full compilable correct example"* I did not advise you to post an FCCE, but an ***SSCCE.*** If you had an SSCCE, it could be posted an an [edit to the question](http://stackoverflow.com/posts/17502210/edit) as opposed to a link I won't visit. – Andrew Thompson Jul 07 '13 at 07:17
  • Okhay...that won't be nescessary now as I've already figured out where I went wrong..So,posted the answer.. – Code Zero Jul 07 '13 at 07:27

3 Answers3

3

Here I have done, the same thingy using Nested Layout, have a look at the code example :

import java.awt.*;
import javax.swing.*;

public class WelcomeExample
{
    private JPanel headerPanel;
    private JButton logoutButton;

    private JPanel leavePanel;
    private JRadioButton casualRButton;
    private JRadioButton specialRButton;
    private JRadioButton sickRButton;
    private JRadioButton privilegeRButton;
    private ButtonGroup radioButtonGroup;

    private JTextField leaveDaysField;
    private JButton checkLeaveButton;

    private JTextArea notesArea;
    private JScrollPane notesScroller;

    private JButton applyLeaveButton;

    private String headerText = "<html><body><h1><font " + 
            "color=\"red\">Welcome : </font><font color" +
            "=\"blue\">Code Zero</font></h1></body></html>";

    private void displayGUI()
    {
        JFrame frame = new JFrame("Welcome");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(5, 5));
        contentPane.setBorder(
            BorderFactory.createEmptyBorder(5, 5, 5, 5));

        headerPanel = getHeaderPanel();
        leavePanel = getLeavePanel();

        contentPane.add(headerPanel, BorderLayout.PAGE_START);
        contentPane.add(leavePanel, BorderLayout.CENTER);
        contentPane.add(getApplyPanel(), BorderLayout.PAGE_END);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel getHeaderPanel()
    {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout(5, 5));
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        JLabel headerLabel = new JLabel(headerText, JLabel.CENTER);
        JPanel buttonPanel = new JPanel();
        logoutButton = new JButton("Logout");
        buttonPanel.add(logoutButton);
        panel.add(headerLabel, BorderLayout.CENTER);
        panel.add(buttonPanel, BorderLayout.LINE_END);
        panel.add(new JSeparator(
            SwingConstants.HORIZONTAL), BorderLayout.PAGE_END);

        return panel;
    }

    private JPanel getLeavePanel()
    {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout(5, 5));
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        JPanel leaveHeaderPanel = new JPanel();
        leaveHeaderPanel.setLayout(new GridLayout(0, 1, 5, 5));
        leaveHeaderPanel.setBorder(
            BorderFactory.createTitledBorder("Choose a leave type : "));
        JPanel leaveTypePanel = new JPanel();
        leaveTypePanel.setLayout(new FlowLayout(
                                FlowLayout.LEFT, 5, 5));
        casualRButton = new JRadioButton("Casual Leave");
        specialRButton = new JRadioButton("Special Leave");
        sickRButton = new JRadioButton("Sick Leave");
        privilegeRButton = new JRadioButton("Privilege Leave");

        radioButtonGroup = new ButtonGroup();
        radioButtonGroup.add(casualRButton);
        radioButtonGroup.add(specialRButton);
        radioButtonGroup.add(sickRButton);
        radioButtonGroup.add(privilegeRButton);

        leaveTypePanel.add(casualRButton);
        leaveTypePanel.add(specialRButton);
        leaveTypePanel.add(sickRButton);
        leaveTypePanel.add(privilegeRButton);

        JPanel applyLeavePanel = new JPanel();
        applyLeavePanel.setLayout(new FlowLayout(
                                FlowLayout.LEFT, 5, 5));
        JLabel applyLeaveLabel = new JLabel(
                "Apply for (No. of days) : ", JLabel.CENTER);
        leaveDaysField = new JTextField(5);
        checkLeaveButton = new JButton("Check Leave Availability");

        applyLeavePanel.add(applyLeaveLabel);
        applyLeavePanel.add(leaveDaysField);
        applyLeavePanel.add(checkLeaveButton);

        leaveHeaderPanel.add(leaveTypePanel);
        leaveHeaderPanel.add(applyLeavePanel);

        notesArea = new JTextArea(10, 10);
        notesScroller = new JScrollPane();
        notesScroller.setBorder(
            BorderFactory.createTitledBorder(
                "Leave Note (Max. 200 Characters) : "));
        notesScroller.setViewportView(notesArea);

        panel.add(leaveHeaderPanel, BorderLayout.PAGE_START);
        panel.add(notesScroller, BorderLayout.CENTER);

        return panel;
    }

    private JPanel getApplyPanel()
    {
        JPanel panel = new JPanel();
        applyLeaveButton = new JButton("Apply");
        panel.add(applyLeaveButton);

        return panel;
    }

    public static void main(String[] args)
    {
        Runnable runnable = new Runnable()
        {
            @Override
            public void run()
            {
                new WelcomeExample().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

OUTPUT :

WELCOME EXAMPLE

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • ahhh...just what I was looking for..!! Thanks maitey..!! :D (Y) – Code Zero Jul 07 '13 at 08:29
  • Do compare it with you code, I am too sure, it does not contains the fuss, we were talking about related to using Layout Managers :-), for the rest You're MOST WELCOME and KEEP SMILING :-) – nIcE cOw Jul 07 '13 at 08:30
1

I don't think you need to do contentPane.add(textArea);. It is this line that is causing the problem. Comment out this and your code should work fine.

See this answer, it might help you.

The following code runs fine at my place :

final JTextArea textArea = new JTextArea();
        textArea.setFont(new Font("MS UI Gothic", Font.PLAIN, 13));
        textArea.setLineWrap(true);
        textArea.setBounds(77, 310, 474, 136);

        JScrollPane sbrText = new JScrollPane(textArea);
        sbrText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        frame.getContentPane().add(sbrText);//contentPane.add(sbrText);
        frame.setVisible(true);

If your code is not running fine then you must have some other error probably related to your contentpane.

Community
  • 1
  • 1
voidMainReturn
  • 3,339
  • 6
  • 38
  • 66
  • As per the previous comment/suggestion I did try that out too... But the problem remains the same..!! :( – – Code Zero Jul 06 '13 at 11:07
  • Also I did try the code snippet in this : http://stackoverflow.com/questions/8849063/adding-a-scrollable-jtextarea-java... Still no luck..!! :-( – Code Zero Jul 06 '13 at 11:16
  • @CodeZero : Have you tried to initialize your `JTextArea` like this `JTextArea tArea = new JTextArea(20, 20);` providing `rows/columns` (don't use the no-argument constructor), that will do. Give some size(dont' use setXxX() methods though, just this thingy alone) in this way to the `JTextArea` so that `JScrollPane` can rely on it. the answer is good, no doubt in that :-), already upvoted this one – nIcE cOw Jul 06 '13 at 11:25
  • I refered to this example..and specified definite rows and columns.. http://stackoverflow.com/questions/13273752/changing-jtextarea-to-jscrollpane-causes-it-to-not-be-visible Still...problem persists..!! I'm getting kinda frustrated now..!! :P – Code Zero Jul 06 '13 at 11:29
  • @nIcEcOw : accepted ! but this code is just for illustration purpose that it works. Otherwise I am all for using constructor to determine size. – voidMainReturn Jul 06 '13 at 11:34
  • @CodeZero : You can check this [__example__](http://gagandeepbali.uk.to/gaganisonline/swing/downloads/TextAreaScroller.java) to verify the thingy works, god knows what you doing at your end :( – nIcE cOw Jul 06 '13 at 11:34
  • @tejas : I know your answer has been upvoted.. But I just can't get it to work....still...!!!! Did everything as per the suggestions..!!! :'( – Code Zero Jul 06 '13 at 11:34
  • @CodeZero : I wasn't talking about the upvote, I was talking about the code that I added and it works. – voidMainReturn Jul 06 '13 at 11:36
  • Here is my full code.. https://friendco.de/ui#!/workspace/user/soumava/java-spring-sample/master/employeeloggedin.java – Code Zero Jul 06 '13 at 11:52
1

Ok...I finally got it to work...I specified the same set bounds in the scroll pane instead of the text area.. Here is the code.!!! ^_^

        final JTextArea textArea = new JTextArea();
        textArea.setFont(new Font("MS UI Gothic", Font.PLAIN, 13));
        textArea.setLineWrap(true);
        //textArea.setBounds(77, 310, 474, 136);

        JScrollPane scroll = new JScrollPane (textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);         
        scroll.setBounds(77, 310, 474, 136);
        contentPane.add(scroll);

Here are the screenshots:

Previous: http://oi40.tinypic.com/11jyum0.jpg
Now: http://oi44.tinypic.com/2s9vdvt.jpg

Code Zero
  • 113
  • 2
  • 9
  • 1
    That is what you doing wrong, using `Absolute Positioning` __(The Bane of all problems)__ . Instead you should be using Nested Layouts for achieving this `View` :-) – nIcE cOw Jul 07 '13 at 07:30
  • Haha..well ya..but since I'm new to JAVA programming,with Absolute Positioning I seem to get what I need without much fuss..!! :P Thanks for all your tips though..!! :) – Code Zero Jul 07 '13 at 07:37
  • When I was new to Java Swing, I use to do the same mistakes as you doing now, but later on I realized how important Layouts are a part of Swing :-) – nIcE cOw Jul 07 '13 at 07:39
  • Learning is all that counts in the end..!! :P – Code Zero Jul 07 '13 at 07:40
  • 2
    even if it appears to somehow solve the problem, it's the *wrong-thingy* to do - there is no way around learning to use *real* LayoutManagers (not that bad excuse called Absolute :-), the earlier the better. -1 (sorry, but this answer is seriously misleading) – kleopatra Jul 07 '13 at 08:16