JLabels in a JScrollPane - java

I have a scrollpane in which i want to add multiple jlabel.
This is the code..
JPanel panelEast = new JPanel();
panelEast.setBorder(new TitledBorder(null, "Notifiche", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panelEast.setPreferredSize(new Dimension(250,120));
panelEast.setLayout(null);
JLabel lblNewLabel_3 = new JLabel("label1");
lblNewLabel_3.setIcon(new ImageIcon(Home.class.getResource("/it/polimi/icon/contact.png")));
lblNewLabel_3.setBounds(10, 81, 240, 52);
panelEast.add(lblNewLabel_3);
JLabel label_3 = new JLabel("label2");
label_3.setIcon(new ImageIcon(Home.class.getResource("/it/polimi/icon/verified.png")));
label_3.setBounds(new Rectangle(4, 0, 0, 0));
label_3.setAlignmentY(Component.TOP_ALIGNMENT);
label_3.setBounds(10, 30, 240, 52);
panelEast.add(label_3);
JLabel label_4 = new JLabel("label3");
label_4.setIcon(new ImageIcon(Home.class.getResource("/it/polimi/icon/verified.png")));
label_4.setBounds(new Rectangle(4, 0, 0, 0));
label_4.setAlignmentY(Component.TOP_ALIGNMENT);
label_4.setBounds(10, 131, 240, 52);
panelEast.add(label_4);
JScrollPane scrollPane = new JScrollPane(panelEast,JScrollPane.VERTICAL_SCROLLBAR_NEVER,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
pan2.add(scrollPane,BorderLayout.CENTER);
And does not work, any suggestion?

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify
JScrollPane relies on the layout manager API in order to calculate the preferred size of the container it is showing and determine when that view is larger than itself and it should show the scroll bars

Related

How to have gap between the frame and a component?

I'm trying practice my GUI and I am having troubles putting gap between component and the frame.
The picture above is what I have so far. But I really want to put a gap between the left side of the frame and "label1".
private void initialize() {
frame = new JFrame();
frame.setTitle("WINDOW");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 300);
panel = new JPanel();
panel.setLayout(new BorderLayout());
bottomPanel = new JPanel();
bottomPanel.setLayout(new GridLayout(1, 5));
l1 = new JLabel("Label1");
l2 = new JLabel("Label2");
l3 = new JLabel("Label3");
l4 = new JLabel("Label4");
l5 = new JLabel("Label5");
bottomPanel.add(l1);
bottomPanel.add(l2);
bottomPanel.add(l3);
bottomPanel.add(l4);
bottomPanel.add(l5);
panel.add(bottomPanel, BorderLayout.SOUTH);
frame.add(panel);
}
Above is part my code. I tried doing:
bottomPanel.setLayout(new GridLayout(1, 5, -20, 0));
to put some horizontal gap but that only added gap between the components. That didn't move "label1" away from the frame at all. Is there any other way of doing this? I am very new to Java so I don't really know much of the other tricks. I would appreciate any help! Thank you!
The other answers are fudges that won't achieve the desired effect when the GUI is resized. Instead use:
JLabel.setHorizontalAlignment(SwingConstants.CENTER);
By centering the text within the JLabel, combined with GridLayout stretching the components to the full width of the cell, each label will have as much space either side as the GUI can allow. E.G. here is the effect when the GUI is at minimum size.
And when stretched wider:
(The red border is added to show the bounds of each label.)
Add a Border to the panel:
bottomPanel = new JPanel();
bottomPanel.setBorder( new EmptyBorder(0, 10, 0, 0) );
Read the section from the Swing tutorial on How to Use Borders for more information about the different borders you can create.
Try the following:
bottomPanel.add(javax.swing.Box.createHorizontalStrut(10));

Add Scroll bar in JEditorPane, setLayout null

I can't add scroll bar on EditorPane.
private JEditorPane editorPane;
private JScrollPane scrollpane;
Container :
Container c = getContentPane();
c.setLayout(null);
setBounds(100, 100, 450, 300);
editorPane = new JEditorPane();
editorPane.setBounds(0, 54, 434, 208);
scrollpane = new JScrollPane(editorPane);
scrollpane.setPreferredSize(new Dimension(350, 110));
c.add(scrollpane);
..
..
Nothing added
You're shooting yourself in the foot here:
editorPane.setBounds(0, 54, 434, 208);
By setting the editorPane's absolute size, you prevent it from expanding when it needs to do so, preventing the JScrollBars from having to show.
Solution: don't do this. And yeah, avoid using null layouts. They'll bite you, as you're finding out. Set the width using CSS
getContentPane().setLayout(null);
This means "I give a damn on the help of others because I know better that anyone else how to layout a GUI!"
So this is where you are.
I'd recomment to go through the tutorials and learn how to build GUIs using LayoutManagers.

Java FlowLayout Remove Horizontal Space in Front of First Component

Is there an easier way to just remove the horizontal space in front of the first component in FlowLayout?
This is basically what my code looked like :
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
JLabel label1 = new JLabel("Hello");
JLabel label2 = new JLabel("Goodbye");
panel.add(label1);
panel.add(label2);
What I'm seeing is that there is a horizontal gap between label1 and label2, however, it also added spacing in front of label1. My current solution is remove the horizontal gap and add an EmptyBorder to label2 to fix this.
But for situations with many components, I am wondering if there is a more easy and efficient way to do something this simple?
You can use a horizontal BoxLayout:
panel.add( label1 );
panel.add( Box.createHorizontalStrut(5) );
panel.add( label2 );
Or you can add an EmptyBorder to the panel, instead of the labels:
panel.setBorder( BorderFactory.createEmptyBorder(0, -5, 0, 0) );
Try
new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
The second parameter stands for a horizontal gap. So maybe this fix your problem.
You can also look:
http://docs.oracle.com/javase/7/docs/api/java/awt/FlowLayout.html
http://docs.oracle.com/javase/tutorial/uiswing/layout/flow.html

Hide JScrollBar in a JScrollPane

Update : The second solution works perfectly but you have to be careful with the size of some of your object to not active default value for ScrollBar.You can't hide both you have to make a choice ;)
I would like to hide the ScrollBar but still able to scroll on my JScrollPane.
I have tried this :
scrollPane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_NEVER);
But i can't scroll anymore :( I have also try this :
scrollPane.getVerticalScrollBar().setPreferredSize (new Dimension(0,0));
scrollPane.getHorizontalScrollBar().setPreferredSize (new Dimension(0,0));
scrollPane.getVerticalScrollBar().setMinimumSize(new Dimension(0, 0));
scrollPane.getVerticalScrollBar().setMinimumSize(new Dimension(0, 0));
scrollPane.getVerticalScrollBar().setMaximumSize(new Dimension(0, 0));
scrollPane.getVerticalScrollBar().setMaximumSize(new Dimension(0, 0));
But still 1 pixel (by default i think) of the ScrollBar still be seen when i scroll :(
I have found the solution :
panLeft.setBackground(new Color(1, 0, 0, 0));
panLeft.setLayout(null);
panLeft.setPreferredSize(null);
panLeft.addMouseWheelListener(new MouseWheelListenerPanLeft());
scrollPaneLeft.setBounds(10, 35, 250, 525);
scrollPaneLeft.setBackground(new Color(1, 0, 0, 0));
scrollPaneLeft.getVerticalScrollBar().setPreferredSize(new Dimension(0, 0));
scrollPaneLeft.getViewport().setBorder(null);
scrollPaneLeft.setViewportBorder(null);
scrollPaneLeft.setBorder(null);

JScrollPane Contents Not Showing

I have a JTextArea inside of a JPanel that is then placed into the JScrollPane. When the JPanel that contains the JScrollPane is first show the JScrollPane shows up but not the contents. As soon as the JFrame is resized the contents show up.
JTextArea area = new JTextArea(6, 20);
area.setText("Some test text");
JPanel panel = new JPanel(new BorderLayout());
panel.add(area, BorderLayout.CENTER);
JScrollPane pane = new JScrollPane();
pane.setBounds(20, 20, WIDTH - 40, 300 - 40);
pane.setPreferredSize(new Dimension(WIDTH - 40, 300 - 40));
add(pane);
pane.setViewportView(panel);
pane.setBounds(20, 20, WIDTH - 40, 300 - 40);
pane.setPreferredSize(new Dimension(WIDTH - 40, 300 - 40));
Those two lines of code doen't make sense (although they are not the cause of your problem)
The first line is used when you are using a "null layout".
The second is used when you are using layout managers.
They should not be used together.
The second is preferred since you should be using layout managers.
In the application different JPanels are swapped out in a manner similar to a slide-show. So something like this would be found in the code:
panel.remove(slide1);
panel.add(slide2);
panel.repaint();
The problem being that all of the contents of the second slide, slide2, would not show up. The solution is to add
frame.validate();
Where frame is the parent window of panel.
new JScrollPane(panel);
I believe that you need to add the panel to the scroll pane constructor.

Categories