Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9134347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:38:37+00:00 2026-06-17T08:38:37+00:00

I don’t know why my scroll bar in text area doesn’t work. I found

  • 0

I don’t know why my scroll bar in text area doesn’t work. I found many solutions in internet, but no 1 helped for me.

textArea1 = new JTextArea();
textArea1.setBounds(13, 28, 182, 199);
panel.add(textArea1);

JScrollBar scrollBar = new JScrollBar();
scrollBar.setBounds(205, 1, 17, 242);
panel.add(scrollBar);

I found that can’t be Panel’s layout Absolute, if I change It to Group layout the same.
What’s wrong? Could you help me? Thank you.

UPDATED:

package lt.kvk.i3_2.kalasnikovas_stanislovas;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JTextPane;
import javax.swing.DropMode;
import javax.swing.JFormattedTextField;
import java.awt.Component;
import javax.swing.Box;
import java.awt.Dimension;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JMenuItem;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import java.awt.SystemColor;
import java.awt.Font;
import javax.swing.border.BevelBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.JScrollBar;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

import javax.swing.JLabel;
import javax.swing.JToolBar;

public class KDVizualizuotas {

    private JFrame frmInformacijaApieMuzikos;
    private JTextField txtStilius;
    private JTextArea textArea1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    KDVizualizuotas window = new KDVizualizuotas();
                    window.frmInformacijaApieMuzikos.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public KDVizualizuotas() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frmInformacijaApieMuzikos = new JFrame();
        frmInformacijaApieMuzikos.setResizable(false);
        frmInformacijaApieMuzikos.setIconImage(Toolkit.getDefaultToolkit().getImage(KDVizualizuotas.class.getResource("/lt/kvk/i3_2/kalasnikovas_stanislovas/resources/Sidebar-Music-Blue-icon.png")));
        frmInformacijaApieMuzikos.setTitle("Muzikos stiliai");
        frmInformacijaApieMuzikos.setBounds(100, 100, 262, 368);
        frmInformacijaApieMuzikos.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        txtStilius = new JTextField();
        txtStilius.setBounds(10, 34, 128, 20);
        txtStilius.setColumns(10);

        JButton btnIekoti = new JButton("Ie\u0161koti");
        btnIekoti.setBounds(146, 36, 89, 19);
        btnIekoti.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
        //      textArea1.append(txtStilius.getText()+"\n");
        //      txtStilius.getText();



                 Scanner input = new Scanner(System.in);
                    try {
                        FileReader fr = new FileReader("src/lt/kvk/i3_2/kalasnikovas_stanislovas/Stiliai.txt");
                        BufferedReader br = new BufferedReader(fr);
                        String stiliuSarasas;
                        while((stiliuSarasas = br.readLine()) != null) {
                            System.out.println(stiliuSarasas);
                            textArea1.append(stiliuSarasas+"\n");
                            }
                        fr.close();
                        }
                    catch (IOException e) {
                        System.out.println("Error:" + e.toString());
                    }





            }
        });

        JPanel panel = new JPanel();
        panel.setBounds(10, 65, 224, 243);
        panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
        panel.setBackground(SystemColor.text);

        JLabel lblveskiteMuzikosStili = new JLabel("\u012Eveskite muzikos stili\u0173:");
        lblveskiteMuzikosStili.setBounds(10, 14, 222, 14);
        frmInformacijaApieMuzikos.getContentPane().setLayout(null);
        panel.setLayout(null);
        frmInformacijaApieMuzikos.getContentPane().add(panel);


        JLabel lblInformacijaApieMuzikos = new JLabel("Informacija apie muzikos stili\u0173:");
        lblInformacijaApieMuzikos.setBounds(12, 3, 190, 14);
        panel.add(lblInformacijaApieMuzikos);

        textArea1 = new JTextArea();
        textArea1.setBounds(13, 28, 182, 199);
        panel.add(textArea1);

        JScrollBar scrollBar = new JScrollBar();
        scrollBar.setBounds(205, 1, 17, 242);
        panel.add(scrollBar);
        frmInformacijaApieMuzikos.getContentPane().add(txtStilius);
        frmInformacijaApieMuzikos.getContentPane().add(btnIekoti);
        frmInformacijaApieMuzikos.getContentPane().add(lblveskiteMuzikosStili);

        JMenuBar menuBar = new JMenuBar();
        frmInformacijaApieMuzikos.setJMenuBar(menuBar);

        JMenu mnFile = new JMenu("File");
        menuBar.add(mnFile);

        JMenuItem mntmExit = new JMenuItem("Exit");
        mntmExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.exit(0);             
            }

        });
        mntmExit.setIcon(new ImageIcon(KDVizualizuotas.class.getResource("/lt/kvk/i3_2/kalasnikovas_stanislovas/resources/exitas.png")));
        mnFile.add(mntmExit);

        JMenu mnEdit = new JMenu("Edit");
        menuBar.add(mnEdit);

        JMenu mnHelp = new JMenu("Help");
        menuBar.add(mnHelp);

        JMenuItem mntmHelp = new JMenuItem("Help");
        mnHelp.add(mntmHelp);

        JMenu mnAbout = new JMenu("About");
        menuBar.add(mnAbout);

        JMenuItem mntmAbout = new JMenuItem("About");
        mntmAbout.setIcon(new ImageIcon(KDVizualizuotas.class.getResource("/lt/kvk/i3_2/kalasnikovas_stanislovas/resources/questionmark.png")));
        mnAbout.add(mntmAbout);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:38:38+00:00Added an answer on June 17, 2026 at 8:38 am

    You need to add the component you want to be contained within the scroll pane to it.

    You can do this via the JScrollPane‘s constructor or JScrollPane#setViewportView method

    enter image description here

    public class ScrollPaneTest {
    
      public static void main(String[] args) {
        new ScrollPaneTest();
      }
    
      public ScrollPaneTest() {
        EventQueue.invokeLater(new Runnable() {
          @Override
          public void run() {
            try {
              UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception ex) {
            }
    
            JPanel bigPane = new JPanel();
            bigPane.setBackground(Color.BLUE);
            // This is not recommended, but is used for demonstration purposes
            bigPane.setPreferredSize(new Dimension(1024, 768)); 
    
            JScrollPane scrollPane = new JScrollPane(bigPane);
            scrollPane.setPreferredSize(new Dimension(400, 400));
    
            JFrame frame = new JFrame("Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(scrollPane);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
          }
        });
      }
    }
    

    Updated with proper layout

    You should avoid null or absolute layouts, they will only hurt you in the long wrong.

    You may also find How to use Scroll Panes of use

    enter image description here

    public class BadLayout {
    
      public static void main(String[] args) {
        new BadLayout();
      }
    
      public BadLayout() {
        EventQueue.invokeLater(new Runnable() {
          @Override
          public void run() {
            try {
              UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception ex) {
            }
    
            JFrame frame = new JFrame("Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new TestPane());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
    
          }
        });
      }
    
      public class TestPane extends JPanel {
    
        private JTextField searchField;
        private JButton searchButton;
        private JTextArea searchResults;
    
        public TestPane() {
          setLayout(new BorderLayout());
    
          searchResults = new JTextArea();
          searchResults.setLineWrap(true);
          searchResults.setWrapStyleWord(true);
          JScrollPane scrollPane = new JScrollPane(searchResults);
    
          JPanel results = new JPanel(new BorderLayout());
          results.setBorder(new EmptyBorder(4, 8, 8, 8));
          results.add(scrollPane);
          add(results);
    
          JPanel search = new JPanel(new GridBagLayout());
          search.setBorder(new EmptyBorder(8, 8, 4, 8));
          searchField = new JTextField(12);
          searchButton = new JButton("Search");
    
          GridBagConstraints gbc = new GridBagConstraints();
          gbc.gridx = 0;
          gbc.gridy = 0;
          gbc.fill = GridBagConstraints.HORIZONTAL;
          gbc.weightx = 1;
          gbc.insets = new Insets(0, 0, 0, 4);
          search.add(searchField, gbc);
    
          gbc.insets = new Insets(0, 0, 0, 0);
          gbc.gridx++;
          gbc.fill = GridBagConstraints.NONE;
          gbc.weightx = 0;
          search.add(searchButton, gbc);
    
          add(search, BorderLayout.NORTH);
        }
    
        @Override
        public Dimension getPreferredSize() {
          return new Dimension(200, 400);
        }
      }  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't know why this doesn't work. I can't do implicit animation for a newly
Don't know how to frase this but I found this code wich works as
don't know if the title describes anything about what I'm trying to say but
Don't really know how to formulate the title, but it should be pretty obvious
Don't know how to google for such, but is there a way to query
Don't know why but font is not displaying.Please help. CSS(in css folder): style.css: @font-face
I don't know why, but this code worked for me a month ago... maybe
Don't know what happened, but it just hangs on Type Space loading (4%) and
Don't know whats exactly going on, but it's definitely killing my time for nothing.
Don't know a whole lot about streams. Why does the first version work using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.