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.
Related
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.
I have a JScrollPane on a panel (which has many panels inside of it) and when I open the frame with the scroll pane on it, the fame is scrolled to the bottom. Is there anyway I can avoid this?
Here are some facts:
The panel the scroller is on contains multiple panels. Some of these panels have text fields. I have tried to set the carat of the text fields to 0 and this did not work.
I know there should be actual code, but when I tried to make a mock pane (as the one I am using is intertwined with a lot of code) it is not replicating the issue.
The panel that is being scrolled is being generated using a loop that generates a series of questions... so a text box, a said amount of buttons/answers, and a text box and label that shows the amount of points for each question.
The last elements of my panel that is being scrolled are a JTextArea and a JLabel.
Below is the code to declare those.
Is there anyone out there that could at least throw out an idea of what would be making the scroll pane automatically go to the bottom?
Here is the declaration of the pane and the panels inside/outside of it
JPanel newPanel = new JPanel(new BorderLayout(0, 0));
JPanel showPanel = new JPanel();
BoxLayout layout = new BoxLayout(showQuizPanel, BoxLayout.Y_AXIS);
showQuizPanel.setLayout(layout);
buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.setBackground(Color.white);
newPanel.setBackground(Color.white);
showPanel.setBackground(Color.white);
populateButtonPanel();
populateShowPanel(showPanel, buttonPanel);
populateQuestions(showPanel);
JScrollPane scrollPane = new JScrollPane(showPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.getViewport().setBackground(Color.WHITE);
scrollPane.setAlignmentX(JScrollPane.CENTER_ALIGNMENT);
scrollPane.getVerticalScrollBar().setUnitIncrement(16);
scrollPane.getVerticalScrollBar().setValue(0);
scrollPane.getViewport().setViewPosition(new Point(0,0));
newPanel.add(scrollPane, BorderLayout.CENTER);
return newPanel;
code to declare last elements on page
JPanel pPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
pPanel.setBackground(Color.WHITE);
pPanel.add(qValue);
pPanel.add(new JLabel("Points"));
qArea.add(pPanel);
qArea.add(Box.createVerticalStrut(50));
qValue.setCaretPosition(0);
Thanks!
I believe that when you add text to a text area when building the GUI, the scroll pane will scroll to make the text area visible.
So basically you need to reset the scroll pane to the top.
You can use code like the following after adding all components to the scroll pane:
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
scrollPane.getViewport().setViewPosition( new Point(0, 0) );
}
});
The invokeLater() adds the code to the end of the Event Dispatch Thread (EDT) so that it gets executed after the GUI is visible.
Of course this code assumes that you are creating the rest of the GUI properly on the EDT.
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);
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));
INTRO:
I created a java application using JFrame. I have a JMenuBar at the top and under that I'd like to display rows of text.
PURPOSE:
When I have 50 rows and only 20 are displayable at once, I'd like to be able to scroll down and back up again.
PROBLEM:
Of course, my theory doesn't wanna work as it should. My problem is that I don't know how to add a vertical scroll properly.
QUESTION:
How should I change this code to reach my goal?
public void display(){
Container content = this.window.getContentPane();
content.setLayout(new BorderLayout());
Border border = LineBorder.createGrayLineBorder();
//this is just a sample
for(int i = 0;i<50;i++){
JLabel lab = new JLabel("lonyaladek");
lab.setSize(570, 20);
lab.setBorder(border);
lab.setLocation(10, 20+(i*25));
content.add(lab);
}
//scroll
JScrollBar sb = new JScrollBar(JScrollBar.VERTICAL, 0, 0, 0, 0);
content.add(sb);
}
First you need to start with a layout manager that allows you to add multiple components to the container. Maybe a GridLayout is the best place to start.
Then you add this container to the scrollPane and then you add the scrollpane to the window.
So the basic code would be:
JPanel panel = new JPanel( new GridLayout(0, 1) );
panel.add(...);
panel.add(...);
JScrollPane scrollPane = new JScrollPane( panel );
window.add(scrollPane, BorderLayout.CENTER);
I suggest you read the section from the Swing tutorial on How to Use Scroll Panes for more info.