Results 1 to 20 of 20
  1. #1
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    47
    Rep Power
    0

    Unhappy Trouble creating a component to enable a JToolTip popup

    It's pretty easy to set a tooltip for an existing component like a button. But I can't get tooltips working on my home made components.

    My program defines "blocks" and "pins" which live on the blocks. The pin constructor defines the relative location of each pin. When the block is drawn, then the pin gets its actual screen coordinates which it sends to setBounds(). Well this is all fine, the block and pins draw just fine, however the tooltips do not work. SSCCE below.

    Java Code:
    package painting;
    
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.geom.Ellipse2D;
    import java.io.Serializable;
    import javax.swing.JComponent;
    import painting.blockTest.SpinCADBlock;
    
    public class SpinPin extends JComponent implements Serializable {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = -6926516028763652131L;
    	private int x_pos = -1;
    	private int y_pos = -1;
    	private SpinCADBlock block = null;
    
    	// the register value is assigned during code generation
    	private Color pinColor;
    	private String name = "";
    
    	public SpinPin() {
    		setToolTipText("Default");
    		setEnabled(true);
    	}
    
    	/**
    	 * @param args
    	 */
    
    	public SpinPin(String pinName, int x, int y) {
    //		block = b;
    		name = pinName;
    		setToolTipText(name);
    		setEnabled(true);
    		x_pos = x;
    		y_pos = y;
    		setVisible(false);
    		setBackground(Color.BLUE);
    	}
    
    	public void checkXY() {
    		int x = getX();
    		int y = getY();
    		System.out.printf("X= %d, Y = %d\n", x,y);		
    	}
    
    	public void paint(Graphics2D g2) {
    		int size = 10;
    		setBounds(block.getX() + x_pos - size/2, block.getY() + y_pos - size/2, size, size);
    		checkXY();
    		Ellipse2D boundingRect = new Ellipse2D.Double(block.getX() + x_pos - size/2, block.getY() + y_pos - size/2, size, size);
    		g2.setColor(pinColor);
    		g2.setStroke(new BasicStroke(2));
    		g2.draw(boundingRect);
    	}
    
    	public void setColor(Color c) {
    		pinColor = c;
    	}
    
    	public void setBlock(SpinCADBlock b) {
    		block = b;
    	}
    }
    Java Code:
    package painting;
    
    import java.awt.BasicStroke;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.RenderingHints;
    import java.awt.geom.Rectangle2D;
    import java.awt.geom.RoundRectangle2D;
    import java.util.ArrayList;
    import java.util.Iterator;
    
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class blockTest {
    	JFrame frame = new JFrame();
    	BlockPanel panel = new BlockPanel();
    	SpinCADBlock testBlock = new SpinCADBlock(40,20);
    
    	blockTest () {
    		frame.add(panel, BorderLayout.CENTER);
    		frame.setVisible(true);
    		frame.setTitle("SpinCADPin Designer");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setBounds(100, 100, 200, 120);
    		testBlock.setName("Test Block");
    		testBlock.addControlInputPin(testBlock);
    		testBlock.addControlInputPin(testBlock);
    		testBlock.addInputPin(testBlock);
    		testBlock.addOutputPin(testBlock);
    		testBlock.addOutputPin(testBlock);
    		testBlock.addOutputPin(testBlock);
    
    		//		testBlock.drawRect(g2);
    	}
    
    	public class BlockPanel extends JPanel {
    		/**
    		 * 
    		 */
    		private static final long serialVersionUID = 1L;
    
    		public void paintComponent( Graphics g ) {
    			super.paintComponent(g);
    			Graphics2D g2 = (Graphics2D)g; 
    			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    					RenderingHints.VALUE_ANTIALIAS_ON);
    			SpinCADBlock block;
    			block = testBlock;
    			block.drawRect(g2);	   
    			Iterator<SpinPin> itrPin = block.pinList.iterator();
    			SpinPin nPin;
    			while(itrPin.hasNext()) {
    				nPin = itrPin.next();
    				nPin.setBlock(block);
    				nPin.setColor(Color.BLACK);
    				nPin.paint(g2);
    			}
    		}
    	}
    
    	public class SpinCADBlock extends JComponent {
    
    		/**
    		 * 
    		 */
    		private static final long serialVersionUID = -3067055375662565795L;
    
    		// used to sort the blocks, assigned during addBlock
    		int blockNum = -1;
    
    		// these values are to be used for register addresses?
    		int nInputs = 0;
    		int nOutputs = 0;
    		int nControlInputs = 0;
    		int nControlOutputs = 0;
    
    		public ArrayList<SpinPin> pinList = new ArrayList<SpinPin>();
    
    		// position and size of the enclosing rectangle
    		int width = 80;
    		int height = 40;
    
    		private String name = "Block";
    
    		Color borderColor = Color.green;
    		Color connectorColor = Color.blue;
    
    		public SpinCADBlock(int x, int y) {
    			super();
    			nInputs = 0;
    			nOutputs = 0;
    			nControlInputs = 0;
    			nControlOutputs = 0;
    			setBounds(x, y, width, height);
    			setToolTipText("Block");
    			setEnabled(true);
    		}
    
    		public void addInputPin(SpinCADBlock b) {
    			nInputs++;
    			SpinPin pin = new SpinPin("Audio Input " + nInputs, nInputs * 20, 0);
    			pinList.add(pin);
    		}
    
    		public void addOutputPin(SpinCADBlock b) {
    			nOutputs++;
    			SpinPin pin = new SpinPin("Audio Output " + nOutputs, nOutputs * 20, height);
    			pinList.add(pin);
    		}
    
    		public void addControlInputPin(SpinCADBlock b) {
    			nControlInputs++;
    			SpinPin pin = new SpinPin("Control Input " + nControlInputs, 0, nControlInputs * 20);
    			pinList.add(pin);
    		}
    
    		public void addControlOutputPin() {
    			nControlOutputs++;
    			SpinPin pin = new SpinPin("Control Output " + nControlOutputs, width, nControlOutputs * 20);
    			pinList.add(pin);
    		}
    
    		public SpinPin getPin(String name) {
    			SpinPin nPin = new SpinPin(null, -1, -1);
    			Iterator<SpinPin> itr = pinList.iterator();
    
    			while(itr.hasNext()) {
    				nPin = itr.next();
    				if(name.equals(nPin.getName())) {
    					return nPin;
    				}
    			}
    			return null;		
    		}
    
    		public void sizeRect(Graphics2D g2) {
    			Font f = new Font("Serif", Font.BOLD, 12);
    			g2.setFont(f);
    			//		FontMetrics metrics = g2.getFontMetrics(f);
    			//		int adv = metrics.stringWidth(name);
    
    			// width = (Math.max(nInputs, nOutputs) + 1) * 15  + adv;
    			// height = (Math.max(nControlInputs, nControlOutputs) + 3) * 15;
    			width = 80;
    			height = 40;
    		}
    
    		public void drawRect(Graphics2D g2) {
    
    			sizeRect(g2);
    			//		Iterator<SpinCADPin> itr = pinList.iterator();
    			//		SpinCADPin nPin = new SpinCADPin(null, null, -1, -1, null);
    			//		while(itr.hasNext()) {
    			//			nPin = itr.next();
    			//			nPin.drawPin(g2);
    			//		}
    
    			Rectangle2D rectHandle = new Rectangle2D.Double(getX() - 4 , getY() - 4, 8, 8);
    			g2.setColor(Color.BLACK);
    			g2.setStroke(new BasicStroke(2));
    			g2.draw(rectHandle);
    
    			RoundRectangle2D rect = new RoundRectangle2D.Double(getX() + 3, getY() + 5, width - 6, height - 10, 5, 5);
    			g2.setColor(borderColor);
    			g2.setStroke(new BasicStroke(4));
    			g2.draw(rect);
    			GradientPaint redtowhite = new GradientPaint(getX(), getY(),borderColor, getX() + width, getY() + height, Color.WHITE);
    			g2.setPaint(redtowhite);
    			g2.fill(rect);
    
    			Point pt = new Point(0,0);
    			pt.setLocation(rect.getMinX() + 5, 5 + (rect.getMaxY() + rect.getMinY())/2);
    			//		System.out.println("Text:" + pt.getX() + pt.getY());
    			g2.setColor(Color.BLACK);
    			g2.drawString(name,  (int)pt.getX(), (int)pt.getY());
    		}
    
    		// returns the X,y associated with the current block's given pin 
    		public Point getPinXY(SpinPin p) {
    			Point pt = new Point();
    			pt.setLocation(getX() + p.getX(), getY() + p.getY());
    			return pt;
    		}
    
    		// returns the Point location of the current block's drag/delete handle
    		public Point getXY() {
    			Point pt = new Point();
    			pt.setLocation(getX(), getY());
    			return pt;
    		}
    
    	}
    	public static void main(String[] args) {
    		new blockTest();
    
    	}
    }
    Last edited by Digital Larry; 05-26-2013 at 01:03 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default Re: Trouble creating a component to enable a JToolTip popup

    Did you register your SpinPin component(s) at the ToolTipManager?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Trouble creating a component to enable a JToolTip popup

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Trouble creating a component to enable a JToolTip popup

    Quote Originally Posted by JosAH View Post
    Did you register your SpinPin component(s) at the ToolTipManager?

    kind regards,

    Jos
    setToolTipText(...) takes care of that, so that's not it.

    From JComponent.java:
    Java Code:
        public void setToolTipText(String text) {
            String oldText = getToolTipText();
            putClientProperty(TOOL_TIP_TEXT_KEY, text);
            ToolTipManager toolTipManager = ToolTipManager.sharedInstance();
            if (text != null) {
    	    if (oldText == null) {
                    toolTipManager.registerComponent(this);
    	    }
            } else {
                toolTipManager.unregisterComponent(this);
            }
        }
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Trouble creating a component to enable a JToolTip popup

    Can you post an SSCCE to demonstrate the problem? Nothing superfluous, probably just one block with just one pin, and all in one source file.

    Some other notes:
    -- repeatedly constructing a new Font(...) is computationally expensive. (line 144) All you need is one Font instance, preferably as a final static field.
    -- There are some other local variables that could better be final static fields, like the BasicStroke(n) in lines 167 and 172.-- Point can be constructed with coordinates, making the code cleaner. Also, on line 178 the zero coordinates passed are redundant.
    -- Point can be constructed with coordinates, making the code cleaner. Also, on line 178 the zero coordinates passed are redundant.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Trouble creating a component to enable a JToolTip popup

    Why does the second constructor of SpinPin have a setVisible(false)? I don't see anywhere that the pin is setVisible(true) for it to be seen on screen and receive input events. A tooltip will not be shown for an invisible component.

    Also, for custom painting in Swing the method of choice to override is paintComponent(...) not paint(...)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: Trouble creating a component to enable a JToolTip popup

    Doub its related to your problem but , in your SpinPin class:

    - don't override paint(). Custom painting is done in the paintComponent() method. Don't forget to invoke super.paintComponent();
    - don't invoke setBounds(...) (or any other method that changes a component property) in any painting method. When you change a property the component will be repainted so you can potentially introduce an infinite loop.

  8. #8
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    47
    Rep Power
    0

    Default Re: Trouble creating a component to enable a JToolTip popup

    OK I have cleaned up the SSCCE. I think I've incorporated all of the advice given so far.

    Java Code:
    package painting;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.io.Serializable;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class blockTest {
    	JFrame frame = new JFrame();
    	JPanel panel = new JPanel();
    
    	blockTest () {
    		frame.add(panel, BorderLayout.CENTER);
    		frame.setVisible(true);
    		frame.setTitle("SpinCADPin Designer");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setBounds(100, 100, 200, 120);
    		panel.add(new SpinPin("Pin", 40, 50));
    		frame.repaint();
    	}
    
    	public class SpinPin extends JComponent implements Serializable {
    		/**
    		 * 
    		 */
    		private static final long serialVersionUID = -6926516028763652131L;
    		private int x_pos = -1;
    		private int y_pos = -1;
    		private String name = "";
    		int size = 10;
    
    		public SpinPin(String pinName, int x, int y) {
    			super();
    			name = pinName;
    			x_pos = x;
    			y_pos = y;
    			setBounds(x_pos - size/2, y_pos - size/2, size, size);
    			setToolTipText(name);
    			setEnabled(true);
    			setVisible(true);
    			setBackground(Color.BLUE);
    		}
    
    		public void checkXY() {
    			int x = getX();
    			int y = getY();
    			System.out.printf("X= %d, Y = %d\n", x,y);		
    		}
    
    		public void paintComponent(Graphics g2) {
    			super.paintComponent(g2);
    			checkXY();
    			g2.setColor(Color.RED);
    			g2.drawRect(x_pos - size/2, y_pos - size/2, size, size);
    		}
    	}
    
    	public static void main(String[] args) {
    		new blockTest();
    
    	}
    }
    Last edited by Digital Larry; 05-26-2013 at 09:53 PM.

  9. #9
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: Trouble creating a component to enable a JToolTip popup

    Well, it worked for me. But I removed your setBounds statements and put in setPreferredSize values.

    Edit: The problem of this SSSCCE wasn't tooltip. It was that the SpinPin didn't appear. And it takes about
    2 seconds for the tip to appear. I believe you are using your setBounds parameters as the starting coordinates
    in which to draw your rectangle. But the two coordinate systems are different. So the rectangle doesn't appear.

    Regards,
    Jim
    Last edited by jim829; 05-26-2013 at 11:22 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Trouble creating a component to enable a JToolTip popup

    Some more notes:
    Implementing Serializable isn't needed for an SSCCE.
    Initializing fields that are in any case set in the constructor is superfluous.
    JComponent is visible and enabled by default. These don't need to be set explicitly in the SplitPin constructor.
    JComponent doesn't have a UI delegate, so its paintComponent(...) method does nothing. The background color set is never painted. Either extend JPanel or paint the background explicitly in the paintComponent override.
    setBounds(...) has no effect when a layout manager is in effect. Your JPanel has a default FlowLayout.
    A JFrame should normally be setVisible(true) only after adding all content. Then you wouldn't need to repaint() the frame.
    JFrame can be constructed with a title, reducing one more line of code.
    Empty doc comment blocks are just clutter.
    Class names should start with an uppercase letter.
    A package statement is irrelevant for most SSCCEs, including this one.
    All Swing constructors and methods should be invoked on the EDT.

    With those changes and a few more, this is the updated SSCCE:
    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class BlockTest {
    
      BlockTest() {
        JFrame frame = new JFrame("SpinCADPin Designer");
        frame.setLayout(null);
        frame.add(new SpinPin("Pin", 40, 50));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(100, 100, 200, 120);
        frame.setVisible(true);
      }
    
      public class SpinPin extends JPanel {
    
        private int x_pos;
        private int y_pos;
        private String name;
        private static final int SIZE = 10;
    
        public SpinPin(String pinName, int x, int y) {
          name = pinName;
          x_pos = x;
          y_pos = y;
          setBounds(x_pos - SIZE / 2, y_pos - SIZE / 2, SIZE, SIZE);
          setToolTipText(name);
          setBackground(Color.BLUE);
        }
    
        public void checkXY() {
          int x = getX();
          int y = getY();
          System.out.printf("X= %d, Y = %d\n", x, y);
        }
    
        @Override
        public void paintComponent(Graphics g) {
          super.paintComponent(g);
          checkXY();
          g.setColor(Color.RED);
          g.drawRect(x_pos - SIZE / 2, y_pos - SIZE / 2, SIZE, SIZE);
        }
      }
    
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
    
          @Override
          public void run() {
            new BlockTest();
          }
        });
      }
    }
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    47
    Rep Power
    0

    Default Re: Trouble creating a component to enable a JToolTip popup

    Thanks a lot for the comments. Believe it or not, in spite of my obviously lacking Java skills, my actual program has reached a pretty satisfying level of functionality and very few of its noticeable issues are due to bad Java skills! (he said)

    This all came about because I simply wanted to add tooltips to my Pins. I saw that the JTooltip was associated with the JComponent, so I sought to make my Pins support JTooltips by extending JComponent. Hierarchically, my Pins are part of a Block and the blocks are part of a Model, which is a bunch of Blocks linked together by Connections. The Pins' coordinates are relative to the block they are part of, and when it comes time to paint everything, I calculate the Pin's location by adding the Block's coordinates to the Pin's coordinates. Each pin also contains a reference to the block that created it, so I can also reference the offset that way.

    So I'm interested in your solution to see that you have extended JPanel to create the Pin class. While I'm interested in "whatever works" (hence my coding lack of style) I am also still wondering whether it's possible to enable JTooltip support on some arbitrary object merely by extending JComponent and doing a few things to make that work.

    Thanks again!

    DL
    Last edited by Digital Larry; 05-27-2013 at 02:01 PM.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Trouble creating a component to enable a JToolTip popup

    I already told you why I made that change.
    JComponent doesn't have a UI delegate, so its paintComponent(...) method does nothing. The background color set is never painted.
    Each component in the GUI should take care of its own painting. There's no need to calculate offsets, the Swing painting mechanism does that for you.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: Trouble creating a component to enable a JToolTip popup

    I am also still wondering whether it's possible to enable JTooltip support on some arbitrary object merely by extending JComponent
    Yes, all Swing components support tooltips without adding any code:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class SSCCEToolTip extends JComponent
    {
    	private static void createAndShowUI()
    	{
    		JComponent component = new SSCCEToolTip();
    		component.setToolTipText("Hello");
    
    		JFrame frame = new JFrame("SSCCEToolTip");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.add( component );
    		frame.setSize(200, 200);
    		frame.setLocationByPlatform( true );
    		frame.setVisible( true );
    	}
    
    	public static void main(String[] args)
    	{
    		EventQueue.invokeLater(new Runnable()
    		{
    			public void run()
    			{
    				createAndShowUI();
    			}
    		});
    	}
    }
    The above code works because I added the component to a BorderLayout so it automatically gets resized to all the available space.

    In your case when you actually do custom painting, you will want to override the getPreferredSize() method to return the size of the component taking into account the size of your custom painting.

  14. #14
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    47
    Rep Power
    0

    Default Re: Trouble creating a component to enable a JToolTip popup

    Quote Originally Posted by DarrylBurke View Post
    I already told you why I made that change.

    Each component in the GUI should take care of its own painting. There's no need to calculate offsets, the Swing painting mechanism does that for you.

    db
    Thanks.

    I tried your code and the red rectangle does not paint. Only the blue background paints. I had stuck that background paint call in there, out of frustration, just to see if anything would happen. In reality I don't need the blue background.

    In order for Swing to know where the component is though, we need to set the desired location. Which is better for this, setBounds() or setLocation()?

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Trouble creating a component to enable a JToolTip popup

    Quote Originally Posted by Digital Larry View Post
    I tried your code and the red rectangle does not paint. Only the blue background paints.
    Didn't you read Jim's response at #9?
    I believe you are using your setBounds parameters as the starting coordinates
    in which to draw your rectangle. But the two coordinate systems are different. So the rectangle doesn't appear.
    Quote Originally Posted by Digital Larry View Post
    In order for Swing to know where the component is though, we need to set the desired location. Which is better for this, setBounds() or setLocation()?
    For most GUIs, neither. A Layout Manager would take care of positioning and sizing the components.

    setBounds(...) sets the size as well as the location; setLocation(...) doesn't affect the size. Use the method that's appropriate to your need.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  16. #16
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    47
    Rep Power
    0

    Default Re: Trouble creating a component to enable a JToolTip popup

    I'm going to try to study all of this stuff before asking more stupid questions.

    I thought I would show you what my program really does, at least visually. It is a visual CAD system, the output of which is DSP assembler for audio effects. For this reason, I do not want to use a layout manager in the central area.

    The extended JPanel paintComponent() method currently handles drawing the entire network. I hadn't made the blocks nor the pins extensions of JComponent because prior to wanting to use JTooltips, there was no reason for doing so.



    Uploaded with ImageShack.us
    Last edited by Digital Larry; 05-27-2013 at 07:35 PM.

  17. #17
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: Trouble creating a component to enable a JToolTip popup

    I'm curious about something. Your statement about not wanting to use a layout manager implies to me that you are using individual Swing components as your audio components and placing them using setBounds(). Am I correct?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  18. #18
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    47
    Rep Power
    0

    Default Re: Trouble creating a component to enable a JToolTip popup

    Quote Originally Posted by jim829 View Post
    I'm curious about something. Your statement about not wanting to use a layout manager implies to me that you are using individual Swing components as your audio components and placing them using setBounds(). Am I correct?

    Regards,
    Jim
    Hi Jim,

    The blocks and pins (circles) and connections (lines) that you see are not Swing components at all. Every time I need to redraw the screen, I iterate through the master data structure which is called a "Model". None of these were "added" as components to the JPanel. It doesn't know anything about them explicitly. In fact the Model is declared within the enclosing frame as other actions need to take place on the model that are not within the scope of the JPanel. I'm sure it's all been terribly designed but it does actually work. My real interest is in the DSP area of the project and I am stumbling through the Java/Swing aspects of it.

    The representation of the Model is all that is drawn in that central area so I draw it via brute force using Graphics2D draw for lines, rounded rectangles, and ellipses.

  19. #19
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: Trouble creating a component to enable a JToolTip popup

    Well, for what its worth, that's probably how I would do it. I have done it both ways. In the past when I did some experimentation moving JComponents around with the mouse, I ended up having to redispatch the mouse events. I believe drawing the components explicitly and tracking them in a data structure is much easier.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  20. #20
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    47
    Rep Power
    0

    Default Re: Trouble creating a component to enable a JToolTip popup

    Ultimately I implemented what I was after by adding functionality to my own MouseMoved handler. This took me about 15 minutes after I decided to do it. Fortunately I already had a fairly elaborate MouseMoved handler and so it was just a matter of adding a bit to it. Trying to get my data model components to be Swing JComponents looks like it would have added a lot of useless overhead.

Similar Threads

  1. Calling methods of a component from a different component
    By Mate de Vita in forum AWT / Swing
    Replies: 0
    Last Post: 10-26-2012, 08:15 PM
  2. JSF2: Creating Custom Component [OutputObject]
    By adevedo in forum Reviews / Advertising
    Replies: 1
    Last Post: 10-07-2012, 09:29 AM
  3. how to enable JButton?
    By sandeep43 in forum AWT / Swing
    Replies: 4
    Last Post: 05-30-2011, 06:50 AM
  4. Creating popup menus with Swing
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:49 PM
  5. add JToolTip on focus gained forJTextField
    By ismailsaleh in forum AWT / Swing
    Replies: 6
    Last Post: 02-01-2008, 12:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •