开发者

remove borders inside JScrollPane

开发者 https://www.devze.com 2023-04-11 05:09 出处:网络
I\'m trying to put multiple JLists in a JPanel and the JPanel in a JScrollPane. The problem is that when I add the JScrollPane to the equation there is a space between the JList and outer border, how

I'm trying to put multiple JLists in a JPanel and the JPanel in a JScrollPane. The problem is that when I add the JScrollPane to the equation there is a space between the JList and outer border, how do I get rid of this border so it's fills horizontally?

Here is a small sample code that demonstrates this problem with a JLabel instead of JList.

/edit: It's seems to be windows thing, i开发者_开发问答t runs fine on Mac OS.

import java.awt.*;
import javax.swing.*;

public class Test extends JFrame
{  
    public static void main(String[] args) 
    {
        Test test = new Test();
    }

    public Test()
    {
        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout());        
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);        
        this.setContentPane(contentPane);        
        this.setMinimumSize(new Dimension(400, 300));

        JScrollPane sp = new JScrollPane(createLeftPane());
        sp.setBorder(BorderFactory.createLineBorder(Color.yellow));
        this.add(sp, BorderLayout.WEST);

        LookAndFeel.installBorder(sp, "BorderFactory.createEmptyBorder()");        

        StackTraceElement[] st = Thread.currentThread().getStackTrace();
        for(int i = 0; i < st.length; i++) {
            System.out.println(st[i]);
        }

        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);

    }

    private JPanel createLeftPane()
    {
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        JLabel label = new JLabel("TEST LABEL");
        label.setBorder(BorderFactory.createLineBorder(Color.blue));
        panel.add(label, c);
        panel.setBorder(BorderFactory.createLineBorder(Color.red));

        return panel;
    }        
}

Here's a screenshot (first one is without the scrollpane)

remove borders inside JScrollPane

Stack Trace:

java.lang.Thread.getStackTrace(Thread.java:1479) 
test.Test.<init>(Test.java:27) 
test.Test.main(Test.java:10)

/edit: After some trial and error I found out that when I add a c.weightx = 0.5 to the contrains it does fill Horizontally, but when the scrollPane becomes larger than it's content it makes itself wider, which is weird. See the screenshots below:

remove borders inside JScrollPane


The problem isn't because of the scrollpane. Its because you add the label to a JPanel. By default a panel uses a FlowLayot which has has a vertical/horizontal gap of 5 pixels around each component.

Change the FlowLayout to use a 0 gap, or use a different layout. Maybe a BoxLayout.


component.setBorder(null) removes any border which has been set on the component


The problem doesn't occur when I run it on my Mac, so apparently it's a Windows thing.

In addition to the expected UI defaults for background, foreground, font and opaque, BasicPanelUI installs a border, while com.apple.laf.AquaPanelUI (apparently) does not.

LookAndFeel.installBorder(p,"Panel.border");

You might try setting yours to null.


are you meaning this way ???

remove borders inside JScrollPane

import java.awt.*;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;

public class FilesInTheJList {

    private static final int COLUMNS = 5;
    private Dimension size;

    public FilesInTheJList() {
        final JList list = new JList(new File("C:\\").listFiles()) {

            private static final long serialVersionUID = 1L;

            @Override
            public Dimension getPreferredScrollableViewportSize() {
                if (size != null) {
                    return new Dimension(size);
                }
                return super.getPreferredScrollableViewportSize();
            }
        };
        list.setFixedCellHeight(50);
        list.setFixedCellWidth(150);
        size = list.getPreferredScrollableViewportSize();
        size.width *= COLUMNS;
        list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        list.setCellRenderer(new MyCellRenderer());
        list.setVisibleRowCount(0);
        list.setLayoutOrientation(JList.HORIZONTAL_WRAP);

        JFrame f = new JFrame("Files In the JList");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new JScrollPane(list));
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                FilesInTheJList fITJL = new FilesInTheJList();
            }
        });
    }

    private static class MyCellRenderer extends JLabel implements ListCellRenderer {

        private static final long serialVersionUID = 1L;

        @Override
        public Component getListCellRendererComponent(JList list, Object value,
                int index, boolean isSelected, boolean cellHasFocus) {
            if (value instanceof File) {
                File file = (File) value;
                setText(file.getName());
                setIcon(FileSystemView.getFileSystemView().getSystemIcon(file));
                if (isSelected) {
                    setBackground(list.getSelectionBackground());
                    setForeground(list.getSelectionForeground());
                } else {
                    setBackground(list.getBackground());
                    setForeground(list.getForeground());
                }
                setPreferredSize(new Dimension(250, 25));
                setEnabled(list.isEnabled());
                setFont(list.getFont());
                setOpaque(true);
            }
            return this;
        }
    }
}

.

.

similair but better way by @trashgod here


The solution of trashgod worked for me with

LookAndFeel.installBorder(scrollpane,BorderFactory.createEmptyBorder().toString());


You can also remove the JScrollPane's border using

JScrollPane yourScrollPane = new JScrollPane();
yourScrollPane.setBorder(BorderFactory.createEmptyBorder());
0

精彩评论

暂无评论...
验证码 换一张
取 消