I don't know why this is happening, I've looked up nearly every Stack Overflow question regarding this.
The error is, When I add a JScrollPane to JList, Nothing shows up. Even if I'm adding it to the frame.
JList mainlist = new JList();
JScrollPane listScroller = new JScrollPane(mainlist);
listScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
listScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
mainlist.setToolTipText("List of People");
mainlist.setFont(new Font("Consolas", Font.BOLD, 13));
int pos = mainlist.getModel().getSize();
DefaultListCellRenderer renderer = (DefaultListCellRenderer)mainlist.getCellRenderer();
renderer.setHorizontalAlignment(JLabel.CENTER);
mainlist.setModel(model);
mainlist.setForeground(Color.GREEN);
mainlist.setBackground(new Color(44, 47, 51));
mainlist.setBounds(10, 108, 780, 248);
mainlist.setFixedCellHeight(20);
mainlist.setFixedCellWidth(30);
mainlist.setBorder(new EmptyBorder(10,10, 10, 10));
frm.getContentPane().add(listScroller);
You should apply bounds to your scrollPane and not Your List
listScroller.setBounds(10, 108, 780, 248);
Also make sure Your Content Pane has null layout for setBounds() to work or better just use an border layout and set the preferred&maximum Size() to what works for you
Related
public void createSpringLayout(SpringLayout spring, JLabel label, JScrollPane scrollPane, JPanel buttonPanel) {
spring.putConstraint(SpringLayout.NORTH, label, 10, SpringLayout.NORTH, this);
spring.putConstraint(SpringLayout.WEST, label, 10, SpringLayout.WEST, this);
spring.putConstraint(SpringLayout.NORTH, scrollPane, 10, SpringLayout.SOUTH, label);
spring.putConstraint(SpringLayout.WEST, scrollPane, 0, SpringLayout.WEST, label);
spring.putConstraint(SpringLayout.EAST, scrollPane, -10, SpringLayout.EAST, this);
spring.putConstraint(SpringLayout.NORTH, buttonPanel, Spring.constant(10, 30, 30), SpringLayout.SOUTH, scrollPane);
spring.putConstraint(SpringLayout.SOUTH, buttonPanel, -10, SpringLayout.SOUTH, this);
spring.putConstraint(SpringLayout.WEST, buttonPanel, 0, SpringLayout.WEST, label);
}
The two Jpanels with all the stuff on them are using the same superclass for better looks.
However as you can see if the contents of the scrollpane are to wide the scrollpane just uses extra space in the bottom to create a horizontal scrollbar.
Even if I tell the Springlayout that the spring between the scrollpane and the buttonPanel can be between 10 and 30.
I think that first the SpringLayoutManager is called to layout its components and then the ScrollPane comes along and notices that its displayed Components do not fit in the Viewport and creates a Scrollbar, which the SpringLayoutManager is unaware of.
I can't find any solution tha tell the ScrollPane beforehand to calculate its needed Size or for it to just dont use more Space than it has from the beginning.
Do not pass negative numbers to putConstraint.
From the documentation of putConstraint:
Links edge e1 of component c1 to edge e2 of component c2, with a fixed distance between the edges.
The pad value is not an absolute pixel offset, it is the amount of padding (the distance) between the component and the linked edge. It should always be positive.
The same javadoc also describes the pad parameter as:
pad - the fixed distance between dependent and anchor
SpringLayout is complex, which means it’s easy to make mistakes with it. You can easily accomplish the same layout with a BorderLayout:
setLayout(new BorderLayout());
add(label, BorderLayout.PAGE_START);
add(scrollPane, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.PAGE_END);
label.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
this.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
I'm trying to create a list of labels in java swing.
Main Code:
JLabel local = new JLabel();
local.setBackground(new Color(186, 79, 84));
local.setFont(new Font("SansSerif", Font.BOLD, 10));
local.setForeground(new Color(255, 255, 255));
local.setText("Local");
JTable list = new JTable(new JLabel[][]{{local}}, new String[]{"Local"});
But it's giving me a little problem.
What would be the best way, the goal would be to create a vertical list with one item on each line.
I'm creating a list of bank movements with the following code. pane is a JPanel and array is an ArrayList that contains respectively amount and description data. Setting is a little icon that allow you to modify each movement.
MouseClass is a class that extends MouseAdapter that I've created to add "k" index to mouseClicked method. I'm new with java gui programming. I'd like to know if there is a quick method to add a scroll to my panel
JLabel[] movement = new JLabel[array.size()];
JLabel[] description = new JLabel[array.size()];
JLabel[] data = new JLabel[array.size()];
JLabel[] setting = new JLabel[array.size()];
System.out.println(array.size());
int i = 0;
for(int k=0; k<array.size(); k++){
movement[k] = new JLabel("");
movement[k].setForeground(SystemColor.text);
movement[k].setFont(new Font("Segoe UI", Font.PLAIN, 20));
movement[k].setBounds(17, i, 145, 30);
movement[k].setText(array.get(array.size() - k - 1).getAmount() + "€");
panel.add(movement[k]);
description[k] = new JLabel("");
description[k].setForeground(SystemColor.text);
description[k].setFont(new Font("Segoe UI", Font.PLAIN, 20));
description[k].setBounds(187, i, 274, 30);
description[k].setText(array.get(array.size() - k - 1).getDescription());
panel.add(description[k]);
data[k] = new JLabel("");
data[k].setForeground(SystemColor.text);
data[k].setFont(new Font("Segoe UI", Font.PLAIN, 20));
data[k].setBounds(478, i, 145, 30);
data[k].setText(array.get(array.size() - k - 1).getDate());
panel.add(data[k]);
setting[k] = new JLabel();
setting[k].setIcon(new ImageIcon(List.class.getResource("/it/andreavaiuso/financemanager/images/edit.png")));
setting[k].setForeground(SystemColor.text);
setting[k].setFont(new Font("Segoe UI", Font.PLAIN, 20));
setting[k].addMouseListener(new MouseClass(array.size() - k - 1) {
#Override
public void mouseClicked(MouseEvent arg0) {
Modify mdf = new Modify(this.index);
mdf.setVisible(true);
dispose();
}
});
setting[k].setBounds(640, i, 82, 30);
panel.add(setting[k]);
i += 40;
}
But I don't know how to scroll it. I've tried woth JScrollPane but don't work!
I'm sure there is a simplest way to add these items to my panel...
I've tried woth JScrollPane but don't work!
Well I see lots of code with setBounds(...) which implies you are using a null layout.
Don't use a null layout. Swing was designed to be used with layout managers. In fact the scroll pane will only work when used with a layout manager because the scroll pane needs to know the preferred size of the panel so it can determine when you use scrollbars.
I would also suggest you should also be using a JTable for something like this. It is more efficient because you don't need to create individual components for each row of data. Read the section from the Swing tutorial on How to Use Tables for more information and examples.
For a JScrollPane you need, 1 JScrollPane, 1 JList and one DefaultListModel.
First you add your items to DefaultListModel, then you add the model to the JList and then you make a JSCrollPane with passing as argument your JList
Example:
DefaultListModel model = new DefaultListModel<String>();
JList list = new JList<String>();
model.addElemenet("1");
model.addElemenet("3");
model.addElemenet("2");
list.setModel(model);
JScrollPane scrollPane = new JScrollPane(list);
I used windowbuilder to create a GUI that should be a contact list. There are two lists: one of contacts, the other one of numbers. when you select a contact his number will appear.
But, when I add a lot of contacts, the scroll bar does not automatically appear, as it does to my friend, so I tried adding a JScrollPane. Still does not work.
Here is what it was:
DefaultListModel contact = new DefaultListModel();
JList contacts = new JList(contact);
contacts.setBounds(22, 64, 186, 135);
contentPane.add(contacts);
here is what I tried:
DefaultListModel contact = new DefaultListModel();
JList contacts = new JList(contact);
contacts.setBounds(22, 64, 186, 135);
JScrollPane scrollPane1 = new JScrollPane(contacts);
contentPane.add(contacts);
contentPane.add(scrollPane1, BorderLayout.WEST);
then I tried addind Bounds to the scrollPanel as well:
DefaultListModel contact = new DefaultListModel();
JList contacts = new JList(contact);
contacts.setBounds(22, 64, 186, 135);
JScrollPane scrollPane1 = new JScrollPane(contacts);
scrollPane1.setBounds(22, 64, 186, 135);
contentPane.add(contacts);
contentPane.add(scrollPane1, BorderLayout.WEST);
when I erased 'contacts.setBounds(22, 64, 186, 135);', the list stopped working.
I also tried not having the last line, I tried only having the last line and not having the second last line, I tried adding "scrollPane1.setViewportView(contacts);", I tried taking away the "BorderLayout.WEST" argument for all the other tries, so I basically tried every combination of "solutions" I could find on the internet
I have no idea what is going on and I have no idea how to fix it. All I want is to be able to see all my contacts by scrolling.
EDIT, SOLVED:
The problem was my panel was declared as contentPane.setLayout(null); therefore "BorderLayout.WEST" was unnecessary. In addition, the bounds should be applied to the scrollpane, not the list. Last, I should not have used add(contacts) to the panel. the solution was:
DefaultListModel contact = new DefaultListModel();
JList contacts = new JList(contact);
JScrollPane scrollPaneContact = new JScrollPane(contacts);
scrollPaneContact.setBounds(22, 64, 186, 135);
contentPane.add(scrollPaneContact);
I'm updating in case anyone have a similar problem.
You need to set the policies for JScrollPane using proper constructor.
JScrollPane(Component view, int vsbPolicy, int hsbPolicy)
Creates a JScrollPane that displays the view component in a viewport whose view position can be controlled with a pair of scrollbars.
For instance:
new JScrollPane(COMPONENT, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
EDIT:
DefaultListModel model = new DefaultListModel();
JList contactsList = new JList(model);
JScrollPane scrollPane1 = new JScrollPane(contactsList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JScrollPane scrollPane1 = new JScrollPane(contacts);
//contentPane.add(contacts); // get rid of this
contentPane.add(scrollPane1, BorderLayout.WEST);
First you add the contacts JList to the viewport of the scroll pane which is correct.
But then you add the contacts to the frame directly, which is wrong. A component can only have a single parent. By adding the contact JList to the frame you remove it from the scroll pane.
Also, don't use a null layout and don't use setBounds(...). Swing was designed to be used with Layout Managers.
If you need more help then post a proper SSCCE that demonstrates the problem. A SSCCE should be included with every question.
I have a JPanel which contains some fields. The height of the JPanel is limited so I have to put a JScrollPane around it so people can scroll down.
As you can see below, it displays perfectly. But you can't scroll down (or up).
DetailPanel detail = new DetailPanel();
JScrollPane jsp = new JScrollPane(detail);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jsp.setBounds(745, 10, 235, 225);
add(jsp);
Detail panel:
private void init(){
setLayout(null);
setSize(140, 400);
int x = 5, y = 0;
for(int i = 0; i < lbls.length; i++) {
JLabel lbl = new JLabel(lbls[i]);
lbl.setBounds(x, y, 200, 25);
add(lbl);
fields[i] = new JTextField();
fields[i].setBounds(x, y+26, 200, 25);
add(fields[i]);
y+=50;
}
}
Your DetailPanel has no layout manager associated with it, which means it doesn't expand when you add children to it, which means the JScrollPane doesn't have anywhere to scroll. Either call setLayout() on your DetailPanel or override getPreferredSize() to add up the heights of its children and return a meaningful value.
I could be wrong, but I think this might be happening because DetailPanel's layout is null. What happens if you use a BoxLayout in vertical orientation and call detail.setPreferredSize(new Dimension(140,400));?