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?
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.