Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By Tolls

Thread: a Button that extends JButton

  1. #1
    alexix is offline Member
    Join Date
    Feb 2013
    Posts
    19
    Rep Power
    0

    Default a Button that extends JButton

    I'm trying to make a Button class with a costomized appearance. Here is my code so far:

    Java Code:
    public class Buton extends JButton implements ActionListener{
    
        public Buton(String name){
         setText(name);
       }
      
       class AscultMouse implements MouseListener{...}
        
      public void paintComponent(Graphics g)
        {   //super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g.create();
            g2.setPaint(new GradientPaint(new Point(0, 0),Color.YELLOW.brighter(),new Point(0, getHeight()),Color.CYAN.darker()));
            g2.fillRoundRect(0, 0, getWidth(), getHeight(), 30, 30);
            g2.drawString(getText(), 0, 0);
           
           // g2.dispose();  
        }
    }
    My button looks how I want it to look, but the problem is it no longer shows the string name of the button on it, I think my painting is on top of the text, how can fix it?
    < g2.drawString(getText(),0,0); > doesn't seem to have any effect.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,791
    Rep Power
    19

    Default Re: a Button that extends JButton

    Aren't you drawing in the same colour as the filled button?
    Try changing the paint colour.
    And you should include the call to super.paintComponent.
    alexix likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    alexix is offline Member
    Join Date
    Feb 2013
    Posts
    19
    Rep Power
    0

    Default Re: a Button that extends JButton

    Thank you. It worked.

    But can you tell me how to set the text on the center of the button?

    g2.drawString(getText(), CENTER, CENTER); is the same as g2.drawString(getText(), 0, 0); which sets the text above the button, and I can't manually enter x and y coordinates because text sizes varies.
    Last edited by alexix; 04-23-2013 at 09:41 PM.

  4. #4
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: a Button that extends JButton

    i believe to set the text position on a button you do

    setHorizontalTextPosition(SwingConstants.CENTER);

    so in your case:
    Java Code:
     public Buton(String name){
         setText(name);
         setHorizontalTextPosition(SwingConstants.CENTER);
       }
    I think that is correct.

  5. #5
    alexix is offline Member
    Join Date
    Feb 2013
    Posts
    19
    Rep Power
    0

    Default Re: a Button that extends JButton

    No, its not, because my paint component paints a rectangle on top of that text, and then I repaint the text with g2.drawString(getText(), x, y);

    And my I can't make my drawString set the text on center everytime (it demends on text length). Any other ideas?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,791
    Rep Power
    19

    Default Re: a Button that extends JButton

    getFontMetrics returns a FontMetrics object.
    That has some handy methods on it for getting the eventual size of the text that will appear in the graphics.
    You can then do a simple bit of maths to figure out how to centre the text image.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,375
    Rep Power
    5

    Default Re: a Button that extends JButton

    Also check out SwingUtilities.computeStringWidth().

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

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,791
    Rep Power
    19

    Default Re: a Button that extends JButton

    Hmm, you still need the FontMetrics for that, and if you have the FontMetrics then I don't see why FontMetrics.stringWidth() is any different.
    I must be missing something.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,375
    Rep Power
    5

    Default Re: a Button that extends JButton

    Nope, I am missing something. I have used Fontmetrics primarily as an argument to SwingUtilities.computeStringWidth() and very little else. In fact, I didn't realize (or quickly forgot) that Fontmetrics had a stringWidth method. I am always learning something. Of course, it seems strange or perhaps slightly redundant that a SwingUtilities method would do the same thing as one of its arguments. Perhaps the Fontmetrics class was later modified to include a method to compute string widths. It is time to investigate the differences.

    Edit: Well, SwingUitilities.computeStringWidth calls SwingUtilties2.stringWidth. I had to search online for the JavaDoc of the latter. Its return value is Fontmetrics.stringWidth(). Fontmetrics has been around since Java 1.0 so I am somewhat confused.

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

  10. #10
    alexix is offline Member
    Join Date
    Feb 2013
    Posts
    19
    Rep Power
    0

    Default Re: a Button that extends JButton

    Thank you guys. fontMetrics did the job.

Similar Threads

  1. Replies: 1
    Last Post: 01-15-2013, 06:44 PM
  2. Extends JLabel
    By Dipke in forum New To Java
    Replies: 2
    Last Post: 10-24-2012, 01:50 PM
  3. ActionListener for JButton after changing Button to JButton
    By ravi.joshi53 in forum Java Applets
    Replies: 2
    Last Post: 10-07-2011, 07:35 AM
  4. Initialize Vs extends
    By N00Bie in forum New To Java
    Replies: 1
    Last Post: 03-22-2011, 02:58 PM
  5. My icon extends my JButton
    By hitmen in forum AWT / Swing
    Replies: 5
    Last Post: 03-06-2009, 07:49 AM

Posting Permissions

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