Java Code Examples for javax.swing.JScrollPane#addComponentListener()

The following examples show how to use javax.swing.JScrollPane#addComponentListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GuiUtils.java    From sc2gears with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: EditSpeciesService.java    From Shuffle-Move with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: EditTeamService.java    From Shuffle-Move with GNU General Public License v3.0 6 votes vote down vote up
@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;
}
 
Example 4
Source File: EditRosterService.java    From Shuffle-Move with GNU General Public License v3.0 6 votes vote down vote up
@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;
}
 
Example 5
Source File: SummaryTablePanel.java    From nmonvisualizer with Apache License 2.0 4 votes vote down vote up
@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;
                    }
                }
            });
}