JPanel with SpringLayout is not visible - java

I am getting insane! I am adding a JPanel to an another JPanel by a method. This method is generating a grid via SpringLayout. The problem is that after adding all JComponents to the panel the preferred size of the panel is 0, thus no content is displayed. Using an another layout manager solves the visibility but I need the structures of the SpringLayout. I can't find my problem.
private JPanel panelErzeugen(ArrayList<JComponent> labels){
JPanel panel = new JPanel();
SpringLayout layout = new SpringLayout();
panel.setLayout(layout);
int abstandVert = 5;
int abstandHori = 5;
for(JComponent label : labels){
if(labels.indexOf(label) == labels.size()/2){
abstandVert = 100;
abstandHori = 5;
}
layout.putConstraint(SpringLayout.WEST, panel, abstandVert, SpringLayout.WEST, label);
layout.putConstraint(SpringLayout.NORTH, panel, abstandHori, SpringLayout.NORTH, label);
panel.add(label);
abstandHori = abstandHori + 25;
}
return panel;
}

Related

Why oh why, does my scrollPane not scroll to anywhere?

I can't figure out why this code doesn't make scrollbars appear. I'm a complete beginner at Swing so the scrollpanes are very confusing and I don't understand some of the solutions I have seen online. The annoying thing is this code briefly worked but I destroyed the successful part before backing it up when trying to add a component. Any help would be appreciated!
import javax.swing.*;
import java.awt.*;
public class Test extends JFrame
{
private JPanel leftPanel;
private JButton myButton;
private JPanel scrollPanel;
private JScrollPane scrollPane;
public static void main(String[] args)
{
System.setProperty("swing.defaultlaf", "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run() {
new Test();
}
});
}
/**
* Constructor for objects of class Backup
*/
public Test()
{
this.setTitle("testy");
this.setSize(new Dimension(1280,622));
leftPanel = new JPanel();
leftPanel.setBackground(new Color(255,0,0));
leftPanel.setBounds(0, 100, 640, 558);
leftPanel.setEnabled(true);
this.add(leftPanel);
scrollPanel = new JPanel(null);
scrollPanel.setBackground(new Color(100,100,100));
scrollPanel.setPreferredSize(new Dimension(640, 550));
scrollPanel.setEnabled(true);
JScrollPane scrollPane = new JScrollPane(scrollPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBounds(0, 0, 642, 550);
scrollPane.setEnabled(true);
leftPanel.add(scrollPane);
for (int i = 1; i < 30; i++)
{
scrollPanel.setLayout(new GridLayout(30, 1, 0, 1));
myButton = new JButton("AAAAAAAAAAAAAAAAAAAAA " + i);
myButton.setPreferredSize(new Dimension(630, 70));
scrollPanel.add(myButton);
}
this.setBackground(new Color(0,0,0));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
}
I think I've massively improved it since then thanks to camickr
Here is the current version:
import javax.swing.*;
import java.awt.*;
public class Test extends JFrame
{
private JPanel leftPanel;
private JButton myButton;
private JPanel scrollPanel;
private JScrollPane scrollPane;
public static void main(String[] args)
{
System.setProperty("swing.defaultlaf",
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run() {
new Test();
}
});
}
public Test()
{
this.setTitle("testy");
this.setSize(new Dimension(1280,622));
leftPanel = new JPanel();
leftPanel.setBackground(new Color(255,0,0));
leftPanel.setBounds(0, 100, 640, 558);
this.add(leftPanel);
scrollPanel = new JPanel();
scrollPanel.setLayout(new GridLayout(70, 1, 0, 1));
//scrollPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20,
20));
scrollPanel.setBackground(new Color(100,100,100));
//scrollPanel.setPreferredSize(new Dimension(640, 550));
//JScrollPane scrollPane = new JScrollPane(scrollPanel);
JScrollPane scrollPane = new JScrollPane(scrollPanel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//scrollPane.setLayout(new GridLayout(30, 1, 0, 1));
//scrollPane.setBounds(0, 0, 642, 550);
scrollPane.setEnabled(true);
//scrollPanel.add(scrollPane);
leftPanel.add(scrollPanel);
for (int i = 1; i < 71; i++)
{
myButton = new JButton("AAAAAAAAAAAAAAAAAAAAA " + i);
//myButton.setPreferredSize(new Dimension(640, 80));
scrollPanel.add(myButton);
}
this.setBackground(new Color(0,0,0));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}}
Don't use a null layout.
Don't use setBounds().
Don't use setPreferredSize().
Each Swing component is responsible for determining its own preferred size. The layout manager will then set the size/location of each components added to the panel and it will then (dynamically) calculated the preferred size of the panel. The scroll bars will appear when the preferred size of the panel is greater than the size of the scroll pane.
scrollPanel = new JPanel(null);
scrollPanel.setBackground(new Color(100,100,100));
// scrollPanel.setPreferredSize(new Dimension(640, 550)); // delete
//scrollPanel.setEnabled(true); // not needed
...
for (int i = 1; i < 30; i++)
{
//scrollPanel.setLayout(new GridLayout(30, 1, 0, 1)); // set layout when panel created.
//myButton.setPreferredSize(new Dimension(630, 70)); // not needed.
The layout manager should be set outside the loop, when you create the panel. It should NOT be null.
Don't hard code the preferred size. It will not dynamically adjust as components are added.
Swing components are enabled by default to setEnabled is not needed.
Don't use the "leftPanel". Just add the scroll pane directly to the frame. This will allow the scrollpane to resize dynamically as the frame is resized. Then the scroll bars will appear when requirement.
Don't set the preferreid size of the button. The size will be determined based on the text and the Font of the button.

JScrollPanel, horizontal and vertical scroll not working

I have the next code:
public class MyScroll extends JFrame {
public MyScroll() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(null);
for (int i = 0; i < 10; i++) {
JButton b = new JButton("Hello-" + i);
b.setBounds(0, i * 50, 100, 45);
panel.add(b);
b.setLayout(null);
}
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(50, 30, 100, 325);
JPanel contentPane = new JPanel(null);
contentPane.setPreferredSize(new Dimension(500, 400));
contentPane.setBackground(Color.BLUE);
contentPane.add(scrollPane);
contentPane.setLayout(null);
setContentPane(contentPane);
pack();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
}
And, it is rendering this:
As you can see, the vertical and horizontal scroll aren't working, but both are defined and are displaying inside the JPanel.
Can someone explain me what am I doing wrong?
This code is based on this one:
Scrolling a JPanel
But, the is not working at the moment to use the verticall scrolling
Can someone explain me what am I doing wrong?
panel.setLayout(null);
Don't use a null layout.
The scrollbars will only appear automatically when the preferred size of the component added to the scrollpane is greater than the size of the scrollpane.
It is the job of the layout manager to determine the preferred size of the panel. Since you don't use a layout manager the preferred size isn't calculated.
So the solution is to use a Layout Manager. Maybe vertical BoxLayout.

Increase a row size in Gridlayout

The above frame contains gridlayout with two rows. First row is text area,second is a panel with two checkboxes. I want to increase height of first row so that first row should be 75% of Total height and second row should be 25%. How can I do that? Here is my code snippet:
setLayout(new GridLayout(2, 0, 0, 0));
Panel text_panel = new Panel();
add(text_panel);
text_panel.setLayout(new GridLayout(1, 0, 0, 0));
JTextArea textArea = new JTextArea();
textArea.setText("text to be displayed");
JScrollPane scroll = new JScrollPane (textArea);
text_panel.add(scroll);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
Border border = BorderFactory.createLineBorder(Color.GRAY);
textArea.setBorder(border);
textArea.setFont(new Font("Arial",Font.PLAIN,12));
textArea.setCaretPosition(0);
textArea.requestFocus();
Panel checebox_panel = new Panel();
checebox_panel.setBackground(Color.WHITE);
add(checebox_panel);
checebox_panel.setLayout(new GridLayout(1, 0, 0, 0));
androidCheckBox = new JCheckBox("Open start page");
androidCheckBox.setBackground(Color.WHITE);
androidCheckBox.addItemListener(itemListener);
androidCheckBox.setSelected(true);
checebox_panel.add(androidCheckBox);
eclipseCheckBox = new JCheckBox("register for updates");
eclipseCheckBox.setBackground(Color.WHITE);
eclipseCheckBox.addItemListener(itemListener);
eclipseCheckBox.setSelected(true);
checebox_panel.add(eclipseCheckBox);
This is not possible with GridLayout. GridLayout will always use even spacing. Look into another layout manager.
Here is a good reference:
https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
With GridLayout you can't have two rows that are different sizes. Look into BoxLayout. Something like this:
JPanel content = new JPanel();
frame.getContentPane().add(content);
LayoutManager layout = new BoxLayout(content, BoxLayout.Y_AXIS);
Box boxes[] = new Box[2];
boxes[0] = Box.createHorizontalBox();
boxes[1] = Box.createHorizontalBox();
boxes[0].createGlue();
boxes[1].createGlue();
content.add(boxes[0]);
content.add(boxes[1]);
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
panel.setPreferredSize(new Dimension(500,300));
panel2.setPreferredSize(new Dimension(500,200));
boxes[0].add(panel);
boxes[1].add(panel2);
Using setPreferredSize is never optimal, but it works. This is just an example of how you could do it, I'm sure there is better ways though! ;)
"Increase a row size in Gridlayout" I came across this requirement and by trying to device a solution I got one, tried border instead grid layout. it might be helpful :)
here is the code:
import ComponentMeta.RequiredComp;
import javax.swing.*;
import java.awt.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class PromptPopup extends JDialog {
private final JScrollPane scrollPane = new JScrollPane();
private int rows;
private int cols;
private int vGap;
private int hGap;
private Map<String, Component> componentRepo;
public PromptPopup(JFrame parent) {
super(parent);
componentRepo = new HashMap<>();
this.setModalityType(ModalityType.APPLICATION_MODAL);
}
public void setComponentsDisplayStyle(int rows, int cols, int vGap, int hGap) {
this.rows = rows;
this.cols = cols;
this.vGap = vGap;
this.hGap = hGap;
}
public void setComponentReop() {
JTextField dynamicParamTextField = new JTextField();
this.componentRepo.put("COMPANY_CODE", dynamicParamTextField);
JTextField dynamicParamTextField2 = new JTextField();
this.componentRepo.put("DIST_CODE", dynamicParamTextField2);
JTextField dynamicParamTextField3 = new JTextField();
this.componentRepo.put("LOCA_CODE", dynamicParamTextField3);
JTextField dynamicParamTextField4 = new JTextField();
this.componentRepo.put("TOKEN_EXEC", dynamicParamTextField4);
}
public void initPopupUI() {
//Setting content panes layout
getContentPane().setLayout(new BorderLayout(0, 0));
//Creating a root panel(root container) to hold the child components
JPanel rootContainer = new JPanel();
rootContainer.setLayout(new BorderLayout());
//Creating header panel(header container) to hold the header components
JPanel header = new JPanel();
header.setLayout(new FlowLayout());
JLabel headerText = new JLabel("Source query parameters required ");
headerText.setForeground(Color.WHITE);
header.add(headerText);
header.setBackground(Color.BLUE);
//Creating footer panel(footer container ) to hold the footer components
JPanel footer = new JPanel();
footer.setLayout(new FlowLayout());
JButton executeWithParamsButton = new JButton("Execute with params");
executeWithParamsButton.setBackground(Color.BLACK);
executeWithParamsButton.setForeground(Color.WHITE);
JButton cancelButton = new JButton("Cancel");
cancelButton.setBackground(Color.RED);
cancelButton.setForeground(Color.WHITE);
footer.add(executeWithParamsButton);
footer.add(cancelButton);
footer.setBackground(Color.BLUE);
//Creating content panel(content container) to hold the all dynamically generated components
JPanel contentContainer = new JPanel();
GridLayout gridLayout = new GridLayout(this.rows, this.cols, this.hGap, this.vGap);
contentContainer.setLayout(gridLayout);
for (Map.Entry entry : componentRepo.entrySet()) {
JLabel dynamicParamLabel = new JLabel(entry.getKey().toString());
contentContainer.add(dynamicParamLabel);
contentContainer.add((Component) entry.getValue());
}
// Adding all the created containers to the root container one by one
rootContainer.add(header, BorderLayout.NORTH);
rootContainer.add(contentContainer, BorderLayout.CENTER);
rootContainer.add(footer, BorderLayout.SOUTH);
//Adding the root container to the scroll pane in order the view to be scrollable nno matter how many components are there.
scrollPane.setViewportView(rootContainer);
getContentPane().add(scrollPane);
}
}
And Here is output I wanted i.e The Header to be on top consuming space only based on it's components and same for the Center panel and The footer containing button controls.
output

How to get a JScrollPane to resize with its parent JPanel

My question is similar to this one (How to get JScrollPanes within a JScrollPane to follow parent's resizing), but that question wasn't clear and the answer there didn't help me..
I have this SSCCE (using MigLayout):
public static final int pref_height = 500;
public static void main(String[] args) {
JPanel innerPanel = new JPanel(new MigLayout());
innerPanel.setBorder(new LineBorder(Color.YELLOW, 5));
for(int i = 0; i < 15; i++) {
JTextArea textArea = new JTextArea();
textArea.setColumns(20);
textArea.setRows(5);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
JScrollPane jsp = new JScrollPane(textArea);
innerPanel.add(new JLabel("Notes" + i));
innerPanel.add(jsp, "span, grow");
}
JScrollPane jsp = new JScrollPane(innerPanel) {
#Override
public Dimension getPreferredSize() {
setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
Dimension dim = new Dimension(super.getPreferredSize().width + getVerticalScrollBar().getSize().width, pref_height);
setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
return dim;
}
};
jsp.setBorder(new LineBorder(Color.green, 5));
JPanel outerPanel = new JPanel();
outerPanel.setBorder(new LineBorder(Color.RED, 5));
outerPanel.add(jsp);
JFrame frame = new JFrame();
JDesktopPane jdp = new JDesktopPane();
frame.add(jdp);
jdp.setPreferredSize(new Dimension(800, 600));
frame.pack();
JInternalFrame jif = new JInternalFrame("Title", true, true, true, true);
jif.pack();
jif.add(outerPanel);
jdp.add(jif);
jif.pack();
jif.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
I want the JScrollPane to resize whenever the parent JPanel is resized. Basically, I want the green border to line up with the red border. Right now, the green border stays the same size no matter the red border (unless you resize too small).
JPanel outerPanel = new JPanel();
A JPanel uses a FlowLayout by default which always respects the size of the component added to it. As a guess, maybe you can use:
JPanel outerPanel = new JPanel( new BorderLayout() );
A BorderLayout give all the space available to the component added to the panel. By default a JInternalFrame also uses a BorderLayout. So since all the parent components of your scroll pane use a BorderLayout all the space should go to the scroll pane.
When you post a SSCCE you should post code using classes from the JDK that simulates your problem so that everybody can test your SSCCE.
I noticed this did not have an answer that uses the original layout so here is one.
In order to make the JScrollPane resize when the parent JPanel is resized you need to do two things.
1) Set the layout of the panel to grow. This can be using the following code.
new MigLayout("", //Layout Constraints
"grow", //Column Constraints
"grow"); //Row Constraints
2) Set the component to grow. This is as simple as adding an extra argument in the add() function.
add(jsp, "grow");
ExtraIn order to make the JTextArea column grow when you resize the JScrollPane you can change the layout to only make the second column change. For example
new MigLayout("", //Layout Constraints
"[/*Column 1*/][grow /*Column 2*/]", //Column Constraints
""); //Row Constraints
Also, I would recommend you use wrap instead of span to use the next row as span refers using so many columns. For example span 2 //Means use 2 columns for this component. This would mean when you add your jsp to innerPanel it would become
innerPanel.add(jsp, "wrap, grow");
Edited SSSCE
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import net.miginfocom.swing.MigLayout;
public class JSPR extends JFrame {
public static final int pref_height = 500;
public static void main(String[] args) {
JPanel innerPanel = new JPanel(new MigLayout("", "[][grow]", ""));
innerPanel.setBorder(new LineBorder(Color.YELLOW, 5));
for(int i = 0; i < 15; i++) {
JTextArea textArea = new JTextArea();
textArea.setColumns(20);
textArea.setRows(5);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
JScrollPane jsp = new JScrollPane(textArea);
innerPanel.add(new JLabel("Notes" + i));
innerPanel.add(jsp, "wrap, grow");
}
JScrollPane jsp = new JScrollPane(innerPanel) {
#Override
public Dimension getPreferredSize() {
setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
Dimension dim = new Dimension(super.getPreferredSize().width + getVerticalScrollBar().getSize().width, pref_height);
setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
return dim;
}
};
jsp.setBorder(new LineBorder(Color.green, 5));
JPanel outerPanel = new JPanel(new MigLayout("", "grow", "grow"));
outerPanel.setBorder(new LineBorder(Color.RED, 5));
outerPanel.add(jsp, "grow");
JFrame frame = new JFrame();
JDesktopPane jdp = new JDesktopPane();
frame.add(jdp);
jdp.setPreferredSize(new Dimension(800, 600));
frame.pack();
JInternalFrame jif = new JInternalFrame("Title", true, true, true, true);
jif.pack();
jif.add(outerPanel);
jdp.add(jif);
jif.pack();
jif.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

New invisible layer in JPanel

I would like to ask for help. I want something like this: http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html#glasspane
I have this application with map and i want to have a small picture in an new invisible layer. Small picture could move by JSliders up and left. That layer should be only over "label". A have this code:
private JPanel mujPanel5() {
JSlider [] posuvniky = new JSlider[2];
posuvniky[0] = new JSlider(SwingConstants.HORIZONTAL, 0, 100, 30);
posuvniky[1] = new JSlider(SwingConstants.VERTICAL, 0, 100, 60);
for (int i = 0; i < posuvniky.length; i++) {
posuvniky[i].addChangeListener(new ZmenaPosuvniku5()); //Listener do nothing
}
URL umisteniMapy = this.getClass().getResource("map.jpg");
Icon obrazek = new ImageIcon(umisteniMapy);
JLabel label = new JLabel(obrazek, SwingConstants.CENTER);
label.setOpaque(true);
label.setBackground(Color.white);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(posuvniky[0], BorderLayout.NORTH);
panel.add(posuvniky[1], BorderLayout.WEST);
panel.add(label, BorderLayout.CENTER);
return panel;
}
Thanks to everybody.
MmM ...
I do not know how to create described invisible layer
See How to Use Layered Panes.

Categories

Resources