问题
I am working on a project for my college course. I was just wondering if anyone knew how to add a scrollBar to a JTextArea. At present I have the GUI laid out correctly, the only thing missing is the scroll bar.
This is what the GUI looks like. As you can see on the second TextArea I would like to add the Scrollbar.
This is my code where I create the pane. But nothing seems to happen... t2 is the JTextArea I want to add it to.
scroll = new JScrollPane(t2); scroll.setBounds(10,60,780,500); scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
Any help would be great, thanks!
回答1:
The Scroll Bar comes when your text goes beyond the bounds of your view area. Don't use Absolute Positioning, for such a small talk at hand, always prefer Layout Managers, do read the first para of the first link, to know the advantage of using a Layout Manager.
What you simply need to do is use this thingy :
JTextArea msgArea = new JTextArea(10, 10); msgArea.setWrapStyleWord(true); msgArea.setLineWrap(true); JScrollPane msgScroller = new JScrollPane(); msgScroller.setBorder( BorderFactory.createTitledBorder("Messages")); msgScroller.setViewportView(msgArea); panelObject.add(msgScroller);
Here is a small program for your understanding :
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class JTextAreaScroller { private JTextArea msgArea; private JScrollPane msgScroller; private JTextArea logArea; private JScrollPane logScroller; private JButton sendButton; private JButton terminateButton; private Timer timer; private int counter = 0; private String[] messages = { "Hello there\n", "How you doing ?\n", "This is a very long text that might won't fit in a single line :-)\n", "Okay just to occupy more space, it's another line.\n", "Don't read too much of the messages, instead work on the solution.\n", "Byee byee :-)\n", "Cheers\n" }; private ActionListener timerAction = new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { if (counter < messages.length) msgArea.append(messages[counter++]); else counter = 0; } }; private void displayGUI() { JFrame frame = new JFrame("Chat Messenger Dummy"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel contentPane = new JPanel(); contentPane.setLayout(new BorderLayout(5, 5)); JPanel centerPanel = new JPanel(); centerPanel.setLayout(new GridLayout(0, 1, 5, 5)); logArea = new JTextArea(10, 10); logArea.setWrapStyleWord(true); logArea.setLineWrap(true); logScroller = new JScrollPane(); logScroller.setBorder( BorderFactory.createTitledBorder("Chat Log")); logScroller.setViewportView(logArea); msgArea = new JTextArea(10, 10); msgArea.setWrapStyleWord(true); msgArea.setLineWrap(true); msgScroller = new JScrollPane(); msgScroller.setBorder( BorderFactory.createTitledBorder("Messages")); msgScroller.setViewportView(msgArea); centerPanel.add(logScroller); centerPanel.add(msgScroller); JPanel bottomPanel = new JPanel(); terminateButton = new JButton("Terminate Session"); terminateButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { if (timer.isRunning()) timer.stop(); else timer.start(); } }); sendButton = new JButton("Send"); bottomPanel.add(terminateButton); bottomPanel.add(sendButton); contentPane.add(centerPanel, BorderLayout.CENTER); contentPane.add(bottomPanel, BorderLayout.PAGE_END); frame.setContentPane(contentPane); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); timer = new Timer(1000, timerAction); timer.start(); } public static void main(String... args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new JTextAreaScroller().displayGUI(); } }); } }
Here is the outcome of the same :
回答2:
The scroll bar by default will only be shown when the content overfills the available viewable area
You can change this via the JScrollPane#setVerticalScrollBarPolicy method, passing it ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS
来源:https://stackoverflow.com/questions/13096045/adding-scrollpane-to-jtextarea