JList中的组件被White Square Thing隐藏直到被单击 [英] Components in JList are Hidden by White Square Thing Until Clicked

查看:76
本文介绍了JList中的组件被White Square Thing隐藏直到被单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在从事的项目中,我注意到JScrollPane中的所有JList项都被隐藏,直到单击JScrollPane/JList为止.奇怪的是它没有被完全覆盖.有一个带有透明边框的白色框,该边框散布在整个对象上,在所有边缘上覆盖了除少数像素以外的所有像素.

In a project I've been working on, I noticed that all the JList items in my JScrollPane are hidden until the JScrollPane/JList has been clicked. The weird part is it's not completely covered. There's this white box with a transparent border that spreads out over the whole thing, covering all but a few pixels on all edges.

图片:

如您所见,中间有一个白色块-注意粉红色的边框":

As you can see, there is this white block in the middle - notice the pink "border":

现在,一旦我单击该白框,它就会消失:

Now, once I click that white box, it goes away:

我知道洋红色看起来很可怕,但是我将其用于对比.

I know the magenta looks horrible, but I'm using it for contrast.

哪个使我想到了一个问题:我该如何摆脱那个令人讨厌的白盒子?

Which leads me to my question: how do I get rid of that obnoxious white box?

这是我的代码:

public static void listJars(File f)
{
    JCheckBox firstBox = null;
    DefaultListModel<JCheckBox> model = new DefaultListModel<>();
    if(mainGUI.checkList != null)
    {
        //System.out.println("Already exists lol: " + mainGUI.checkList.getName());
        mainGUI.pluginList.remove(mainGUI.checkList);
    }
    //mainGUI.pluginList.repaint();

    File[] files = new File(f.getPath()).listFiles();
    if (files != null)
    {
        for (File file : files)
        {
            if (file.getName().endsWith(".jar") || file.getName().endsWith("._jar"))
            {
                JCheckBox cb = new JCheckBox(file.getName());

                if(firstBox == null)
                {
                    firstBox = cb;
                }

                cb.setSelected(file.getName().endsWith(".jar"));
                cb.setVisible(true);
                cb.setText(file.getName());
                model.addElement(cb);
                cb.repaint();
            }
        }
    }

    JCheckBoxList jCheckBoxList = new JCheckBoxList(model, mainGUI.textField1.getText());
    jCheckBoxList.setName("pluginCheckboxList");
    jCheckBoxList.setSize(mainGUI.pluginList.getSize());
    mainGUI.pluginList.add(jCheckBoxList);
    mainGUI.checkList = jCheckBoxList;
    jCheckBoxList.setVisible(true);
    jCheckBoxList.setVisibleRowCount(10);
}

还有十个是我的JCheckBoxList类.

And ten there's my JCheckBoxList class.

package Components;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

@SuppressWarnings("serial")
public class JCheckBoxList extends JList<JCheckBox>
{
    protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
    protected String lastPath;

    public JCheckBoxList(final String lastPath)
    {
        this.lastPath = lastPath;
        setCellRenderer(new CellRenderer());
        setBackground(Color.magenta);
        addMouseListener(new MouseAdapter()
        {
            public void mousePressed(MouseEvent e)
            {
                int index = locationToIndex(e.getPoint());
                if (index != -1)
                {
                    JCheckBox checkBox = getModel().getElementAt(index);
                    checkBox.setSelected(!checkBox.isSelected());
                    repaint();

                    final String oldname = checkBox.getText();
                    if (!checkBox.isSelected())
                    {
                        checkBox.setName(checkBox.getText().substring(0, checkBox.getText().length() - 4) + "._jar");
                    }
                    else
                    {
                        checkBox.setName(checkBox.getText().substring(0, checkBox.getText().length() - 5) + ".jar");
                    }
                    System.out.println("Changed! Sel: " + checkBox.isSelected() + ", Name: " + checkBox.getName());
                    checkBox.setText(checkBox.getName());
                    String base = new File(lastPath).getParent() + "/plugins/";
                    boolean rename = new File(base + oldname).renameTo(new File(base + checkBox.getText()));
                }
            }
        });
        setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }

    public JCheckBoxList(ListModel<JCheckBox> model, String lastPath)
    {
        this(lastPath);
        setModel(model);
    }

    protected class CellRenderer implements ListCellRenderer<JCheckBox>
    {
        public Component getListCellRendererComponent(
                JList<? extends JCheckBox> list, JCheckBox value, int index,
                boolean isSelected, boolean cellHasFocus)
        {
            //Drawing checkbox, change the appearance here
            value.setBackground(isSelected ? getSelectionBackground()
                    : getBackground());
            value.setForeground(isSelected ? getSelectionForeground()
                    : getForeground());
            value.setEnabled(isEnabled());
            value.setFont(getFont());
            value.setFocusPainted(false);
            value.setBorderPainted(true);
            value.setBorder(BorderFactory.createEmptyBorder(0, 10, 5, 0));
            return value;
        }
    }
}

然后是我的滚动窗格,其中具有以下设置(使用Intelliji IDEA UI设计器):

And then there's my scroll pane, which has these settings (using the Intelliji IDEA UI designer):

有什么想法吗?

推荐答案

mainGUI.pluginList.add(jCheckBoxList);
mainGUI.checkList = jCheckBoxList;
jCheckBoxList.setVisible(true);
jCheckBoxList.setVisibleRowCount(10);

在我看来,就像您正在将组件动态添加到可见的GUI中一样.

Looks to me like you are dynamically adding components to a visible GUI.

执行此操作时,基本代码为:

When you do this the basic code is:

panel.add(...);
panel.revalidate();
panel.repaint();

在执行上述代码之前,您应该设置visibleRowCount().

You should set the visibleRowCount() before the above code is executed.

也:

  1. 默认情况下,Swing组件是可见的,因此您不需要setVisible(true).
  2. 您可能要考虑使用单列JTable,因为它已经支持复选框渲染器和编辑器.

我上面给您的解决方案是常规解决方案.滚动窗格是不同的,您只应将一个组件添加到视口中.

The solution I gave you above is the general solution. A scroll pane is different, you should only ever add a component to the viewport.

基于不正确的解决方案,您应该使用:

Based on your incorrect solution you should be using:

//mainGUI.pluginList.add(jCheckBoxList);
mainGUI.pluginList.setViewportView(jCheckBoxList);

仅发布几行随机代码的问题是我们不知道代码的完整上下文.我没有意识到"pluginList"实际上是一个滚动窗格.通常,变量名称的名称中会包含滚动或滚动窗格.

The problem with posting only a few random lines of code is that we don't know the full context of the code. I did not realize "pluginList" was actually a scrollpane. Usually the variable name will have scroll or scrollpane in the name.

这篇关于JList中的组件被White Square Thing隐藏直到被单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆