如何使拐角组件始终在JScrollPane中可见 [英] How to make a corner component always visible in a JScrollPane
问题描述
给予
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是否为空,如何使cornerButton始终可见?
how might one make the cornerButton always visible, irrespective of whether the JTable is empty or not?
我看到SwingX中的JXTable完成了此任务,但是我无法从源代码中了解它是如何完成的.
I see that the JXTable in SwingX accomplishes this however, I wasn't able to decipher from the source code how it was done.
谢谢
推荐答案
我的问题是,当ScrollPane的列标题视口中有内容时,JScrollPane仅显示按钮.
My issue is that the JScrollPane only shows the button when there is something in the ScrollPane's column header viewport.
我认为这是一半的问题.您需要在视口中使用一个组件来占用一些空间.
I think that is half the problem. You need a component in the viewport to take up some space.
我尝试在其中放置一个空的JPanel,但这似乎不起作用
I've tried putting an empty JPanel in there but that doesn't seem to work
尝试在视口中使用空的JPanel以及列标题中的面板:
Try the empty JPanel in the viewport along with the panel in the column header:
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);
}
});
}
}
这篇关于如何使拐角组件始终在JScrollPane中可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!