下面列出了javax.swing.JScrollPane#addComponentListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Creates and returns a scroll panel which wraps the specified view component.<br>
* The returned scroll panel disables vertical scroll bar, and only displays the horizontal scroll bar when the view does not fit
* into the size of the view port. When the view fits into the view port, the scroll pane will not claim the space of the scroll bar.
*
* @param view view to wrap in the scroll pane
* @param parentToRevalidate parent to revalidate when the scroll pane decides to change its size
*
* @return the created self managed scroll pane
*/
public static JScrollPane createSelfManagedScrollPane( final Component view, final JComponent parentToRevalidate ) {
final JScrollPane scrollPane = new JScrollPane( view );
scrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_NEVER );
scrollPane.getHorizontalScrollBar().setPreferredSize( new Dimension( 0, 12 ) ); // Only want to restrict the height, width doesn't matter (it takes up whole width)
scrollPane.getHorizontalScrollBar().setUnitIncrement( 10 );
final ComponentListener scrollPaneComponentListener = new ComponentAdapter() {
@Override
public void componentResized( final ComponentEvent event ) {
scrollPane.setHorizontalScrollBarPolicy( view.getWidth() < scrollPane.getWidth() ? JScrollPane.HORIZONTAL_SCROLLBAR_NEVER : JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS );
scrollPane.setPreferredSize( null );
scrollPane.setPreferredSize( new Dimension( 10, scrollPane.getPreferredSize().height ) );
parentToRevalidate.revalidate();
}
};
scrollPane.addComponentListener( scrollPaneComponentListener );
return scrollPane;
}
private Component makeCenterPanel() {
speciesSelectPanel = new JPanel(new WrapLayout()) {
private static final long serialVersionUID = 5094314715314389100L;
// Fix to make it play nice with the scroll bar.
@Override
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
d.width = (int) (d.getWidth() - 20);
return d;
}
};
final JScrollPane ret = new JScrollPane(speciesSelectPanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
ret.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
ret.revalidate();
}
});
ret.getVerticalScrollBar().setUnitIncrement(27);
return ret;
}
@SuppressWarnings("serial")
private Component makeRosterPanel() {
rosterPanel = new JPanel(new WrapLayout()) {
// Fix to make it play nice with the scroll bar.
@Override
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
d.width = (int) (d.getWidth() - 20);
return d;
}
};
rosterScrollPane = new JScrollPane(rosterPanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
rosterScrollPane.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
rosterScrollPane.revalidate();
}
});
rosterScrollPane.getVerticalScrollBar().setUnitIncrement(27);
return rosterScrollPane;
}
@SuppressWarnings("serial")
private Component makeCenterPanel() {
rosterEntryPanel = new JPanel(new WrapLayout()) {
// Fix to make it play nice with the scroll bar.
@Override
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
d.width = (int) (d.getWidth() - 20);
return d;
}
};
final JScrollPane ret = new JScrollPane(rosterEntryPanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
ret.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
ret.revalidate();
}
});
ret.getVerticalScrollBar().setUnitIncrement(27);
return ret;
}
@SuppressWarnings("unchecked")
public SummaryTablePanel(NMONVisualizerGui gui, JFrame parent) {
super();
this.gui = gui;
this.parent = parent;
fileChooser = new AnalysisSetFileChooser(gui, analysisSet);
menu = new JMenu("Table");
setupMenu(parent);
setLayout(new BorderLayout());
// combo box with various data statistics for use with the results table
statsPanel = new JPanel();
setupStatsPanel();
JPanel top = new JPanel(new BorderLayout());
setupTopPanel(top);
add(top, BorderLayout.PAGE_START);
// each table has its own data model
dataSetTable = new ByDataSetTable(gui);
statisticsTable = new GUITable(gui);
ByDataSetTableModel dataSetTableModel = new ByDataSetTableModel(gui, analysisSet);
ByStatisticTableModel statTableModel = new ByStatisticTableModel(gui, analysisSet);
dataSetTable.setModel(dataSetTableModel);
statisticsTable.setModel(statTableModel);
setupTable(dataSetTable, parent);
setupTable(statisticsTable, parent);
scrollPane = new JScrollPane(statisticsTable);
scrollPane.getViewport().setBackground(java.awt.Color.WHITE);
// could be inefficient to have both tables updating when only 1 is visible...
scrollPane.addComponentListener(new ScrollingTableFix(statisticsTable, scrollPane));
scrollPane.addComponentListener(new ScrollingTableFix(dataSetTable, scrollPane));
scrollPane.setBorder(Styles.createBottomLineBorder(this));
add(scrollPane, BorderLayout.CENTER);
// alert users they can drag onto the tables
JLabel label = new JLabel("Click and drag measurements from the tree onto this table");
label.setFont(Styles.BOLD);
label.setHorizontalAlignment(SwingConstants.CENTER);
add(label, BorderLayout.PAGE_END);
analysisSet.addListener(this);
// check the count column, if it is 0, do not display
((TableRowSorter<ByStatisticTableModel>) statisticsTable.getRowSorter())
.setRowFilter(new RowFilter<ByStatisticTableModel, Integer>() {
@Override
public boolean include(RowFilter.Entry<? extends ByStatisticTableModel, ? extends Integer> entry) {
ByStatisticTableModel model = ((ByStatisticTableModel) entry.getModel());
int idx = model.getColumnIndex(Statistic.COUNT.toString());
if (idx == -1) {
throw new IllegalStateException(
model + "has no column named " + Statistic.COUNT.toString());
}
else {
Object value = model.getEnabledValueAt(entry.getIdentifier(), idx);
int i = (Integer) value;
return i != 0;
}
}
});
}