Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to make a custom swing component

  1. #1
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Unhappy How to make a custom swing component

    I tried to make a custom swing component. I managed to get it to draw it right. However, there is a problem. It still has a rectangle border. I want it to have the border of the shape passed to it.

    I KNOW there HAS to be some way of making a border other than a rectangle. (Otherwise, they wouldn't be able to have JRadioButton.)

    I've tried paintBorder and even tried messing with the ComponentUI. Nothing.

    I know there probably is a way to set the thing so that it would only handle an event if I clicked within the bounds that I painted. However, that doesn't deal with the fact that it's still using a rectangle border and hence would still be taking up the extra space.

    (I think I could handle the clicking thing by using contains() in Shape and checking, using a MouseListener or something, to see if the point clicked is inside said shape.)

    I'm trying to make a triangle component. It's got the triangle form, but, as you can see, as long as you click inside the rectangular bounds, it's still printing "Click".

    It was hard enough to find something online to help me draw the new component. Finding something that helps reshape the border has proven fruitless so far.

    I was more or less going for a shaped button (but doing it as a JComponent subclass could make it apply to more things so I'm keeping it JComponent for now.)

    However, I hate this rectangular bounds.

    I found this code example online. It was drawing something else. However, I don't think it modified the borders either. I made it so that it would draw a shape.

    However, making the border stay with the shape is proving elusive.

     
    package javaswing;
     
    import java.awt.Dimension;
    import java.awt.Graphics;
    import javax.swing.Box;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseEvent;
     
    /**
     *
     * @web [url=http://java-buddy.blogspot.com/]Java-Buddy[/url]
     */
    public class JavaTestSwing {
     
       static JFrameWin jFrameWindow;
     
       public static class MyComponent extends JComponent implements MouseListener{
     
       //   Dimension myDimension = new Dimension(300, 100);
     
          private java.awt.Shape shape;
     
          private class MyComponentUI extends javax.swing.plaf.ComponentUI
          {
     
          private java.awt.Shape shape;
     
          public MyComponentUI(java.awt.Shape shape)
          {
          this.shape = shape;
          }
     
          @Override
          public void paint(Graphics g, JComponent c)
          {
          java.awt.Graphics2D g2d = (java.awt.Graphics2D) g;
     
             g2d.draw(shape);
     
     
          }
     
     
          }
     
          public MyComponent(java.awt.Shape shape)
          {
             this.shape = shape;
     
             addMouseListener(this);
     
             setBorder(new javax.swing.border.EtchedBorder(java.awt.Color.RED, java.awt.Color.BLUE));
     
             setUI(new MyComponentUI(shape));
     
          }
     
       /*
          @Override
          public Dimension getPreferredSize() {
             return myDimension;
          }
     
          @Override
          public Dimension getMaximumSize() {
             return myDimension;
          }
     
          @Override
          public Dimension getMinimumSize() {
             return myDimension;
          }
          */
     
     
     
          @Override
     
          protected void paintBorder(Graphics g)
          {
     
          java.awt.Graphics2D g2d = (java.awt.Graphics2D) g;
     
             g2d.draw(shape);
     
          }
     
     
     
          @Override
          protected void paintComponent(Graphics g) {
          /*
             g.drawLine(0, 0, getWidth()-1, getHeight()-1);
             g.drawLine(0, getHeight()-1, getWidth()-1, 0);
             g.drawRect(0, 0, getWidth()-1, getHeight()-1);
             */
     
            /*
             g.drawLine(0, 0, 100, 100);
             g.drawLine(100, 100, 0, 200);
             g.drawLine(0, 200, 0, 0);
             */
     
             java.awt.Graphics2D g2d = (java.awt.Graphics2D) g;
     
             g2d.draw(shape);
     
     
          }
     
          public  void 	mouseClicked(MouseEvent e)
          {
     
          System.out.println("Clicked");
          }
          public void 	mouseEntered(MouseEvent e)
          {
     
          }
     
          public void 	mouseExited(MouseEvent e)
          {
     
          }
     
          public void 	mousePressed(MouseEvent e)
          {
     
     
          }
     
          public void 	mouseReleased(MouseEvent e)
          {
     
     
          }
     
       }
     
       public static class JFrameWin extends JFrame{
          public JFrameWin(){
             this.setTitle("java-buddy.blogspot.com");
             this.setSize(350, 250);
             this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
             java.awt.Polygon poly = new java.awt.Polygon();
     
             poly.addPoint(0, 0);
             poly.addPoint(100, 100);
             poly.addPoint(0, 200);
     
     
             MyComponent myComponent1 = new MyComponent(poly);
             MyComponent myComponent2 = new MyComponent(poly);
     
             Box verticalBox = Box.createVerticalBox();
     
             verticalBox.add(myComponent1);
             verticalBox.add(myComponent2);
             this.add(verticalBox);
          }
       }
     
     
       public static void main(String[] args){
          Runnable doSwingLater = 
             new Runnable(){
                public void run() {
                   jFrameWindow = new JFrameWin();
                   jFrameWindow.setVisible(true);
                }
             };
     
          SwingUtilities.invokeLater(doSwingLater);
     
       }
     
    }


    I know there has to be a way to do it. You can do it with JFrame too.

    (P.S., I tried the paintBorder method. Doesn't do it.)


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to make a custom swing component

    Thread moved.

  3. #3
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: How to make a custom swing component

    I checked the source code of the JRadioButton, JToggleButton, and AbstractButton classes. There doesn't seem to be anything in the paint methods that deal with reshaping the border to be non-rectangular.

    I know there HAS to be a way to do it because the JRadioButton is circular, not rectangular.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to make a custom swing component

    The "border" of the JRadioButton component is technically not circular, because the component also contains a text label, not just the physical button.
    I'm not sure about how to create a triangular border, but have you tried using the setBorder() method with a custom border of your own?
    If you are wanting to reshape the button itself, I think you may need to dig into the ButtonUI classes. As an example, the JRadioButton uses the BasicRadioButtonUI (Java Platform SE 7 ) class. You may have to create your own UI class for your button.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. The Following User Says Thank You to aussiemcgr For This Useful Post:

    GoodbyeWorld (May 21st, 2014)

Similar Threads

  1. How to create a custom Swing Component?
    By Pahulja7 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 25th, 2011, 10:55 AM
  2. Reason for swing component overlapping?
    By Furious5k in forum AWT / Java Swing
    Replies: 1
    Last Post: November 6th, 2011, 08:40 AM
  3. Replies: 0
    Last Post: November 27th, 2010, 10:47 PM
  4. How to Use the JList component - Java Swing
    By neo_2010 in forum Java Swing Tutorials
    Replies: 1
    Last Post: July 11th, 2009, 04:02 AM
  5. How to Use the JList component - Java Swing
    By neo_2010 in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: July 11th, 2009, 04:02 AM

Tags for this Thread