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)
Related
This question already has an answer here:
How to set scrollbar on TextArea in Java Swing?
(1 answer)
Closed 3 months ago.
this is the code I have so far
public class WindowHelpGui extends JFrame{
JScrollPane scroll;
//constructor
public WindowHelpGui(){
//add window title
super("Help");
//set window layout
setLayout(null);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setBounds(10, 10, 750, 550);
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
panel1.add(new JTextArea("\nDRAFT TEXT 101
\ncodnvfdinofndovndofinvinfdivnifdnvidfnviofnivnidfnviofdnvindfinv
ivondfviondfiovndfionvifdnvionivfdninfdinfivnidfovniofnviofnvifdnv
fndiovnf\nh\ne\nl\nl\no\n\nf\nr\no\nm\n\nt\nh\ne\n\no\nt\nh"+
"e\nr\n\ns\ni\nd\ne\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
nHello"));
panel2.add(new JTextArea("panel2 working"));
panel3.add(new JTextArea("panel3 working"));
JScrollPane scroll = new JScrollPane(panel1, JScrollPane.
HORIZONTAL_SCROLLBAR_NEVER, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDE
add(scroll, panel1);
tabbedPane.add("How to play", panel1);
tabbedPane.add("Seeds", panel2);
tabbedPane.add("Tools", panel3);
//scroll = new Jscro
this.add(tabbedPane);
//set size of window
setSize(800,600);
//set visibility
setVisible(true);
//set resizable
setResizable(false);
//set default close
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
when I run this I dont see any window popping up. Can someone help me please?
I have tried also tried using add(scroll, tabbedPane) but it just adds a new tab on my window.
The JTextArea must be added directly to the JScrollPane and you can add the scrollpane directly to the tabbedPane:
JTextArea textArea = new JTextArea("\nDRAFT TEXT 101\ncodnvfdinofndovndofinvinfdivnifdnvidfnviofnivnidfnviofdnvindfinvivondfviondfiovndfionvifdnvionivfdninfdinfivnidfovniofnviofnvifdnvfndiovnf\nh\ne\nl\nl\no\n\nf\nr\no\nm\n\nt\nh\ne\n\no\nt\nh" +
"e\nr\n\ns\ni\nd\ne\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nn\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nnHello");
JScrollPane scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
tabbedPane.add("How to play", scrollPane);
You set the JTextArea into the ScrollPane constructor. Here is a runnable example (read comments in code):
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
public class WindowHelpGui extends JFrame {
private static final long serialVersionUID = 34534L;
private JTextArea textArea_1;
private JTextArea textArea_2;
private JTextArea textArea_3;
private JTabbedPane tabbedPane;
//constructor
public WindowHelpGui() {
createForm();
}
private void createForm() {
//set window layout
// setLayout(null); // If possible, don't ever use a Null layout.
//set resizable
// setResizable(false); // It's nice to be able to resize from time to time.
// add window title
setTitle("Help");
// set size of window
setPreferredSize(new Dimension(800, 600));
//set default close
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// OPTIONAL - Set form on top of everything.
setAlwaysOnTop(true);
tabbedPane = new JTabbedPane();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
textArea_1 = new JTextArea();
textArea_1.setText("\nDRAFT TEXT 101\ncodnvfdinofndovndofinvinfdivnifdnvidfnviofnivnid"
+ "fnviofdnvindfinvivondfviondfiovndfionvifdnvionivfdninfdinfivnidfovniofnvio"
+ "fnvifdnvfndiovnf\nh\ne\nl\nl\no\n\nf\nr\no\nm\n\nt\nh\ne\n\no\nt\nhe\nr\n\n"
+ "s\ni\nd\ne\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
+ "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nHello");
textArea_1.setCaretPosition(0); // Set the caret to beginning of document.
textArea_2 = new JTextArea();
textArea_2.setText("Seeds Area Working");
textArea_3 = new JTextArea();
textArea_3.setText("Tools Area Working");
JScrollPane scroll_1 = new JScrollPane();
scroll_1.setViewportView(textArea_1);
JScrollPane scroll_2 = new JScrollPane();
scroll_2.setViewportView(textArea_2);
JScrollPane scroll_3 = new JScrollPane();
scroll_3.setViewportView(textArea_3);
tabbedPane.add("How to play", scroll_1);
tabbedPane.add("Seeds", scroll_2);
tabbedPane.add("Tools", scroll_3);
add(tabbedPane);
pack(); // Now you can resize or maximize the form and everything will resize with it.
setVisible(true);
// Set Screen Location (after the pack())
setLocationRelativeTo(null);
}
public static void main(String[] args) {
new WindowHelpGui();
}
}
Why JTextArea not showing in GUI ?
public class AddMovie extends JTextField {
static JFrame frame;
private JLabel description;
JTextArea movieDescription;
public JPanel createContentPane() throws IOException
{
JPanel totalGUI = new JPanel();
totalGUI.setLayout(null);
totalGUI.setBackground(Color.WHITE);
description = new JLabel("Description ");
description.setLocation(15,285);
description.setSize(120, 25);
description.setFont(new java.awt.Font("Tahoma", 0, 12));
movieDescription=new JTextArea();
movieDescription.setLocation(15,320);
movieDescription.setSize(420, 110);
JScrollPane scrollPane = new JScrollPane(movieDescription);
totalGUI.add(description);
totalGUI.add(movieDescription);
totalGUI.add(cancel);
totalGUI.add(scrollPane);
return totalGUI;
}
static void createAndShowGUI() throws IOException
{
JFrame.setDefaultLookAndFeelDecorated(true);
frame = new JFrame("New Movie");
//Create and set up the content pane.
AddMovie demo = new AddMovie();
frame.setContentPane(demo.createContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(515, 520);
frame.setLocation(480,120);
frame.setVisible(true);
}
public void setVisible(boolean b) {
// TODO Auto-generated method stub
}
}
As suggested here and here, a null layout invites trouble. Because your display centers on the description, use the JTextArea constructor that lets you specify a size in rows and columns. When you pack() the enclosing Window, it will be resized to fit the text area. I've added some ad hoc text to illustrate the effect. I've also updated the code to allow the text area to grow when resizing the frame.
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
* #see https://stackoverflow.com/a/38282886/230513
*/
public class Test {
private JPanel createPanel() {
JPanel panel = new JPanel(new GridLayout());
//panel.setBorder(BorderFactory.createTitledBorder("Description"));
JTextArea movieDescription = new JTextArea(10, 20);
panel.add(new JScrollPane(movieDescription));
movieDescription.setLineWrap(true);
movieDescription.setWrapStyleWord(true);
movieDescription.setText(movieDescription.toString());
return panel;
}
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(createPanel());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Test()::display);
}
}
totalGUI.setLayout(null);
There's the 1st problem you should solve: Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space.
Besides many other problems, it typically destroys the ability of scroll-panes to do the job they were designed for.
General tips:
There is no attribute named cancel.
Don't override methods like setVisible(boolean) then leave them empty! That is achieving nothing more than breaking functionality that works just fine as it is!
The movieDescription is added to both the totalGUI & the scroll pane. It should only be added to the scroll pane.
A white text area added to a white BG will be invisible except for the caret.
The Tahoma font will only be available in some OS'
Rather than use a JLabel for the description, you might also add the text area to a panel and set the Description text to a TitledBorder used on a panel that the text area is placed in.
Nothing in the two wethods that declare throws IOException could actually cause one.
In the createAndShowGUI method you are creating again a AddMovie object..
so if you modifiy the coda as following, it will work...
Example:
public class AddMovie extends JTextField {
private static final long serialVersionUID = 6180900736631578119L;
private JFrame frame;
private JLabel description;
private JTextArea movieDescription;
public JPanel createContentPane() {
JPanel totalGUI = new JPanel();
totalGUI.setBackground(Color.WHITE);
description = new JLabel("Description ");
description.setLocation(15, 285);
description.setSize(120, 25);
description.setFont(new java.awt.Font("Tahoma", 0, 12));
movieDescription = new JTextArea();
movieDescription.setLocation(15, 320);
movieDescription.setSize(420, 110);
JScrollPane scrollPane = new JScrollPane(movieDescription);
totalGUI.add(description);
totalGUI.add(movieDescription);
totalGUI.add(scrollPane);
return totalGUI;
}
public void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
frame = new JFrame("New Movie");
frame.setContentPane(createContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(515, 520);
frame.setLocation(480, 120);
frame.setVisible(true);
}
public static void main(String[] args) {
AddMovie am = new AddMovie();
am.createAndShowGUI();
}
}
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");
}
}
}
Hey guys my buttons and textarea will not display on JFrame when compiled, i have tried everything and searched this site but no luck. Any help would be greatly appreciated. Due to them not letting me post without more detail i am just adding this part so i can hit the submit button.
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class DataManager extends JFrame {
private String students[] = {"John Smith","Ken Hanson","Michael Li","John Andersen","Fiona Harris","Angela Lim","Bob London","Sydney Shield","Tina Gillard",
"Ross Brinns","Scott Cairns","Grant Peterson","David Power","Joshua Kane","Alan Newton","Frady Morgan","Quinn Perth"};
private int english[] = {80,52,71,61,39,62,31,46,60,26,77,40,58,38,94,90,97};
private int maths[] = {60,45,77,90,45,55,66,87,31,42,65,55,80,71,51,55,95};
private int total[];
private JButton sortNameButton;
private JButton sortTotalButton;
private JTextField searchTextField;
private JButton statisticsButton;
private JButton exitButton;
private JTextArea infoTextArea;
private JPanel jPan;
public DataManager() {
super("Data Manager ");
jPan = new JPanel();
sortNameButton = new JButton("Sort By Name");
sortTotalButton = new JButton("Sort By Total");
searchTextField = new JTextField("Search");
statisticsButton = new JButton("Statistics");
exitButton = new JButton("Exit");
infoTextArea = new JTextArea();
setLayout(new BorderLayout());
jPan.add(sortNameButton, BorderLayout.NORTH);
jPan.add(sortTotalButton, BorderLayout.NORTH);
jPan.add(searchTextField, BorderLayout.NORTH);
jPan.add(statisticsButton, BorderLayout.NORTH);
jPan.add(exitButton, BorderLayout.NORTH);
jPan.add(infoTextArea, BorderLayout.CENTER);
}
public static void main(String[] args) {
DataManager frame = new DataManager();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,600);
frame.setVisible(true);
} // End of main method.
} // End of DataManager class
You add your JButtons to the jPan JPanel but never add the jPan to anything -- it must be added to your JFrame, to this to be seen.
jPan.add(sortNameButton);
jPan.add(sortTotalButton);
jPan.add(searchTextField);
jPan.add(statisticsButton);
jPan.add(exitButton);
jPan.add(infoTextArea);
add(jPan); // don't forget this! ************
Note other problems:
You set the JFrame's layout to BorderLayout -- it's already using BorderLayout
You add components to your jPan JPanel with BorderLayout constants, but it's not using a BorderLayout.
If it were, many buttons would not be seen since many are added to the same BorderLayout position and will cover the previous component added there.
In other words, read the tutorials as you're making wrong assumptions.
Better would be something like:
// setLayout(new BorderLayout());
jPan.setLayout(new BorderLayout());
JPanel northPanel = new JPanel(); // **** to hold buttons
northPanel.add(sortNameButton);
northPanel.add(sortTotalButton);
northPanel.add(searchTextField);
northPanel.add(statisticsButton);
northPanel.add(exitButton);
jPan.add(northPanel, BorderLayout.PAGE_START);
jPan.add(infoTextArea, BorderLayout.CENTER);
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.