Forum Stats

  • 3,734,207 Users
  • 2,246,909 Discussions
  • 7,857,181 Comments

Discussions

Using text and a progress bar inside of a tooltip.

802561
802561 Member Posts: 47
edited Mar 25, 2011 7:41AM in Swing
Hi,

I'm trying to get my tool tip to appear as: "Percentage: progressBar," but am not sure how to do so. I've made it so that the progress bar shows up with help from some users on this forum, but am not sure how to use text with it. Here is what I have:
import java.awt.Dimension;
import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToolTip;
import javax.swing.JProgressBar;

public class ToolTip {

	private JFrame frame;
	
	private JButton button;
	
	private JToolTip testToolTip;

	private JProgressBar progressBar;

	public static void main(String[] var) {
		new ToolTip();
	}

	public ToolTip() {
		progressBar = new JProgressBar(0, 100);
		progressBar.setValue(5);
		progressBar.setStringPainted(true);
        
		testToolTip = new JToolTip() {
			{
				setLayout(new BorderLayout());
				add(progressBar);
			}

			@Override
			public Dimension getPreferredSize() {
				return new Dimension(150, 20);
			}
		};

		button = new JButton("herp") {
			
			@Override
			public JToolTip createToolTip() {
				return testToolTip;
			}
			
		};
		button.setToolTipText("A");
		
		frame = new JFrame("derp");
		frame.setSize(100, 100);
		frame.getContentPane().add(button);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

}
Re-reading this I'm not so sure my point will get across, so pretty much what I want is something that would be implementing a JLabel and JProgressBar inside the same tool tip, having the label and progress bar showing themselves in their respectful way.

Best Answer

  • kleopatra-JavaNet
    kleopatra-JavaNet Member Posts: 1,105 Gold Badge
    Accepted Answer
    hmm ... not quite sure I understand where you see the problem: what's wrong with adding a component which shows the text you want it to show? Something like
            testToolTip = new JToolTip() {
                JLabel label = new JLabel();
                {
                    setLayout(new BorderLayout());
                    add(progressBar);
                    add(label, BorderLayout.NORTH);
                }
      
             
                /** 
                 * @inherited <p>
                 */
                @Override
                public void setTipText(String tipText) {
                    label.setText(tipText);
                }
    
                @Override
                public Dimension getPreferredSize() {
                    return getLayout().preferredLayoutSize(this);
                }
    
            };
    which shows the tooltiptext as set to the button, could add something showing a fixed text as well or whatever you like to :-)

    Cheers
    Jeanette

Answers

  • kleopatra-JavaNet
    kleopatra-JavaNet Member Posts: 1,105 Gold Badge
    Accepted Answer
    hmm ... not quite sure I understand where you see the problem: what's wrong with adding a component which shows the text you want it to show? Something like
            testToolTip = new JToolTip() {
                JLabel label = new JLabel();
                {
                    setLayout(new BorderLayout());
                    add(progressBar);
                    add(label, BorderLayout.NORTH);
                }
      
             
                /** 
                 * @inherited <p>
                 */
                @Override
                public void setTipText(String tipText) {
                    label.setText(tipText);
                }
    
                @Override
                public Dimension getPreferredSize() {
                    return getLayout().preferredLayoutSize(this);
                }
    
            };
    which shows the tooltiptext as set to the button, could add something showing a fixed text as well or whatever you like to :-)

    Cheers
    Jeanette
  • 802561
    802561 Member Posts: 47
    Kleopatra wrote:
    hmm ... not quite sure I understand where you see the problem: what's wrong with adding a component which shows the text you want it to show? Something like
    which shows the tooltiptext as set to the button, could add something showing a fixed text as well or whatever you like to :-)

    Cheers
    Jeanette
    Thank you very much!! This was exactly what I was looking for. Can't believe I didn't think of how simple that was. :x
This discussion has been closed.