ScrollPane in the JTextArea - java

This is part of GUI project I'm working with and I'm trying to make JScrollPane to appear in the JTextArea when the text is longer than the size of the JTextArea. It looks fine to me but the JScrollPane still doesn't show up.
JTextArea textArea = new JTextArea();
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setBounds(77, 27, 561, 146);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(380, 100));
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel panel= new JPanel()
panel.add(textArea);
Can anyone verify this peace of code?

The reason your JScrollPane isn't showing up is because you haven't added it to your GUI...
panel.add(textArea);
should be
panel.add(scrollPane);
Why one might ask? Because in this line:
JScrollPane scrollPane = new JScrollPane(textArea); we see that the JScrollPane's constructor takes in the JTextArea/etc thus removing any need to add the textArea to the GUI because the textArea is now part of the scrollPane which, in turn, should be added to the GUI.

Related

How to make frame automatically resize with window?

When I resize the window, the scroll frame doesnt resize with it and just goes out of frame.
Is there any way to fix this? Here's a snippet of the code:
TextEditor(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Emu Editor J");
this.setSize(700,550);
this.setResizable(true);
this.setLayout(new FlowLayout());
this.setLocationRelativeTo(null);
textArea = new JTextArea();
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setFont(new Font("Cascadia Code",Font.PLAIN,20));
scrollPane = new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(this.getWidth()-25,this.getHeight()-100));
// scrollPane.setSize(this.getWidth()-25,this.getHeight()-100);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
I tried allowing resizable and it didn't work
Try using BorderLayout and add the panel to it with BorderLayout.CENTER.

Java JTextArea how to apply syntax highlighting

I am trying to make a simple code editor with the JFrame library in Java and I've got all the base things working except for syntax highlighting. I've read through forums and forums but I am not able to find anything. So my question is how to change the color of certain words
An example
TextEditor(){
JTextArea textArea;
JScrollPane scrollPane;
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("foo");
this.setSize(1000,1000);
this.setLayout(new FlowLayout());
this.setLocationRelativeTo(null);
textArea=new JTextArea();
textArea=new JTextArea();
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setFont(new Font("Times New Roman",Font.PLAIN,20));
scrollPane=new JScrollPane(textArea);
scrollPane=new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(950,950));
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
}
Hey im making a IDE and i can help you :p.
Replace JTextArea by JTextPane : JTextPane can be Stylised.
For the syntax hightlight you can use the StyledDocument.

Why is my scrolling textarea in java not scrolling?

I have a frame f, panel Fpanel. and textarea j.
This is a part of my code.
The scroll does not seem to be working on my text area.
JTextArea j=new JTextArea();
j.setBounds(60,150, 400,400);
j.setMargin(new Insets(3,3,3,3));
j.setEditable ( false ); // set textArea non-editable
JScrollPane scroll = new JScrollPane(j);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
I have added
Fpanel.add(scroll);
and
f.add(Fpanel);
to my code as well but it does not seem to be scrolling.
Am I missing a piece of the code or have I written something incorrectly?
if you want to have a vertical scroll bar:
JPanel panel = new JPanel();
JScrollPane jsp = new JScrollPane(panel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
if you want to have a horizontal scroll bar:
JPanel panel = new JPanel();
JScrollPane jsp = new JScrollPane(panel, ScrollPaneConstants.HORIZONTOL_SCROLLBAR_ALWAYS, ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

Extra column with GridLayout and JScrollPane not showing - Java Swing

I am messing around with Swing and I find that some elements are not appearing in my application. Most notably the JScrollPane and for some reason there seems to be a grid column between my JTextArea and JButtons.
See the image below:
My method is as follows:
private void panels(){
JFrame frame=new JFrame("Viewing All Program Details");
JPanel panel = new JPanel(new GridLayout(1,1));
panel.setBorder(new EmptyBorder(5, 5, 5, 5));
JPanel rightPanel = new JPanel(new GridLayout(15,0,10,10));
rightPanel.setBorder(new EmptyBorder(15, 5, 5, 10));
JTextArea textArea = new JTextArea(storeAllString,0,70);
JScrollPane scrollBarForTextArea=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(textArea);
frame.add(scrollBarForTextArea);
frame.getContentPane().add(panel,BorderLayout.LINE_START);
frame.getContentPane().add(rightPanel,BorderLayout.EAST);
frame.setSize(1000, 700);
frame.setVisible(true);
rightPanel.add(saveCloseBtn);
rightPanel.add(closeButton);
}
Let's start with the obvious...
JTextArea textArea = new JTextArea(storeAllString,0,70);
JScrollPane scrollBarForTextArea=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(textArea);
This isn't how you use a JScrollPane, essentially what you've done is removed the textArea from the JScrollPane by adding it to the panel. A component can only reside within a single parent container at a time.
Instead, you should be adding the scroll pane to the panel...
JTextArea textArea = new JTextArea(storeAllString,0,70);
JScrollPane scrollBarForTextArea=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(scrollBarForTextArea);
The "extra" column is actually a sympton of the previous problem...
panel.add(textArea);
frame.add(scrollBarForTextArea); // This is the "extra" column
frame.getContentPane().add(panel,BorderLayout.LINE_START);
frame.getContentPane().add(rightPanel,BorderLayout.EAST);
The empty JScrollPane is what you are seeing. With the fixes from before, you only need to do...
frame.add(panel);
frame.getContentPane().add(rightPanel,BorderLayout.EAST);
You should, also, call setVisible after you've finished building the UI if possible

Adding a Scrollable JTextArea (Java)

I am trying to add a scroll bar to a JTextArea. Would someone please tell me what I did wrong with the code below?
JFrame frame = new JFrame ("Test");
JTextArea textArea = new JTextArea ("Test");
JScrollPane scrollV = new JScrollPane (textArea);
JScrollPane scrollH = new JScrollPane (textArea);
scrollV.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollH.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.setVisible (true);
Thank you in advance.
EDIT: I fixed the code with the Adel Boutros' advice below.
//FRAME
JFrame frame = new JFrame ("Test");
frame.setSize(500,500);
frame.setResizable(false);
//
//TEXT AREA
JTextArea textArea = new JTextArea("TEST");
textArea.setSize(400,400);
textArea.setLineWrap(true);
textArea.setEditable(false);
textArea.setVisible(true);
JScrollPane scroll = new JScrollPane (textArea);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add(scroll);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
It doesn't work because you didn't attach the ScrollPane to the JFrame.
Also, you don't need 2 JScrollPanes:
JFrame frame = new JFrame ("Test");
JTextArea textArea = new JTextArea ("Test");
JScrollPane scroll = new JScrollPane (textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add(scroll);
frame.setVisible (true);
Open design view
Right click to textArea
open surround with option
select "...JScrollPane".
A scroll pane is a container which contains another component. You can't add your text area to two different scroll panes. The scroll pane takes care of the horizontal and vertical scroll bars.
And if you never add the scroll pane to the frame, it will never be visible.
Read the swing tutorial about scroll panes.
You don't need two JScrollPanes.
Example:
JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta);
// Add the scroll pane into the content pane
JFrame f = new JFrame();
f.getContentPane().add(sp);

Categories

Resources