Silamoth
Silamoth

Reputation: 36

Add a JScrollBar to a JTextArea

I am trying to make a GUI server-to-client message program. I wrote the network side, and that is irrelevant to the problem. I am trying to make a JTextArea scroll with a JScrollBar. How would I do this? Here is my code for the client (with most of the networking code removed):

public class MyClient extends JFrame
{

    public Client client;
    public static Scanner scanner;      
    public JTextField textField;
    public JLabel label;        
    public static String string;        
    public static JTextArea textArea;           
    public String username;                 
    public JScrollBar scrollBar;        
    public JScrollPane scrollPane;

    public MyClient()
    {                   
        setTitle("Client");
        setSize(800, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new FlowLayout());                    
        textArea = new JTextArea("");           
        scrollBar = new JScrollBar();           
        label = new JLabel("Please enter your message");
        add(label);         
        textField = new JTextField(70);
        add(textField);
        textField.addActionListener(new ActionListener()
        {               
            @Override
            public void actionPerformed(ActionEvent e) 
            {                   
                textArea.append(username + ": " + textField.getText() + "\n");                  textField.setText(null);                    
            }               
        }); 

        add(textArea);          
        add(scrollBar, BorderLayout.EAST);          
        string = textField.getText();                       
        scanner = new Scanner(System.in);           
    }

     class MyAdjustmentListener implements AdjustmentListener 
     {

            public void adjustmentValueChanged(AdjustmentEvent e) 
            {                   
               label.setText("    New Value is " + e.getValue() + "      ");
               repaint();                  
            }               
        }

    public static void main(String[] args) throws IOException
    {           
        SwingUtilities.invokeLater(new Runnable()
        {

            @Override
            public void run()
            {                   
                MyClient myClient = new MyClient();
                myClient.setVisible(true);
                myClient.setResizable(false);                   
            }

        });         
    }       
}

Upvotes: 0

Views: 3836

Answers (2)

Braj
Braj

Reputation: 46841

Simply add the JTextArea inside the JScrollPane and then add the JScrollPane in the container instead of adding JTextArea itself.

There is no need of adding JScrollBar, scroll bars are by default shown if needed.

Sample code:

textArea = new JTextArea("");
scrollPane = new JScrollPane(textArea);

Find a working sample here How can we add JScrollPane on JTextArea in java?


Some of the points that I noticed in your code:

  1. JFrame default layout is changed to FlowLayout

    setLayout(new FlowLayout()); 
    
  2. Now adding components as per the BorderLayout properties

    add(scrollBar, BorderLayout.EAST);   
    
  3. A lots of instance variables are declared but never used in the code.

Note: First add the components in a container such as JPanel then finally add the JPanel in the JFrame.

Please read it again A Visual Guide to Layout Managers and choose the appropriate layout manager for your application design.

Upvotes: 1

fscore
fscore

Reputation: 2619

You would need a JScrollPane instead of a JScrollBar, try this code:

JTextArea textArea = new JTextArea ("");

JScrollPane scroll = new JScrollPane (textArea, 
   JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

In the above code, you are assigning a textArea to a ScrollPane and making it scroll vertically and horizontally.

Another way of doing it, Create the ScrollPane containing the TextArea and then Setting vertical scroll = always on :

JTextArea textArea= new JTextArea();     
JScrollPane scroll= new JScrollPane(textArea); 

scroll. setVerticalScrollBarPolicy( JScrollPane. VERTICAL_SCROLLBAR_ALWAYS ); 

Read here for tutorial: Tutorial Link

Upvotes: 2

Related Questions