-2

I don't know where the problem is, when the sample code is run alone and from scratch, the scrollbars are displayed correctly, but when we try to add them to a task area in the middle of a prepared frame, the text area disappears. The last solution I tried was this:

 createAndShowGUI() {
 //
 JTextArea textArea = new JTextArea(20, 20);
 JScrollPane scrollableTextArea = new JScrollPane(textArea);
 scrollableTextArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
 frame.getContentPane().add(scrollableTextArea)
 //}

and for run it:

 javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
           createAndShowGUI();
       }
   });

and had the same problem, Can Anybody Help?

Edit: I have to correct the question

In a good example, I would use this Line:

frame.getContentPane().setLayout(new FlowLayout());

And in bad usage, I used this line:

frame.getContentPane().setLayout(null); //"null" cannot be replaced because the frame and its objects are moved

Is there a layout that does not move the previous objects of the frame and shows the scroll bar correctly?

3
  • 2
    I have no idea what "from scratch" or "prepared frame" mean. Post a proper minimal reproducible example demonstrating the problem. We should be able to copy/paste/compile test the code. Commented Oct 30, 2022 at 16:02
  • @camickr according to your advice, i did Commented Oct 31, 2022 at 4:36
  • (1-) according to your advice, i did - how can we copy/paste/compile test random lines of code? Is there a layout that does not move the previous objects - The code only shows a text area. What previous objects are you talking about? Your verbal description of the problem is NOT clear, which is why an minimal reproducible example should be included with every question so we can see exactly what you are doing. The general answer to your question is yes, there are layout managers that will do what you want. Read the Layout Manager tutorial. Commented Oct 31, 2022 at 14:29

1 Answer 1

0

I found the solution to the problem by selecting the appropriate layout:

frame1= new JFrame("farme1");
frame1.setBounds(100, 100, 200, 200);
frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame1.setLayout(new BoxLayout(frame1.getContentPane(),BoxLayout.PAGE_AXIS) );//**key point**
frame1.setResizable(false);
JPanel pane = new JPanel(new GridBagLayout());
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

and some other initialization for c.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.