user3314952
user3314952

Reputation: 21

JScrollPane wont appear alone

I want to add a jscroll pane in a jframe that is not alone with the something it scrolls through because whenever I add a JScrollPane I must make

JScrollPane p = new JScrollPane();
p.createVerticalScrollBar();
fr.setContentPane(p);
fr.revalidate();

if I just added it It doesn't appear like:

fr.add(p);

that doesn't appear..

I don't want the scrollpane to appear alone in the frame but with other things maybe a button and a textfield..

any help would be much appreciated

Upvotes: 0

Views: 45

Answers (1)

Benjamin
Benjamin

Reputation: 2286

       public class SampleFrame {  
    public static void main(String[] args) {  
    SwingUtilities.invokeLater(new Runnable(){  
     public void run()  
    {  
    createAndShowGUI();  
    }  
    });  
    }  
    public static void createAndShowGUI()  
   {  
  JFrame frame = new JFrame("JFrame with ScrollBar");  
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  JTextArea textArea = new JTextArea(5,20);  
  JScrollPane scrollPane = new JScrollPane(textArea);  
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);  
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);  
    frame.getContentPane().add(scrollPane);  
   frame.setSize(300, 200);  
   frame.setVisible(true);  
   }  
}

  try This

Upvotes: 1

Related Questions