JTabbedPane with JTextAreas. Automatically scroll down - java

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");
}
}
}

Related

JTextArea to ScrollPane not working

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

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.

JScrollPane component change

i have a JScrollPane.
But default it displays JTextArea.
JTextArea jTextArea = new JTextArea();
JScrollPane pane = new JScrollPane(jTextArea);
so here everything is fine. But now i would like to change JScrollPane component by user action:
pane.remove(jTextArea);
pane.add(new JTable(data[][], columns[]));
pane.revalidate();
frame.repaint();
frame in my main Window. JScrollPane is added to main window with GridBagLayout.
But this doesn't work. After running action JScrollPane becomes grey.
jScrollPane.getViewport().remove/add
One alternative is to put a JPanel with a CardLayout1 into the JScrollPane, add both the components to the panel, then simply flip between the text area and table as needed.
How to Use CardLayout
Given the components might be of vastly different size, it might be better to do it this way:
JPanel with CardLayout contains many JScrollPane instances, each of which contains a single component. That will also work inherently better for a JTable.
Edited my answer after receiving one valuable suggestion by His Majesty #camickr, setViewportView(componentObject); is used to do such things.
A sample code to help your cause :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ScrollPaneExample extends JFrame
{
private JPanel panel;
private JScrollPane scrollPane;
private JTextArea tarea;
private JTextPane tpane;
private JButton button;
private int count;
public ScrollPaneExample()
{
count = 0;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationByPlatform(true);
panel = new JPanel();
panel.setLayout(new BorderLayout());
tarea = new JTextArea();
tarea.setBackground(Color.BLUE);
tarea.setForeground(Color.WHITE);
tarea.setText("TextArea is working");
scrollPane = new JScrollPane(tarea);
tpane = new JTextPane();
tpane.setText("TextPane is working.");
button = new JButton("Click me to CHANGE COMPONENTS");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (count == 0)
{
scrollPane.setViewportView(tpane);
count++;
}
else if (count == 1)
{
scrollPane.setViewportView(tarea);
count--;
}
}
});
setContentPane(panel);
panel.add(scrollPane, BorderLayout.CENTER);
panel.add(button, BorderLayout.PAGE_END);
pack();
setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new ScrollPaneExample();
}
});
}
}
Hope this might help you in some way.
Regards

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