I am trying to add JScrollPane to my large JTextArea, but whenever I include the JScrollPane code, my whole JTextArea disappears.
public myGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1174, 656);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
contentPane.setVisible(true);
JTextArea textArea_1 = new JTextArea();
textArea_1.setBounds(203, 5, 869, 440);
textArea_1.setEditable(true);
textArea_1.setVisible(true);
contentPane.add(textArea_1);
JScrollPane scroll = new JScrollPane (textArea_1);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
contentPane.add(scroll);
}
Several problems with your code:
You're trying to add a component, here your JTextArea called textArea_1, to multiple containers, here the contentPane and the JScrollPane. You can't do this in Swing as a component can be added to only one component. Add it only to the JScrollPane and not to the contentPane, and then add the scroll pane to the GUI.
You're constraining the size of your JTextArea via setBounds which almost guarantees that the JScrollPane will not work since doing this prevents the JTextArea from expanding when it holds more text than is shown. Instead set the JScrollPane's rows and columns properties and not its bounds. This will be the number of rows and columns that it should display within the scrollpane
You're using null layouts but not specifying the size of the JScrollPane, and so it defaults to a size of [0, 0] -- and this is why your JTextArea disappears. Null layouts require complete specification of all component sizes and positions.
You're using null layouts to set up your GUI. While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.
For example:
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.*;
#SuppressWarnings("serial")
public class MyGuiPanel extends JPanel {
// some row and column values for our JTextArea
private static final int TXT_AREA_ROWS = 25;
private static final int TXT_AREA_COLS = 80;
// create the JTextArea, passing in the rows and columns values
private JTextArea textArea = new JTextArea(TXT_AREA_ROWS, TXT_AREA_COLS);
public MyGuiPanel() {
// create the JScrollPane, adding our JTextArea
JScrollPane scroll = new JScrollPane(textArea);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
// lets add some buttons to the bottom of the GUI just for fun
JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 0));
buttonPanel.add(new JButton("Save"));
buttonPanel.add(new JButton("Open"));
buttonPanel.add(new JButton("Delete"));
buttonPanel.add(new JButton("Exit"));
// let's add a title to the top:
JLabel title = new JLabel("This is my Applications's Title", SwingConstants.CENTER);
title.setFont(title.getFont().deriveFont(Font.BOLD, 24)); // and make
// the text
// *BIG*
// use a BorderLayout for our GUI
setLayout(new BorderLayout(5, 5));
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
add(scroll, BorderLayout.CENTER); // add the scrollpane to the center
add(buttonPanel, BorderLayout.PAGE_END); // the button panel to the
// bottom
add(title, BorderLayout.PAGE_START); // and the title JLabel to the top
}
private static void createAndShowGui() {
// create our GUI JPanel
MyGuiPanel mainPanel = new MyGuiPanel();
// create a JFrame to add it to
JFrame frame = new JFrame("My GUI");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel); // add the GUI to the JFrame
frame.pack(); // tell the layout managers to do their work
frame.setLocationByPlatform(true);
frame.setVisible(true); // display the GUI
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
Also, if your program extends JFrame, understand that you are painting yourself in a corner by doing this, forcing you to create and display JFrames, when often more flexibility is called for. In fact, I would venture that most of the Swing GUI code that I've created and that I've seen does not extend JFrame, and in fact it is rare that you'll ever want to do this. More commonly your GUI classes will be geared towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. This will greatly increase the flexibility of your GUI coding.
You do not need setBounds for your JTextArea. Because you are using a null layout and the JScrollPane has no bounds, nothing shows up. Your JTextArea is also added to two places which would cause some problems. I would recommend any of swings layout managers. As an example using BorderLayout which is one of the easiest managers:
public mygui() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(700, 500);
setLayout(new BorderLayout());
JTextArea textArea_1 = new JTextArea();
textArea_1.setEditable(true);
JScrollPane scroll = new JScrollPane(textArea_1);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
add(scroll, BorderLayout.CENTER);
}
Related
I read some answered questions in this forum (this one for example) where it is strictly recommended to avoid the use of setXXXSize() methods to resize components in swing applications.
So, coming to my problem, i would like to know how to best resize a JScrollPane in order to avoid its parent panel to increase its size without any control.
Before writing some code, i want to describe the real situation, since i will post a "toy example".
In my JFrame i'm currently using a border layout for my content pane. At BorderLayout.CENTER there is a JPanel where i do some custom painting.
At BorderLayout.EAST there is a JPanel (say eastPanel) containing some components inside another panel (this panel will be added to eastPanel at BorderLayout.NORTH), and a JScrollPane which contains a JTable (added to eastPanel at BorderLayout.CENTER). This table will have a lot of rows.
Since i want eastPanel's height to be the same as centerPanel's height, i need some way to avoid the JScrollPane to increase its size in order to try to display as much rows as possible.
For now i wasn't be able to find another solution apart from calling setPreferredSize on the eastPanel containing the scrollpane, but i have to admit that i hate this kind of solution.
Sample Code
In this code sample i added some random labels at the north of eastPanel and inside the JScrollPane, since my purpose was to post a short sample of code.
However, the situation is very similar to the one i have described above.
I wasn't be able to solve my problem without using this "terrible" line of code :
eastPanel.setPreferredSize(new Dimension(eastPanel.getPreferredSize().width, centerPanel.getPreferredSize().height));
I would like to avoid a more complex layout for a simple situation like this. Am i missing something ? Also, is setting that empty border an acceptable way to set the size of the panel where i will do some custom painting?
Code :
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class Test
{
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new TestFrame().setVisible(true);
}
catch(Exception exception) {
JOptionPane.showMessageDialog(null, "Fatal error while initialiing application", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
}
}
class TestFrame extends JFrame
{
public TestFrame() {
super("Test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel pane = new JPanel(new BorderLayout(20, 0));
pane.setBorder(new EmptyBorder(20, 20, 20, 20));
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.WHITE);
centerPanel.setBorder(new EmptyBorder(400, 400, 0, 0));
// centerPanel.setPreferredSize(new Dimension(400, 400));
JPanel eastPanel = new JPanel(new BorderLayout(0, 20));
JPanel labelsContainer = new JPanel(new GridLayout(0, 1));
for(int i=0;i<7;i++) labelsContainer.add(new JLabel(String.valueOf(i)));
eastPanel.add(labelsContainer, BorderLayout.NORTH);
JPanel moreLabelsContainer = new JPanel(new GridLayout(0, 1));
for(int i=7;i<70;i++) moreLabelsContainer.add(new JLabel(String.valueOf(i)));
JScrollPane scroll = new JScrollPane(moreLabelsContainer, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
eastPanel.add(scroll, BorderLayout.CENTER);
eastPanel.setPreferredSize(new Dimension(eastPanel.getPreferredSize().width, centerPanel.getPreferredSize().height));
pane.add(centerPanel, BorderLayout.CENTER);
pane.add(eastPanel, BorderLayout.EAST);
setContentPane(pane);
pack();
setLocationRelativeTo(null);
}
}
Thanks for your help !
I am not aware of a layout manager that restricts the height of the panel based on the height of a specific component in the panel.
One way is to customize the behaviour of the parent panel that contains the two child components.
The code might be something like:
#Override
public Dimension getPreferredSize()
{
Dimension d = super.getPreferredSize();
BorderLayout layout = (BorderLayout)getLayout();
Component center = layout.getLayoutComponent(BorderLayout.CENTER);
int centerHeight = center.getPreferreidSize().height;
if (d.height > centerHeight)
d.height = centerHeight;
return d;
}
This approach will allow for dynamic calculation of the height based on the component in the center.
Another option is to write you own layout manager. Then you can control this type of logic from within the layout manager.
Also, is setting that empty border an acceptable way to set the size of the panel where i will do some custom painting?
I override the getPreferredSize() to return the appropriate dimension.
By using the EmptyBorder you lose the ability to add a true Border to the panel, so I wouldn't recommend it.
I have a JPanel with scrollbar and i want to add a lot of JLabels to it. But the scrollbar doesnt work . I can not use the scrollbar and even after the panel is full it doesn't scroll . Here is my code :
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Scroll {
public static void main(String[] args) {
JPanel panel = new JPanel(new GridLayout(0,1));
JPanel p = new JPanel(new BorderLayout());
JScrollPane scroll = new JScrollPane(panel);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
p.add(scroll, BorderLayout.NORTH);
JButton but = new JButton("OK");
p.add(but, BorderLayout.SOUTH);
but.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
panel.add(new JLabel("Some random text"));
scroll.revalidate();
p.repaint();p.revalidate();
}
});
JFrame frame = new JFrame();
frame.setSize(800,200);
frame.setVisible(true);
frame.add(p);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
}
}
Your problem seems to lie with your layout managers. I think BorderLayout behaves strangely when you don't use the BorderLayout.CENTER position. I changed the line
p.add(scroll, BorderLayout.NORTH);
to
p.add(scroll, BorderLayout.CENTER);
Then, to make the text appear from the top instead of centering, I changed the layout manager for the panel component to a BoxLayout. From:
JPanel panel = new JPanel(new GridLayout(0, 1));
to
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
This seems to have given me the functionality you want. Let me know if this fixes your issues or not!
As already suggested, you should probably use JList or JTable to implement your use case.
Regarding the issue, this is because all BorderLayout constraints except of CENTER will expand to as much space as their components occupy, even if that means that they will expand out of the screen bounds (in your case the NORTH section expands to the south after each button click).
To solve this, explicitly specify the preferred size for the components which can grow indefinitely if you add them to a non-central panel section with BorderLayout:
scroll.setPreferredSize(new Dimension(-1, 100));
I use -1 for the width here to indicate that it is not important (I could use any other value), since it will be ignored by the layout manager anyway (with BorderLayout.NORTH the component is stretched horizontally to take all the available horizontal space).
I am trying to create a JPanel that is resizable & scrollable and contains x smaller inner panels. Each inner panel can be as wide as it wants/needs. BUT the depth should be a preferred size.
Like:
So far my code is:
public class TestSize {
public static void main(String[] args) {
JFrame F = new JFrame();
F.setVisible(true);
JPanel P = new JPanel();
P.setLayout(new BorderLayout());
JScrollPane scrollPane = new JScrollPane(P);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setPreferredSize(new Dimension(900,900));//.setBounds(50, 30, 300, 50);
JPanel S = new JPanel();
S.setBackground(Color.GREEN);
S.setPreferredSize(new Dimension(900,200));
JPanel S2 = new JPanel();
S2.setBackground(Color.GREEN);
S2.setPreferredSize(new Dimension(900,200));
P.add(S,BorderLayout.NORTH);
P.add(S2,BorderLayout.NORTH);
F.add(scrollPane);
F.pack();
F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
But when I have added a single inner panel it fills all the space vertically - which is not what I want:
//P.setLayout(new BorderLayout());
Why did you set the layout to a BorderLayout? You can only add 1 component to the NORTH. Is that what you want? Read the section from the Swing tutorial on Using Layout Managers and pick a more appropriate layout manager. Bookmark the tutorial link as it provides the basics for Swing programming.
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
Why did you set these properties? These are the default values.
scrollPane.setPreferredSize(new Dimension(900,900));
Why would you set the height to be 900. You only want the scrollpane to contain components of height 200? In general you should NOT be setting the preferred size. Let the frame.pack() method do its job.
S.setBackground(Color.GREEN);
Why would you set the background color of both panels to be the same. How can you tell if the two panels get added? Make them different color for an easy visual.
In BoxLayout, there are different methods of using invisible components as filler. I don't think you will want to use a "rigid area", since I think you want to have a variable number of inner panels. You may want to try using vertical glue or custom Box.Filler.
Another solution might be to put a JPanel between your JFrame and your ScrollPane that uses a BorderLayout, and put the scrollpane in the BorderLayout.NORTH of that panel. Components in BorderLayout.NORTH get resized horizontally, but they do not get resized vertically. Essentially, they just get pushed to the top of the panel.
Edit:
I think you will want something like this:
JFrame F = new JFrame();
F.setVisible(true);
F.setLayout(new BorderLayout());
JPanel P = new JPanel(new BoxLayout(P, BoxLayout.PAGE_AXIS));
JScrollPane scrollPane = new JScrollPane(P);
JPanel S = new JPanel();
S.setBackground(Color.GREEN);
JPanel S2 = new JPanel();
S2.setBackground(Color.BLUE);
P.add(S);
P.add(S2);
F.add(scrollPane, BorderLayout.NORTH);
F.pack();
F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
You said you want it resizeable, so I don't see why you would set the preferred size of the frame. This way it will just fit to the panels inside.
What kind of layout should I use to create a page Like this:
It should be resizable
It has two main panels Right and Left?
Extra space will be given to the 'Main Text' text area, and extra height will be given to the button panel while centering them.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class EndOfLineButtonLayout {
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
// the GUI as seen by the user (without frame)
JPanel gui = new JPanel(new BorderLayout());
gui.setBorder(new EmptyBorder(2, 3, 2, 3));
JPanel textPanel = new JPanel(new BorderLayout(5,5));
textPanel.add(new JScrollPane(new JTextArea("Top Text",3,20)),
BorderLayout.PAGE_START);
textPanel.add(new JScrollPane(new JTextArea("Main Text",10,10)));
gui.add(textPanel, BorderLayout.CENTER);
JPanel buttonCenter = new JPanel(new GridBagLayout());
buttonCenter.setBorder(new EmptyBorder(5,5,5,5));
JPanel buttonPanel = new JPanel(new GridLayout(0,1,5,5));
for (int ii=1; ii<6; ii++) {
buttonPanel.add(new JButton("Button " + ii));
}
// a component added to a GBL with no constraint will be centered
buttonCenter.add(buttonPanel);
gui.add(buttonCenter, BorderLayout.LINE_END);
JFrame f = new JFrame("Demo");
f.add(gui);
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See http://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}
You can use gridbag layout, try using netbeans, I have tried it and found really usefull.
Once you create it with netbeans you can use the same and build infact any kind of layout.
best of luck with other solutions.
p.s. border layout is perfect for your requirement, but I mentioned this just in case you would like to do lot more .
I would use BorderLayout.
Create Three JPanels and add them to a JFrame as follows:
public class YourClass extends JFrame{
//code here
this.setLayout(new BorderLayout());
this.add(TopPanel, BorderLayout.NORTH);
this.add(RightPanel, BorderLayout.EAST);
this.add(MainPanel, BorderLayout.CENTER);
this.pack();
this.setVisible(true);
The two main panels would be placed inside a main JPanel using a BorderLayout. The left panel would be placed using BorderLayout.CENTER, and the right panel would be placed using BorderLayout.LINE_END.
The left panel would use a BoxLayout, Y axis to separate the two JPanels within the left panel.
The right buttons panel would use a GridBagLayout. This sizes the buttons the same and allows you to use Insets to add some spacing to the buttons.
The buttons would be spaced from the top to the bottom of the right buttons panel. If you want all the buttons towards the top of the right buttons panel, you would put the right buttons panel inside of another JPanel using a FlowLayout.
I have a JTextField, and right below it I want to show a JLabel placed in a JLayeredPane (I will use it for autosuggestions later on).
How can I place my JLabel in JLayeredPane right below the JTextField?
Here is some code I have, and the current result shown in the screenshot below:
public static void main(String[] args) {
JTextField field = new JTextField();
JLabel lbl = new JLabel("Hello");
lbl.setBackground(Color.YELLOW);
lbl.setOpaque(true);
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setLayout(new GridLayout(0,1));
layeredPane.add(lbl, (Integer) (JLayeredPane.POPUP_LAYER - 10));
layeredPane.setPreferredSize(field.getPreferredSize());
JPanel panel = new JPanel(new BorderLayout());
panel.add(field, BorderLayout.NORTH);
panel.add(layeredPane, BorderLayout.SOUTH);
JFrame frame = new JFrame();
frame.add(panel);
frame.setSize(200, 360);
frame.setVisible(true);
}
Second try:
public static void main(String[] args) {
JTextField field = new JTextField();
JLabel lbl = new JLabel("Hello");
lbl.setBackground(Color.YELLOW);
lbl.setOpaque(true);
lbl.setBounds(field.getBounds().x, field.getBounds().y,
field.getBounds().width, field.getBounds().height);
JPanel popPanel = new JPanel(new BorderLayout());
popPanel.add(lbl, BorderLayout.NORTH);
popPanel.setLocation(field.getLocation().x+10, field.getLocation().y+20);
popPanel.setPreferredSize(field.getPreferredSize());
JFrame frame = new JFrame();
JLayeredPane layeredPane = frame.getRootPane().getLayeredPane();
layeredPane.setLayout(new GridLayout(0,1));
layeredPane.add(popPanel, (Integer) (JLayeredPane.POPUP_LAYER - 10));
JPanel panel = new JPanel(new BorderLayout());
panel.add(field, BorderLayout.NORTH);
frame.add(panel);
frame.setSize(200, 360);
frame.setVisible(true);
}
Add the layeredPane to the "CENTER", not the SOUTH.
However, your understanding a layed pane seems to be a little confused. You use a layered pane when you want multiple components to be displayed on top (stacked?) of one another. You are still using the layered pane in 2 dimensions which is unnecessary. YOu can just use a panel for this.
If you want to popup a list of suggestions then you should just use a JPopupMenu and position it below the text field. Read the section from the Swing tutorial on Bringing up Popup Menus.
First of all, I don't think you should use a JLayeredPane for that, but just a permanent label.
If you do use a layered pane, you'll have to compute where the text field ends (y = field.getY() + field.getHeight()) and set your JPanel at 'panel.setLocation(0, y)' inside the JLayeredPane (provided the JLayeredPane has the same starting position as the underlying JFrame). You could equivalently position the JLayeredPane at (0, y) and put the label at (0, 0) within that layered pane.
You have to make sure this is done every time the components are resized.
why not using AutoComplete ComboBox / JTextField and if you don't want to display JComboBox, then there is AutoCompleted JTextField, and for somehow reduced autosuggestions, would be better look for undecorated JDialog/Window with JTable with one TableColum and without TableHeaded in the JScrollPane, just with plain RowSorter, very simle job