简体   繁体   English

JCheckBox弄乱了JTable的选择

[英]JCheckBox messing up JTable's selection

I have a JTable in a Swing application. 我在Swing应用程序中有一个JTable I have a JCheckBox over it to toggle the visibility of a column. 我有一个JCheckBox来切换列的可见性。 Now, whenever I select a row from the table and move the mouse over the checkbox, the selection of the table seems to disappear. 现在,每当我从表格中选择一行并将鼠标移到复选框上方时,表格的选择似乎就会消失。

I also added a ListSelectionListener to the table in my application. 我还向应用程序中的表添加了ListSelectionListener When I normally select a cell with the table, it gives out two change events (one for mouse down, one for mouse up). 通常,我在表格中选择一个单元格时,它将发出两个更改事件(一个按下鼠标,一个按下鼠标)。 However, when the weirdness described above happens, I get four events. 但是,当发生上述怪异现象时,我得到了四个事件。

怪异1

怪异2

Here's a simplified example that produces the effect: 这是一个产生效果的简化示例:

import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class JTableSelection extends JFrame {
    public JTableSelection() {
        super("Test");
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {}
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        JTable table = new JTable();
        table.setModel(new DefaultTableModel(new String[] { "item", "another",
                "one more" }, 3));
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        add(new JCheckBox("Example"), BorderLayout.PAGE_START);
        add(new JScrollPane(table), BorderLayout.CENTER);
    }
    public static void main(String[] args) {
        new JTableSelection().setVisible(true);
    }
}

Why does the selection glitch out like this (or does anyone know)? 为什么选择会像这样出现故障(或有人知道)? How can I make it work? 我该如何运作?

In case it matters, the Java VM version running this is: 万一重要,运行此代码的Java VM版本为:

java version "1.7.0_67"
Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

EDIT. 编辑。 I discovered a weird behaviour of this. 我发现了这种奇怪的行为。 The effect vanishes completely when maximizing the frame and reappears only by restarting the application. 当最大化帧时,效果将完全消失,并且仅通过重新启动应用程序才能重新出现。

@not an answer Java7/Win8 @不是答案Java7 / Win8

the selection of the table seems to disappear. - I can't to simulating (should be an issue by using Nimbus L&F) -我无法模拟(使用Nimbus L&F应该是一个问题)

mouse_hover_over mouse_hover_over

在此处输入图片说明

JCheckBox.isArmed JCheckBox.isArmed

在此处输入图片说明

JCheckBox.isSelected JCheckBox.isSelected

在此处输入图片说明

.

.

Java1.7.0_67/Win7_64b Java1.7.0_67 / Win7_64b

在此处输入图片说明

.

在此处输入图片说明

.

在此处输入图片说明

Code runs fine on Linux, with or without the scroll pane around the table 无论是否在表格周围使用滚动窗格,代码在Linux上均可正常运行

I have to agree with Andrew's comment though: 我必须同意安德鲁的评论:

  • either put the JTable into a JScrollPane 要么将JTable放入JScrollPane
  • or make sure you add the header yourself as shown in the JTable tutorial 或确保您自己按照JTable教程中所示添加标头

     add(new JCheckBox("Example"), BorderLayout.PAGE_START); JPanel tablePanel = new JPanel(new BorderLayout()); tablePanel.add(table.getTableHeader(), BorderLayout.PAGE_START ); tablePanel.add(table, BorderLayout.CENTER ); add(tablePanel, BorderLayout.CENTER); 

Maybe you need to call JFrame#pack() after JFrame#add(...) and before JFrame#setVisible(true) . 也许您需要在JFrame#add(...)JFrame#setVisible(true)之前调用JFrame#pack() JFrame#setVisible(true)

How to Make Frames (Main Windows) (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) 如何制作框架(主窗口)(Java™教程>使用JFC / Swing创建GUI>使用Swing组件)
Creating and Showing Frames 创建和显示框架
4. The pack method sizes the frame so that all its contents are at or above their preferred sizes. 4.包装方法调整框架的大小,以使框架中的所有物品均在其首选尺寸以上。 An alternative to pack is to establish a frame size explicitly by calling setSize or setBounds (which also sets the frame location). 打包的另一种方法是通过调用setSize或setBounds(也设置帧位置)显式建立帧大小。

1.7.0_72 on Windows 7 x64 在Windows 7 x64上为1.7.0_72
Same or similar thing happens JButton + JScrollPane + JTree : 发生相同或相似的事情JButton + JScrollPane + JTree

在此处输入图片说明

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

public class JTreeSelection extends JFrame {
  public JTreeSelection() {
    super("Test");
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {}
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLayout(new BorderLayout());
    add(new JButton("Example"), BorderLayout.PAGE_START);
    add(new JScrollPane(new JTree()), BorderLayout.CENTER);
    //pack();
    //setSize(320, 240);
  }
  public static void main(String[] args) {
    //You may also need to understand the EDT
    //EventQueue.invokeLater(new Runnable() {
    //  @Override public void run() {
    //      new JTreeSelection().setVisible(true);
    //  }
    //});
    new JTreeSelection().setVisible(true);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM