给定
import javax.swing.*;
public class TestCornerComponent {
public static void main(String args[]) {
JTable table = new JTable();
final JScrollPane scrollPane = new JScrollPane(table);
/* button to put in corner */
JButton cornerButton = new JButton("#");
scrollPane.setCorner(JScrollPane.UPPER_TRAILING_CORNER,
cornerButton);
scrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Test corner component");
frame.getContentPane().add(scrollPane);
frame.setVisible(true);
}
});
}
}
如何使角按钮始终可见,无论 JTable 是否为空?
我看到 SwingX 中的 JXTable 实现了这一点,但是我无法从源代码中破译它是如何完成的。
谢谢
最佳答案
My issue is that the JScrollPane only shows the button when there is something in the ScrollPane's column header viewport.
我认为这就是问题的一半。您需要在视口(viewport)中放置一个组件来占用一些空间。
I've tried putting an empty JPanel in there but that doesn't seem to work
尝试视口(viewport)中的空 JPanel 以及列标题中的面板:
import java.awt.*;
import javax.swing.*;
public class TestCornerComponent {
public static void main(String args[])
{
final JScrollPane scrollPane = new JScrollPane();
/* button to put in corner */
JButton cornerButton = new JButton("#");
scrollPane.setCorner(JScrollPane.UPPER_TRAILING_CORNER, cornerButton);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel column = new JPanel();
column.setPreferredSize( new Dimension(100, cornerButton.getPreferredSize().height) );
scrollPane.setColumnHeaderView( column );
JPanel view = new JPanel();
view.setPreferredSize( new Dimension(100, 100) );
scrollPane.setViewportView( view );
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Test corner component");
frame.add(scrollPane);
frame.pack();
frame.setVisible(true);
}
});
}
}
关于java - 如何使角组件在 JScrollPane 中始终可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17088671/