Java question. how to add scrollbar?
-
I'm trying to learn how to add a scrollbar but I cant figure it out.. I used GuiGenie to add textArea please help i need to add a scrollbar in my textArea. thanks here's my gui private JButton enter; private JButton mon; private JButton tues; private JButton wed; private JButton thurs; private JButton fri; private JButton sat; private JTextArea textArea; public static void main (String[] args) { ButtonGUI bg = new ButtonGUI (); } public ButtonGUI() { //JFrame related super ("ButtonApplication"); //sets the title of your jFrame setLayout(null); //there are lots of pre-made layouts by Java. null means you want to take control of the layouting refer to http://download.oracle.com/javase/tutorial/uiswing/layout/layoutlist.html setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); //exits the program when your jframe closes setSize (560, 450); //size of your jframe setVisible (true); //makes your jframe visible to the user //construct components mon = new JButton ("Monday"); tues = new JButton ("Tuesday"); wed = new JButton ("Wednesday"); thurs = new JButton ("Thursday"); fri = new JButton ("Friday"); sat = new JButton ("Saturday"); textArea = new JTextArea (5, 5); //adjust size and set layout setPreferredSize (new Dimension (595, 373)); setLayout (null); //add components add (mon); add (tues); add (wed); add (thurs); add (fri); add (sat); add (textArea); //set component bounds (only needed by Absolute Positioning) mon.setBounds (40, 60, 155, 35); tues.setBounds (40, 105, 155, 35); wed.setBounds (40, 150, 155, 35); thurs.setBounds (40, 195, 155, 35); fri.setBounds (40, 240, 155, 35); sat.setBounds (40, 285, 155, 35); textArea.setBounds (235, 50, 285, 285); mon.addActionListener(this); tues.addActionListener(this); wed.addActionListener(this); thurs.addActionListener(this); fri.addActionListener(this); sat.addActionListener(this); }
-
Answer:
Here's the code that creates the text area, makes it the scroll pane's client, and adds the scroll pane to a container: //In a container that uses a BorderLayout: textArea = new JTextArea(5, 30); ... JScrollPane scrollPane = new JScrollPane(textArea); ... setPreferredSize(new Dimension(450, 110)); ... add(scrollPane, BorderLayout.CENTER); The boldface line of code creates the JScrollPane, specifying the text area as the scroll pane's client. The program doesn't invoke any methods on the JScrollPane object, since the scroll pane handles everything automatically: creating the scroll bars when necessary, redrawing the client when the user moves the scroll knobs, and so on. You might have noticed that the preceding code sets the preferred size of the scroll pane's container. In the Java look and feel, this preferred size happens to be a bit less tall than required for the text area to display the 5 rows that we requested when creating it, so the scroll bar initially displays a vertical scroll bar. If we didn't restrict the size of the scroll pane's container, the scroll pane would be big enough for the text area to display the full 5 rows and 30 columns specified with the JTextArea constructor. Refer to Sizing a Scroll Pane for information about techniques for making a scroll pane the size you want.
Rolly at Yahoo! Answers Visit the source
Other answers
it'd be better if you used netbeans. it actually lets you drag the gui to wherever you want.
Try jScrollPane = new JScrollPane(); jScrollPane.setViewportView(textArea);
Related Q & A:
- How To Add Fonts In Ubuntu?Best solution by wiki.ubuntu.com
- How To Add The News?Best solution by Quora
- How To Add Cityville Neighbors?Best solution by gamersunite.coolchaser.com
- How to make scrollbar transparent which is compatible in all browsers?Best solution by Stack Overflow
- How to add buttons and panels in frame in java?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.