how to add scrollbar to textarea in java - java

the jscrollpane that I am adding doesnt appearin my textarea
textArea = new JTextArea();
scroll = new JScrollPane(textArea);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.add(textArea);
this.add(scroll);
this.setSize(1000, 600);
this.setLayout(new BorderLayout());
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);

textArea = new JTextArea();
scroll = new JScrollPane(textArea);
//this.add(textArea); // get rid of this
this.add(scroll);
You create the scrollpane with the text area, but then the next statement removes the text area from the scrollpane because a component can only have a single parent.
Get rid of that statement and just add the scrollpane to the frame.
Then scrollbars will appear automatically as you add data to the text area.
Also you should create the text area using something like:
textArea = new JTextArea(5, 20);
to give a suggestion on how big to make the text area.
I did what you said but still nothing happens
Another problem is that you need to set the layout manager BEFORE you start adding components to the frame (or panel).

Remove this.add(textArea); and add scroll.setSize( 100, 100 ); will also work for you.

Related

Fix the length of java GUI Table

I want to increase the width that the table is covering On Jpanel.
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setTitle("Person Table");
jf.setSize(1100, 700);
JPanel jp1 = new JPanel();jp1.setBackground(Color.green);
JPanel jp2 = new JPanel(); jp2.setBackground(Color.red);
jp1.setSize(1100, 400);
jp2.setSize(980, 200);
JTable jt = new JTable(data,columnNames); jt.setSize(900, 350);
jt.setBackground(Color.ORANGE);
// Add table to JScrollpane
JScrollPane sp = new JScrollPane(jt); sp.setSize(1000, 380);
sp.setBackground(Color.CYAN);
jp1.add(sp);
jf.add(jp1);
jf.add(jp2);
jf.setVisible(true);
This is the output
I noticed that IF I don't use Scroll pan the Column name disappears and size increases..
But I also want the column name to appear..
Keep the scroll pane, and replace jp1 with it,
AKA Change:
jf.add(jp1);
to
jf.add(jsp);
This will make the table take up the whole green area*. If this isn't what you want, use nested layouts.
*Depending on the LayoutManager. In this case, FlowLayout is used which does not resize the component. Were you using a GridLayout or BorderLayout, the entire green area would be filled.

Java - Swing - TextArea - scrollbar

I want to have a textArea to display results that can be scrolled. The scrollbar doesn't appear even though I set it to VERTICAL_SCROLLBAR_ALWAYS
What Am I doing wrong??
void addPlayerPanel(JFrame gameFrame) {
JPanel playerPanel = new JPanel();
// automatically added to contentPane with gameFrame.add()
gameFrame.add(playerPanel, BorderLayout.CENTER);
playerPanel.setBorder(new TitledBorder(new EtchedBorder(), "Registered players"));
// text are to show registered players
JTextArea display = new JTextArea(5, 40);
display.setEditable(true); // set textArea to editable
display.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
JScrollPane scroll = new JScrollPane(display);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
// Add Text area
playerPanel.add(scroll);
playerPanel.add(display);
}
You're adding both the JScrollPane AND it's display to the GUI -- DON'T do that. Add just the JScrollPane. It holds the display and that is what you need.
So change:
playerPanel.add(scroll);
playerPanel.add(display);
to
playerPanel.add(scroll);
// playerPanel.add(display);
Question: why are you setting the layout manager of your JTextArea? That really makes little sense.

Scroll bars for JTextArea

I'm trying to create a scroll bar for my text area. However, the scroll bar isn't appearing. Can anyone give me any tips. This is the from the method which creates the panel where the scroll bar will be.
displayCD = new JPanel();
displayCD.setSize(new Dimension(500, 500));
jta = new JTextArea();
jta.setMaximumSize(new Dimension(500, 500));
scrollPane = new JScrollPane();
scrollPane.getViewport().add(jta);
displayCD.add(scrollPane);
see this example. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS property.
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Main {
public static void main(String[] argv) throws Exception {
JTextArea textArea = new JTextArea();
JScrollPane pane = new JScrollPane(textArea);
pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
}
}
Reference: http://www.java2s.com/Code/JavaAPI/javax.swing/JScrollPaneVERTICALSCROLLBARALWAYS.htm
You need to provide the textArea to the constructor of the scrollpane. Please check this link, for more information: http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html.
JScrollPane scrollPane = new JScrollPane(textArea);
Check out How to Use Scroll Panes. Here follows their example:
//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);
Try to pass the JTextArea in the JScrollPane's constructor, and, most of all, try to give it meaningful size hints (rows and columns).
Applying this to your specific case:
jta = new JTextArea(5, 30);
scrollPane = new JScrollPane(jta);
displayCD = new JPanel();
displayCD.add(scrollPane);
The scroll bar should appear, if your text contents exceed the default dimensions. To always show the bars, try the setHorizontalScrollBarPolicy and setVerticalScrollBarPolicy methods, or other JScrollPane constructors.
Regarding the size of the text area:
Unless you explicitly set a scroll pane's preferred size, the scroll pane computes it based on the preferred size of its nine components (the viewport, and, if present, the two scroll bars, the row and column headers, and the four corners). The largest factor, and the one most programmers care about, is the size of the viewport used to display the client.

How to make a JTextArea scrollable but still have a height set?

I have a Java application that connects to a device and shows the log in a JTextArea. I want the JTextArea to be scrollable, which I have achieved by putting it inside a JScrollPane. The JScrollPane containing the JTextArea is in the CENTER part of a BorderLayout. I use pack() to set the JFrame's size just before it is shown. However, this has some problems:
With no height set for the JTextArea it is very thin, and the text can't be seen very well (in the second picture there is actually text):
With a preferred size set for the JTextArea it seems to work fine at first. But when there is more text the scrollbars do not appear as expected. They appear when the JFrame is resized down regardless of the amount of text in the JTextArea. This also doesn't show all the text via scrolling.
Also, setting a minimum height doesn't help; it leads to the same result as in the first case.
My code for initializing the frame:
JButton connectBtn, disconnectBtn;
JTextArea logArea;
public MyApplication() throws HeadlessException {
super();
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new MyWindowListener()); // Disconnect and exit on close
setLayout(new BorderLayout());
connectBtn = new JButton("Connect");
disconnectBtn = new JButton("Disconnect");
disconnectBtn.setEnabled(false);
connectBtn.addActionListener(new ConnectListener()); // Connects to device
disconnectBtn.addActionListener(new DisconnectListener()); // Disconnects from device
logArea = new JTextArea();
logArea.setEditable(false);
// Whatever fixes the problem goes here... e.g.
// logArea.setPreferredSize(new Dimension(100, 200));
JPanel buttons = new JPanel(new BorderLayout());
buttons.add(connectBtn, BorderLayout.LINE_START);
buttons.add(disconnectBtn, BorderLayout.LINE_END);
add(buttons, BorderLayout.PAGE_START);
add(new JScrollPane(logArea), BorderLayout.CENTER);
pack();
setVisible(true);
}
Don't play with the sizes.
Specify the rows/column that you want in the text area and the text area will calculate its own preferred size.
//logArea = new JTextArea();
logArea = new JTextArea(5, 20);

JTextArea that needs to grow with parent panel

I want to make my JTextArea field as big as it can be in current JPanel. How to do that?
Now it is like this:
JPanel statusBar = new StatusBar(project);
JTextArea outputBox = new JTextArea(1, 50);
outputBox.setEditable(true);
statusBar.add(outputBox);
The default layout manager of JPanel is FlowLayout, which wouldn't let the text area fill the entire available space in the panel.
Using BorderLayout should work well:
statusBar.setLayout( new BorderLayout() );
JTextArea outputBox = new JTextArea(1, 50);
outputBox.setEditable(true);
statusBar.add(outputBox, BorderLayout.CENTER);
You need a layout manager on the JPanel. If its just the JTextArea contained within it and you need to maximise it you can use a simple GridLayout:
statusBar.setLayout(new GridLayout(1,1));

Categories

Resources