2 Replies Latest reply on Mar 25, 2011 11:41 AM by 802561

    Using text and a progress bar inside of a tooltip.

    802561
      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.
        • 1. Re: Using text and a progress bar inside of a tooltip.
          kleopatra-JavaNet
          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
          • 2. Re: Using text and a progress bar inside of a tooltip.
            802561
            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