如果我将 a 添加JTable
到 aJPanel
然后将其添加JPanel
到 a JScrollPane
,只要JTable
获得焦点,滚动窗格就会自动滚动到最底部,这很糟糕。
我这样做有很多理由,所以我希望有某种解决方案来阻止这种自动滚动。
哦,这是踢球者......它似乎只在通过 运行应用程序时发生JNLP/WebStart
,它不会在 Eclipse 中这样做,这更令人沮丧。
这是一个快速示例,如果您通过 启动JLNP
,单击文本字段,单击表格,然后它会自动滚动到底部:
import java.awt.*;
import javax.swing.*;
public class ScrollDemo extends JPanel{
private static final long serialVersionUID = 1L;
public ScrollDemo()
{
this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
JTable table = new JTable(100,6);
this.add(new JTextField());
JPanel panel = new JPanel();
panel.add(table);
JScrollPane scroll = new JScrollPane(panel);
this.add(scroll);
}
/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event-dispatching thread.
*/
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("ScrollDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create and set up the content pane.
JComponent newContentPane = new ScrollDemo();
newContentPane.setOpaque(true); // content panes must be opaque
frame.setContentPane(newContentPane);
frame.setPreferredSize(new Dimension(500, 500));
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}