JTextArea to ScrollPane not working - java

The Scrollbar does not appear in the Frame and the TextArea is somehow not editable, please help, thanks :)
import javax.swing.*;
import java.awt.*;
public class Test extends JFrame{
Container c;
JTextArea jT;
JScrollPane scroll;
public Test(){
c = getContentPane();
c.setLayout(new GridLayout(1,1));
jT = new JTextArea();
scroll = new JScrollPane(); //creating JScrollPane
scroll.add(jT); // adding jT to scroll
c.add(scroll);
}
public static void main(String[] args){
Test fenster = new Test();
fenster.setLocationRelativeTo(null);
fenster.setTitle("Test");
fenster.setSize(200, 200);
fenster.setVisible(true);
fenster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

You need initialize the Scroll Pane with the component for which you need to display the scroll bars.
scroll = new JScrollPane(jT); //creating JScrollPane; Do this
// scroll.add(jT); // don't do this

Related

JTabbedPane with JTextAreas. Automatically scroll down

I´ve got a JTabbedPane with JTextAreas as Components. With another methode I add Texts to the TextAreas. But if the TextArea is full it doesnt scroll down, so I can´t see the latest texts. How can I solve this?
public class View extends JFrame{
public class Field extends JTextArea{
public Field(){
this.setEditable(false);
this.setLineWrap(true);
DefaultCaret caret = (DefaultCaret)this.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
}
}
public View(){
this.setLayout(new BorderLayout());
this.tabs = new JTabbedPane();
this.tabs.add("abc", new Field());
this.add(tabs, BorderLayout.CENTER);
}
}
You need to put your JTextArea inside of a JScrollPane to make it scrollable:
import javax.swing.*;
import javax.swing.text.DefaultCaret;
import java.awt.*;
public class Example {
public static void main(String[] args) {
JFrame jFrame = new JFrame();
JTextArea textArea = new JTextArea();
DefaultCaret caret = (DefaultCaret)textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
JScrollPane jScrollPane = new JScrollPane(textArea);
jScrollPane.setPreferredSize(new Dimension(300, 300));
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("abc", jScrollPane);
jFrame.setContentPane(tabbedPane);
jFrame.pack();
jFrame.setVisible(true);
for(int i = 0; i < 100; i ++) {
textArea.append(i + "\n");
}
}
}

Why does the JScrollPane not appear around my JTextField?

I'm pretty new to GUI but I'm trying to create a simple version of notepad and would like scroll bars to appear around the text area. However, I'm not sure why it isn't appearing.
public class NutPad extends JPanel {
public static void main(String[] args) {
JFrame frame = new JFrame("NutPad");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new NutPad(), BorderLayout.CENTER);
frame.setSize(500,300);
frame.setVisible(true);
}
private NutPad() {
add(makeTextAreaPanel());
}
private JPanel makeTextAreaPanel() {
JPanel textAreaPanel = new JPanel();
textAreaPanel.setSize(100,100);
JTextArea textArea = new JTextArea(20, 60); //15,43
JScrollPane scrollPane = new JScrollPane(textArea);
textAreaPanel.add(scrollPane,BorderLayout.CENTER);
textAreaPanel.add(textArea);
return textAreaPanel;
}
}
Thanks
If you're going to use the BorderLayout.CENTER constraint, then the container needs to have its layout set to BorderLayout.
Also you don't need textAreaPanel since you can just add the scrollPane straight into your NutPad panel.
private NutPad() {
setLayout(new BorderLayout());
add(makeScrollPane(), BorderLayout.CENTER);
}
private JScrollPane makeScrollPane() {
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
return scrollPane;
}
Now your text area will fill the frame and the scrollbars will appear when the text takes up more than the available space.
Hope that helps :)

Java TextArea and JScrollPane

I have searched about attaching a scrollbar to a textarea and found some answers. Tried to fix as it says at suggestions but it still seems not to work.
My text area gets longer and longer just, even though I tried to make a static text area.
Wonder what I'm doing wrong here:
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
public class Ovning20 extends JFrame {
private JPanel p1 = new JPanel();
private JTextArea ta1;
JScrollPane scroll;
public Ovning20()
{
setSize(400,200);
setLocation(500, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
ta1 = new JTextArea();
ta1.setEditable(true);
ta1.setBorder(new TitledBorder(new EtchedBorder(), "Skriv in något:"));
scroll = new JScrollPane(ta1, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
ta1.setAutoscrolls(true);
ta1.setColumns (20);
ta1.setRows(3);
ta1.setLineWrap (true);
ta1.setWrapStyleWord (true); //default
p1.add(ta1);
p1.add(scroll);
add(p1);
setVisible(true);
}
public static void main(String[] args) {
new Ovning20();
}
}
Remove the statement
p1.add(ta1);
which is adding the textarea to panel p1 and effectively removing it from its scrollpane parent (since components can only have one parent component)

JScrollPane won't work

Hey guys I wanted to create a JScrollPane but it won't work... and I don't know why... here's my code...
public class test extends JFrame{
public test(){
setSize(1000,600);
}
private static JButton[] remove;
private static JPanel p = new JPanel();
public static void main(String[]args){
p.setLayout(null);
JFrame t=new test();
remove = new JButton[25];
for(int i=0;i<25;i++){
remove[i]=new JButton("Remove");
remove[i].setBounds(243,92+35*i,85,25);
p.add(remove[i]);
}
JScrollPane scrollPane = new JScrollPane(p);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
t.add(scrollPane);
t.setVisible(true);
}
Umm and Im pretty sure the frame isn't big enough for these 25 buttons... But if i delete that p.setLayout(null); A horizontal scroll bar will be created automatically... I don't really know what is wrong with my code... Pls help thank you very much!
You need to set p's preferredSize for this to work.
p.setPreferredSize(new Dimension(800, 2000));
Or you could have p extend JPanel and then override the getPreferredSize() method to return the proper dimension.
And I agree -- get rid of your null layouts. Learn about and use the layout managers if you want to use Swing correctly and have robust Swing GUI's.
e.g.,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Foo extends JFrame {
private static final int BUTTON_COUNT = 25;
public static void main(String[] args) {
JPanel btnPanel = new JPanel(new GridLayout(0, 1, 0, 20));
btnPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
AbstractAction removeAction = new AbstractAction("Remove") {
#Override
public void actionPerformed(ActionEvent evt) {
JButton src = (JButton) evt.getSource();
JPanel container = (JPanel) src.getParent();
container.remove(src);
container.revalidate();
container.repaint();
}
};
for (int i = 0; i < BUTTON_COUNT; i++) {
JButton removeBtn = new JButton(removeAction);
btnPanel.add(removeBtn);
}
JPanel borderPanel = new JPanel(new BorderLayout());
borderPanel.add(btnPanel, BorderLayout.NORTH);
JScrollPane scrollpane = new JScrollPane(borderPanel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollpane.setPreferredSize(new Dimension(400, 800));
JFrame frame = new JFrame("Foo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(scrollpane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
The issue is that a scroll pane checks the component inside it for a "preferred size" so a pane with a null layout has a preferred size of (0,0). Which it ignores.
You should do something along the lines of:
p.setPreferredSize(1000,600);
And you should see some scroll bars appear, I'm not sure how accurate they will be though.

Items not Appearing in New Frame in Java

I'm building a JFrame that will eventually display the output of a program that has a variable number of sections in it. I have parsed the output but displaying it in the frame is a problem.
When the frame appears, it is completely empty with the exception of the scroll pane. How do I get these labels to show up?
public class OutputPanel extends JFrame {
public OutputPanel(Vector parsedOutput) {
this.setTitle("Output");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane();
Iterator<Vector> outputIter = parsedOutput.iterator();
while(outputIter.hasNext()) {
Vector section = outputIter.next();
JLabel sectionLabel = new JLabel((String)section.get(0));
System.out.println((String)section.get(0));
scrollPane.add(sectionLabel);
}
this.add(scrollPane);
this.pack();
this.setVisible(true);
}
}
You shouldn't add components to the scrollPane
scrollPane.add(sectionLabel);
but rather add them to a separate panel, and either use
scrollPane = new JScrollPane(thePanel);
or
scrollPane.setViewportView(thePanel);
Example:
import java.awt.GridLayout;
import java.util.Vector;
import javax.swing.*;
class Test {
public static void main(String[] args) {
new OutputPanel(null);
}
}
class OutputPanel extends JFrame {
public OutputPanel(Vector parsedOutput) {
this.setTitle("Output");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel content = new JPanel(new GridLayout(0, 1));
for (int i = 0; i < 100; i++) {
JLabel sectionLabel = new JLabel("hello " + i);
content.add(sectionLabel);
}
JScrollPane scrollPane = new JScrollPane(content);
this.add(scrollPane);
this.pack();
this.setVisible(true);
}
}
Produces:
You should use setViewPortView() with a container instead of add() for JScrollPane.
Try this.
public class OutputPanel extends JFrame {
public OutputPanel(Vector parsedOutput) {
this.setTitle("Output");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane();
Iterator<Vector> outputIter = parsedOutput.iterator();
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
scrollPane.setViewportView(panel);
while(outputIter.hasNext()) {
Vector section = outputIter.next();
JLabel sectionLabel = new JLabel((String)section.get(0));
System.out.println((String)section.get(0));
panel.add(sectionLabel);
}
this.add(scrollPane);
this.pack();
this.setVisible(true);
}
}

Categories