Hello Im trying to create a JTextArea which once the data gets to large to hold the data the scroll bars can allow me to scroll however ATM there are no scroll bars just a little square box and the data is pushing the rest of the GUI up.
This is the code for the textArea and the scrollbars, p3 is the panel im trying to add the scrollbar and textArea too.
//TEXT AREA
final JTextArea dataTextField = new JTextArea();
dataTextField.setSize(750,200);
dataTextField.setLineWrap(true);
dataTextField.setEditable(false);
dataTextField.setVisible(true);
JScrollPane scroll = new JScrollPane (dataTextField);
scroll.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVisible(true);
p3.add(dataTextField);
p3.add(scroll);
//p3.add(dataTextField);
p3.add(scroll);
A swing component can only have a single parent. When you add the dataTextField to the panel you remove it from the scrollpane. Get rid of that line of code.
Do not use setSize (750, 200) on dataTextField. Just pass number of columns and rows to constructor of JTextArea.
Related
JLabel newLabel = new JLabel();
String a = b;//b is String from database.
newLabel.setText(a);
I need to generate text pulled from my database which contains multiple line, but when i put them into the label, all of the text became same line.
I tried using JTextArea and it works, however, for some reason it messed with all the other component's alignment in a 1 column BoxLayout panel... which I wanted all the content to be aligned to the left. Any help would be much appreciated! thanks
however, for some reason it messed with all the other component's alignment in a 1 column BoxLayout panel
The is related to the setAlignmentX(...) method of each component.
A JLabel uses 0.0f (for left alignment). A JTextArea and JScrollPane use 0.5f (for center alignment).
Using components with different alignments will cause alignment issues when using a BoxLayout.
The solution is to change the alignment of the text area or scroll pane (whichever component you add to the BoxLayout).
So the basic code would be:
JLabel label = new JLabel(...);
JTextArea textArea = new JTextArea(...);
textArea.setAlignmentX(0.0f);
JPanel panel = new BoxLayout(...);
panel.add(label);
panel.add(textArea);
Now both components will be left aligned.
I want to display line numbers in a text component. I found this link
https://tips4java.wordpress.com/2009/05/23/text-component-line-number/
And it worked. But I want to display line number on the right side of textarea. How can I do it. Thank!
TextLineNumber is a Swing component. How do you display multiple components in a scroll pane? You add the components to a panel and then add the panel to the viewport of the scroll pane. One way might be to use a panel with a BorderLayout:
JPanel panel = new JPanel( new BorderLayout() );
panel.add(textArea, BorderLayout.CENTER);
TextLineNumber lineNumber = new TextLineNumber(textArea, 3);
panel.add(lineNumber, BorderLayout.EAST);
JScrollPane scrollPane = new JScrollPane( panel );
Or you could use the existing code and change the orientation of the scroll pane:
scrollPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
The line number will be on the right, but the scrollbar will now be on the left.
For my Java program I am actually using the simple library TableLayout as layout for my main JPanel body so that I can add any widget just by specifying its row and column index, for example"
body.add(new JLabel(
"Search by date"),
"1,8");
Now I would need to add two JScrollPane (one horizontal and one vertical) but they should include all the body and not just a single cell of the layout. Shall I add another JPanel? How can I do it?
Now I would need to add two JScrollPane (one horizontal and one
vertical) but they should include all the body and not just a single
cell of the layout. Shall I add another JPanel?
IMO, yes you should. Nesting Layouts is a common approach that could be applied in this way:
Create a new JScrollPane and set your panel as its viewport view.
Give the scroll pane a reasonable preferred size to enable the scroll bars if your panel's size exceeds this preferred size.
Have a wrapper panel with BorderLayout and add the scroll pane to its CENTER location.
In a nutshell:
JScrollPane scrollPane = new JScrollPane(yourPanel);
scrollPane.setPreferredSize(new Dimension(400, 300));
JPanel wrapperPanel = new JPanel(new BorderLayout());
wrapperPanel.add(scrollPane);
See also:
How to Use Scroll Panes
I made a JScrollPane and put a JTextArea in it. Then I added the ScrollPane to the frame. That works fine. However, When text overflows off the JTextArea, the ScrollPane will not scroll at all. Please help. Ive included a picture (The picture shows that there is text off the pane, but I cant see it, so im trying to scroll down, but its now allowing me to).
JTextArea console = new JTextArea(35, 70);
JScrollPane sp = new JScrollPane(console, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
I have been trying to use JScrollPane with my applet, but it doesn't work. I have a JPanel to which I add 20 buttons, and I want to be able to scroll up and down this JPanel. Instead, the scrollbars do not appear. When I use setPreferredSize they still did not appear even though only about 3 of the buttons are being displayed and the rest are cut off. If I do not use setPreferredSize, there might as well not be any scrollbars because I have to make the window big enough to see all of the buttons. If I try to make the scrollbars always visible, they appear but do nothing. I tried the exact same code with JFrame instead of Applet, and it works fine, but I need it to be an applet. Is JScrollPane incompatible with applets? (Note: I tried to use an outer JPanel and add the scrollable panel to it, but it changed nothing). Changing the layouts also doesn't fix the problem. I have attached a simplified version of my code, but it displays the same errors.
Here is the code I have:
JPanel scrollPanel = new JPanel();
scrollPanel.setLayout(new BoxLayout(scrollPanel, BoxLayout.PAGE_AXIS));
JScrollPane scroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
for (int i = 0; i < 20; i++) scrollPanel.add(new JButton("Button " + i));
add(scrollPanel);
validate();
You never all the panel to the scroll pane
You never add the scroll pane to the applet
The basic code should be:
JScrollPane scrollPane = new JScrollPane(...);
scrollPane.setViewportView( scrollPanel );
add( scrollPane );
You are adding components to a Panel so you shouldn't expect to see a scroll pane wihout showing the scrollpane. What you want to do is then add that panel to a scrollpane which would be added to ur main container.
From your code, i think your problem is
add(scrollPanel);
your should be doing this
add(scroll);`
This is because you only added the panel to the frame which does not contain any scrollpane. Since you have added the panel unto the scrollpane, you should add the scrollpane and not the panel to the main container.
It sounds like you are using Swing components (JScrollPane, JPanel, ...) in an AWT container (Applet). Try using JApplet instead.