Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
Scrolling through a textarea
Martin vanPutten
Ranch Hand
Posts: 124
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
How do I make a scrollbar in my textarea?
import java.awt.*; import javax.swing.*; import java.awt.event.*; /** * Class ViewPanel - Manages the panels in the frame. * * @author Martin vanPutten * @version 2007.04.05 */ public class ViewPanel extends JPanel { private Game game = new Game(); public ViewPanel(Container pane) { pane.setLayout(null); JLabel titleLabel = new JLabel("ZUUL"); titleLabel.setFont(new Font("Comic Sans", Font.BOLD, 70)); JLabel authorLabel = new JLabel("By: Martin vanPutten"); authorLabel.setFont(new Font("Comic Sans", Font.PLAIN, 24)); JLabel goLabel = new JLabel("...Go..."); goLabel.setFont(new Font("Comic Sans", Font.PLAIN, 18)); JButton northButton = new JButton("North"); JButton eastButton = new JButton("East"); JButton southButton = new JButton("South"); JButton westButton = new JButton("West"); JButton northeastButton = new JButton("Northeast"); JButton northwestButton = new JButton("Northwest"); JButton southeastButton = new JButton("Southeast"); JButton southwestButton = new JButton("Southwest"); JButton takeButton = new JButton("Take"); JButton dropButton = new JButton("Drop"); JButton giveButton = new JButton("Give"); JButton backButton = new JButton("Back"); JButton chargeButton = new JButton("Charge"); JButton fireButton = new JButton("Fire"); JButton itemsButton = new JButton("Items"); JButton helpButton = new JButton("Help"); JButton fallButton = new JButton("Fall"); JButton quitButton = new JButton("Quit"); ImageIcon picture; if(game.currentRoom == game.construction) picture = new ImageIcon("images/construction.jpg"); else if(game.currentRoom == game.library) picture = new ImageIcon("images/library.jpg"); else if(game.currentRoom == game.pub) picture = new ImageIcon("images/pub.jpg"); else if(game.currentRoom == game.hallway1) picture = new ImageIcon("images/hallway1.jpg"); else if(game.currentRoom == game.hallway2) picture = new ImageIcon("images/hallway2.jpg"); else if(game.currentRoom == game.hallway3) picture = new ImageIcon("images/hallway3.jpg"); else if(game.currentRoom == game.lab) picture = new ImageIcon("images/lab.jpg"); else if(game.currentRoom == game.mainEntrance) picture = new ImageIcon("images/mainEntrance.jpg"); else if(game.currentRoom == game.office) picture = new ImageIcon("images/office.jpg"); else if(game.currentRoom == game.theatre) picture = new ImageIcon("images/theatre.jpg"); else picture = new ImageIcon("images/outside.jpg"); JLabel map = new JLabel(picture, JLabel.CENTER); final JTextArea textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); textArea.setEditable(false); textArea.setRows(17); textArea.setColumns(29); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); pane.add(titleLabel); pane.add(authorLabel); pane.add(goLabel); pane.add(northButton); pane.add(eastButton); pane.add(southButton); pane.add(westButton); pane.add(northeastButton); pane.add(northwestButton); pane.add(southeastButton); pane.add(southwestButton); pane.add(map); pane.add(textArea); pane.add(takeButton); pane.add(dropButton); pane.add(giveButton); pane.add(backButton); pane.add(chargeButton); pane.add(fireButton); pane.add(itemsButton); pane.add(helpButton); pane.add(fallButton); pane.add(quitButton); Insets insets = pane.getInsets(); Dimension size; size = titleLabel.getPreferredSize(); titleLabel.setBounds(45 + insets.left, 7 + insets.top, size.width, size.height); size = authorLabel.getPreferredSize(); authorLabel.setBounds(75 + insets.left, 90 + insets.top, size.width, size.height); size = goLabel.getPreferredSize(); goLabel.setBounds(523 + insets.left, 85 + insets.top, size.width, size.height); size = northButton.getPreferredSize(); northButton.setBounds(500 + insets.left, 50 + insets.top, 100, size.height); size = eastButton.getPreferredSize(); eastButton.setBounds(610 + insets.left, 85 + insets.top, 100, size.height); size = southButton.getPreferredSize(); southButton.setBounds(500 + insets.left, 120 + insets.top, 100, size.height); size = westButton.getPreferredSize(); westButton.setBounds(390 + insets.left, 85 + insets.top, 100, size.height); size = northeastButton.getPreferredSize(); northeastButton.setBounds(610 + insets.left, 50 + insets.top, 100, size.height); size = northwestButton.getPreferredSize(); northwestButton.setBounds(390 + insets.left, 50 + insets.top, 100, size.height); size = southeastButton.getPreferredSize(); southeastButton.setBounds(610 + insets.left, 120 + insets.top, 100, size.height); size = southwestButton.getPreferredSize(); southwestButton.setBounds(390 + insets.left, 120 + insets.top, 100, size.height); map.setBounds(75 + insets.left, 170 + insets.top, 275, 272); size = textArea.getPreferredSize(); textArea.setBounds(390 + insets.left, 170 + insets.top, size.width, size.height); size = takeButton.getPreferredSize(); takeButton.setBounds(120 + insets.left, 465 + insets.top, 100, size.height); size = dropButton.getPreferredSize(); dropButton.setBounds(120 + insets.left, 500 + insets.top, 100, size.height); size = giveButton.getPreferredSize(); giveButton.setBounds(230 + insets.left, 465 + insets.top, 100, size.height); size = backButton.getPreferredSize(); backButton.setBounds(230 + insets.left, 500 + insets.top, 100, size.height); size = chargeButton.getPreferredSize(); chargeButton.setBounds(340 + insets.left, 465 + insets.top, 100, size.height); size = fireButton.getPreferredSize(); fireButton.setBounds(340 + insets.left, 500 + insets.top, 100, size.height); size = itemsButton.getPreferredSize(); itemsButton.setBounds(450 + insets.left, 465 + insets.top, 100, size.height); size = helpButton.getPreferredSize(); helpButton.setBounds(450 + insets.left, 500 + insets.top, 100, size.height); size = fallButton.getPreferredSize(); fallButton.setBounds(560 + insets.left, 465 + insets.top, 100, size.height); size = quitButton.getPreferredSize(); quitButton.setBounds(560 + insets.left, 500 + insets.top, 100, size.height); textArea.append(game.printWelcome()); helpButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { textArea.setText(""); textArea.append(game.printHelp()); } } ); quitButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } } ); } }
Live And Let Learn.
Michael Dunn
Ranch Hand
Posts: 4632
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
pane.add(map); //pane.add(textArea); pane.add(scrollPane);//<---- pane.add(takeButton);
as pane is a null layout, you may have to give the textArea a preferredSize
so that the scrollPane knows when to 'kick in' the scrollBars. Experiment,
see what happens.
Martin vanPutten
Ranch Hand
Posts: 124
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I've played with the JScrollBar and Pane for a day now... and I must be missing something... what should it look like?
Live And Let Learn.
Martin vanPutten
Ranch Hand
Posts: 124
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
There we go... I figured it out!
Live And Let Learn.
It's exactly the same and completely different as this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Convert the .Tiff file to .png and insert into the database.
How to crop image in java
Creating multiple images using a loop.
Positioning many panels...
Null Pointer Exception
More...