Example usage for javax.swing JToolTip JToolTip

List of usage examples for javax.swing JToolTip JToolTip

Introduction

In this page you can find the example usage for javax.swing JToolTip JToolTip.

Prototype

public JToolTip() 

Source Link

Document

Creates a tool tip.

Usage

From source file:Main.java

/**
 * Returns an appropriate location for a component's tool tip that <i>always</i>
 * lies within the specified frame./*from   w ww.  j  a  v a 2 s  .  c o m*/
 * <p>
 * Intended be used in custom implementations of {@link JComponent#getToolTipLocation(MouseEvent)}.
 *
 * @param e
 *          the event that caused the display of the tool tip
 * @param c
 *          the parent component of the tool tip
 * @param frame
 *          a component in which the tool tip has to fit (usually the surrounding window of "c")
 * @return
 */
public static Point getAdjustedToolTipLocation(MouseEvent e, JComponent c, Component frame) {
    JToolTip tip = new JToolTip();
    tip.setTipText(c.getToolTipText(e));
    Dimension tipSize = tip.getPreferredSize();
    // Tool tip will be positioned within the bounds of the specified component (+ 5px inset)
    Rectangle frameR = frame.getBounds();
    if (frame instanceof Container) {
        Container container = (Container) frame;
        Insets insets = container.getInsets();
        frameR.x += insets.left;
        frameR.y += insets.top;
        frameR.width -= (insets.left + insets.right);
        frameR.height -= (insets.top + insets.bottom);
    }
    frameR.x += 5;
    frameR.y += 5;
    frameR.width -= 10;
    frameR.height -= 10;
    // Initial try for the tool tip's position
    Rectangle r = new Rectangle(e.getXOnScreen(), c.getLocationOnScreen().y + c.getSize().height + 1,
            tipSize.width, tipSize.height);
    // Check if it fits within the frame
    Rectangle intersection = frameR.intersection(r);
    if (r.equals(intersection)) {
        // Tool tip is fully visible within the frame --> use default behaviour
        //
        // Note: The implementation of ToolTipManager.showTipWindow() is not always
        // correct in dual screen mode. The tool tip is _always_ put on that screen,
        // where the most part of the frame lies upon, even if we return coordinates
        // that clearly belong to the other screen. Unfortunately we cannot change
        // that behavior... (bsh 2010-11-24)
        return null;
    }
    // Otherwise, move the tool tip
    int correction = 0;
    if (r.height == intersection.height) {
        // Height is okay, just move left. To make it look better, position the
        // tip 5px below the component.
        r = new Rectangle(r.x, c.getLocationOnScreen().y + c.getSize().height + 5, tipSize.width,
                tipSize.height);
        correction = -5; // needed to make the ToolTipManager use a lightweight pop-up
    } else {
        // The height does not fit. Position the tool tip above the component.
        r = new Rectangle(c.getLocationOnScreen().x + 10, c.getLocationOnScreen().y - tipSize.height - 1,
                tipSize.width, tipSize.height);
    }
    // Adjust to frame bounds
    intersection = frameR.intersection(r);
    intersection.x -= (r.width - intersection.width);
    intersection.y -= (r.height - intersection.height);
    // Return value is expected to be relative to the component's position
    return new Point((-c.getLocationOnScreen().x) + intersection.x + correction,
            (-c.getLocationOnScreen().y) + intersection.y);
}

From source file:org.griphyn.vdl.karajan.monitor.monitors.swing.GraphPanel.java

private void createChart() {
    chart = ChartFactory.createTimeSeriesChart(null, "Time", null, null, false, false, false);
    cp = new ChartPanel(chart);
    // avoid stretching fonts and such
    cp.setMaximumDrawWidth(Integer.MAX_VALUE);
    cp.setMaximumDrawHeight(Integer.MAX_VALUE);
    XYPlot plot = chart.getXYPlot();/* ww  w . j av  a 2 s  .  c  o m*/
    plot.setBackgroundPaint(UIManager.getColor("TextField.background"));
    plot.setDomainCrosshairVisible(true);
    plot.setDomainCrosshairLockedOnData(false);
    cp.addChartMouseListener(this);
    cp.setLayout(new DummyLayoutManager());
    cp.add(toolTip = new JToolTip());
    toolTip.setVisible(false);

    cp.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseExited(MouseEvent e) {
            disableToolTip();
        }
    });
    chart.addChangeListener(new ChartChangeListener() {
        @Override
        public void chartChanged(ChartChangeEvent e) {
            if (e.getType() == ChartChangeEventType.DATASET_UPDATED) {
                updateMaxRange();
                updateToolTipLocation();
            }
        }
    });

    for (String s : enabled) {
        addSeries(sampler.getSeries(s));
    }

    JPanel p = new JPanel();
    p.setLayout(new BorderLayout());
    p.add(cp, BorderLayout.CENTER);
    legend = new JPanel();
    legend.setLayout(new FlowLayout());

    rebuildLegend();

    this.add(p, BorderLayout.CENTER);
    this.add(legend, BorderLayout.SOUTH);
}

From source file:com.github.alexfalappa.nbspringboot.cfgprops.completion.CfgPropCompletionItem.java

@Override
public CompletionTask createToolTipTask() {
    return new AsyncCompletionTask(new AsyncCompletionQuery() {
        @Override//w  w  w .j av a  2 s . c o  m
        protected void query(CompletionResultSet completionResultSet, Document document, int i) {
            JToolTip toolTip = new JToolTip();
            toolTip.setTipText("Press Enter to insert \"" + getText() + "\"");
            completionResultSet.setToolTip(toolTip);
            completionResultSet.finish();
        }
    });
}

From source file:org.jax.maanova.fit.gui.ResidualPlotPanel.java

/**
 * Constructor//  www .  j a v  a  2  s  .  c o  m
 * @param parent
 *          the parent frame
 * @param fitMaanovaResult
 *          the fitmaanova result that we'll plot residuals for
 */
public ResidualPlotPanel(JFrame parent, FitMaanovaResult fitMaanovaResult) {
    this.chartConfigurationDialog = new SimpleChartConfigurationDialog(parent);
    this.chartConfigurationDialog.addOkActionListener(new ActionListener() {
        /**
         * {@inheritDoc}
         */
        public void actionPerformed(ActionEvent e) {
            ResidualPlotPanel.this.updateDataPoints();
        }
    });

    this.fitMaanovaResult = fitMaanovaResult;
    this.dyeCount = this.fitMaanovaResult.getParentExperiment().getDyeCount();
    this.arrayCount = this.fitMaanovaResult.getParentExperiment().getMicroarrayCount();

    this.setLayout(new BorderLayout());

    JPanel chartAndControlPanel = new JPanel(new BorderLayout());
    this.add(chartAndControlPanel, BorderLayout.CENTER);

    this.chartPanel = new MaanovaChartPanel();
    this.chartPanel.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
    this.chartPanel.addComponentListener(this.chartComponentListener);
    this.chartPanel.addAreaSelectionListener(this.areaSelectionListener);
    this.chartPanel.addMouseListener(this.chartMouseListener);
    this.chartPanel.addMouseMotionListener(this.myMouseMotionListener);
    this.chartPanel.setLayout(null);
    chartAndControlPanel.add(this.chartPanel, BorderLayout.CENTER);

    ItemListener updateDataItemListener = new ItemListener() {
        /**
         * {@inheritDoc}
         */
        public void itemStateChanged(ItemEvent e) {
            ResidualPlotPanel.this.forgetGraphState();
            ResidualPlotPanel.this.updateDataPoints();
        }
    };

    if (this.dyeCount <= 1) {
        this.controlPanel = null;
        this.dyeComboBox = null;
    } else {
        this.dyeComboBox = new JComboBox();
        for (int i = 0; i < this.dyeCount; i++) {
            this.dyeComboBox.addItem("Dye #" + (i + 1));
        }
        this.dyeComboBox.addItemListener(updateDataItemListener);

        this.controlPanel = new JPanel(new FlowLayout());
        this.controlPanel.add(this.dyeComboBox);
        chartAndControlPanel.add(this.controlPanel, BorderLayout.NORTH);
    }

    this.add(this.createMenu(), BorderLayout.NORTH);

    this.forgetGraphState();
    this.updateDataPoints();

    this.toolTip = new JToolTip();
}

From source file:org.jax.maanova.madata.gui.ArrayScatterPlotPanel.java

/**
 * Constructor/*from  ww  w. java  2 s .  com*/
 * @param parent
 *          the parent frame
 * @param experiment
 *          the microarray experiment that we're going to be plotting data
 *          for
 */
public ArrayScatterPlotPanel(JFrame parent, MicroarrayExperiment experiment) {
    this.chartConfigurationDialog = new SimpleChartConfigurationDialog(parent);
    this.chartConfigurationDialog.addOkActionListener(new ActionListener() {
        /**
         * {@inheritDoc}
         */
        public void actionPerformed(ActionEvent e) {
            ArrayScatterPlotPanel.this.updateDataPoints();
        }
    });

    this.experiment = experiment;
    this.dyeCount = experiment.getDyeCount();

    this.setLayout(new BorderLayout());

    JPanel chartAndControlPanel = new JPanel(new BorderLayout());
    this.add(chartAndControlPanel, BorderLayout.CENTER);

    this.chartPanel = new MaanovaChartPanel();
    this.chartPanel.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
    this.chartPanel.addMouseMotionListener(this.myMouseMotionListener);
    this.chartPanel.addMouseListener(this.chartMouseListener);
    this.chartPanel.addComponentListener(this.chartComponentListener);
    this.chartPanel.addAreaSelectionListener(this.areaSelectionListener);
    this.chartPanel.setLayout(null);
    chartAndControlPanel.add(this.chartPanel, BorderLayout.CENTER);

    ItemListener updateDataItemListener = new ItemListener() {
        /**
         * {@inheritDoc}
         */
        public void itemStateChanged(ItemEvent e) {
            ArrayScatterPlotPanel.this.forgetGraphState();
            ArrayScatterPlotPanel.this.updateDataPoints();
        }
    };

    this.array1ComboBox = this.initializeArrayComboBox(this.dyeCount);
    this.array1ComboBox.addItemListener(updateDataItemListener);
    this.array2ComboBox = this.initializeArrayComboBox(this.dyeCount);
    this.array2ComboBox.setSelectedIndex(1);
    this.array2ComboBox.addItemListener(updateDataItemListener);

    this.controlPanel = new JPanel(new FlowLayout());
    this.controlPanel.add(this.array1ComboBox);
    this.controlPanel.add(new JLabel("vs."));
    this.controlPanel.add(this.array2ComboBox);
    chartAndControlPanel.add(this.controlPanel, BorderLayout.NORTH);

    this.add(this.createMenu(), BorderLayout.NORTH);

    this.forgetGraphState();
    this.updateDataPoints();

    this.toolTip = new JToolTip();
}

From source file:org.jax.maanova.test.gui.VolcanoPlotPanel.java

/**
 * Constructor//from ww w  .  j a  v  a  2s  .  co m
 * @param parent the parent frame
 * @param maanovaTestResult
 *          the test result to plot
 * @param initialTestIndex
 *          the initial test index to use
 * @param selectedIndices
 *          the initially selected indices
 */
public VolcanoPlotPanel(JFrame parent, MaanovaTestResult maanovaTestResult, int initialTestIndex,
        int[] selectedIndices) {
    selectedIndices = SequenceUtilities.uniqueInts(selectedIndices);

    this.chartConfigurationDialog = new SimpleChartConfigurationDialog(parent);
    this.chartConfigurationDialog.addOkActionListener(new ActionListener() {
        /**
         * {@inheritDoc}
         */
        public void actionPerformed(ActionEvent e) {
            VolcanoPlotPanel.this.updateDataPoints();
        }
    });

    this.maanovaTestResult = maanovaTestResult;
    this.saveSelectedPointsMenuItem = new JMenuItem("Save Selected Points to Gene List");
    this.saveSelectedPointsMenuItem.setEnabled(selectedIndices != null && selectedIndices.length >= 1);
    this.saveSelectedPointsMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            VolcanoPlotPanel.this.saveSelectedPoints();
        }
    });

    this.displayTestResultsAction = new DisplayTestResultsAction("Show Results Table", maanovaTestResult);
    this.selectedIndices = selectedIndices;

    this.setLayout(new BorderLayout());

    JPanel chartAndControlPanel = new JPanel(new BorderLayout());
    this.add(chartAndControlPanel, BorderLayout.CENTER);

    this.chartPanel = new MaanovaChartPanel();
    this.chartPanel.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
    this.chartPanel.addComponentListener(this.chartComponentListener);
    this.chartPanel.addAreaSelectionListener(this.areaSelectionListener);
    this.chartPanel.addMouseListener(this.chartMouseListener);
    chartAndControlPanel.add(this.chartPanel, BorderLayout.CENTER);

    this.controlPanel = new JPanel(new FlowLayout());

    ItemListener updateDataItemListener = new ItemListener() {
        /**
         * {@inheritDoc}
         */
        public void itemStateChanged(ItemEvent e) {
            VolcanoPlotPanel.this.forgetGraphState();
            VolcanoPlotPanel.this.updateDataPoints();
        }
    };

    this.statisticTypeComboBox = new JComboBox();
    for (MaanovaTestStatisticType testStatType : MaanovaTestStatisticType.values()) {
        this.statisticTypeComboBox.addItem(testStatType);
    }
    this.statisticTypeComboBox.addItemListener(updateDataItemListener);
    this.controlPanel.add(this.statisticTypeComboBox);

    List<MaanovaTestStatisticSubtype> availableTestSubtypes = new ArrayList<MaanovaTestStatisticSubtype>();
    this.statisticSubtypeComboBox = new JComboBox();
    MaanovaTestStatistics fStat = this.maanovaTestResult.getStatistics(MaanovaTestStatisticType.F_STAT);
    for (MaanovaTestStatisticSubtype statSubtype : MaanovaTestStatisticSubtype.values()) {
        if (fStat.hasTestStatistic(statSubtype)) {
            availableTestSubtypes.add(statSubtype);

            if (statSubtype != MaanovaTestStatisticSubtype.F_OBSERVED) {
                this.statisticSubtypeComboBox.addItem(statSubtype);
            }
        }
    }
    this.statisticSubtypeComboBox.addItemListener(updateDataItemListener);
    this.controlPanel.add(this.statisticSubtypeComboBox);

    int testStatCount = availableTestSubtypes.size() * MaanovaTestStatisticType.values().length;
    this.availableTestStatistics = new TestStatisticItem[testStatCount];
    for (int i = 0; i < MaanovaTestStatisticType.values().length; i++) {
        for (int j = 0; j < availableTestSubtypes.size(); j++) {
            int flatIndex = i * availableTestSubtypes.size() + j;
            this.availableTestStatistics[flatIndex] = new TestStatisticItem(
                    MaanovaTestStatisticType.values()[i], availableTestSubtypes.get(j));
        }
    }

    int testCount = fStat.getContrastCount();
    if (testCount == 1) {
        this.testNumberComboBox = null;
    } else {
        this.testNumberComboBox = new JComboBox();
        for (int i = 1; i <= testCount; i++) {
            this.testNumberComboBox.addItem("Test Number " + i);
        }
        this.testNumberComboBox.setSelectedIndex(initialTestIndex);
        this.testNumberComboBox.addItemListener(updateDataItemListener);
        this.controlPanel.add(this.testNumberComboBox);
    }

    chartAndControlPanel.add(this.controlPanel, BorderLayout.NORTH);

    JMenuBar menu = this.createMenu();
    this.add(menu, BorderLayout.NORTH);

    this.forgetGraphState();
    this.updateDataPoints();

    this.chartPanel.addMouseMotionListener(this.myMouseMotionListener);
    this.chartPanel.setLayout(null);

    this.toolTip = new JToolTip();
}