I have a JTable
in a Swing application. I have a JCheckBox
over it to toggle the visibility of a column. 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. 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.
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 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
the selection of the table seems to disappear.
- I can't to simulating (should be an issue by using Nimbus L&F)
mouse_hover_over
JCheckBox.isArmed
JCheckBox.isSelected
.
.
Java1.7.0_67/Win7_64b
.
.
Code runs fine on Linux, with or without the scroll pane around the table
I have to agree with Andrew's comment though:
JTable
into a JScrollPane
or make sure you add the header yourself as shown in the JTable
tutorial
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)
.
How to Make Frames (Main Windows) (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
Creating and Showing Frames
4. The pack method sizes the frame so that all its contents are at or above their preferred sizes. An alternative to pack is to establish a frame size explicitly by calling setSize or setBounds (which also sets the frame location).
1.7.0_72 on Windows 7 x64
Same or similar thing happens 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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.