7 Replies - 18122 Views - Last Post: 29 December 2010 - 11:06 PM Rate Topic: -----

#1 Ritesht93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 30-August 10

Change the shape of JButton

Posted 26 December 2010 - 08:57 PM

hello evryone , can anybody tell me that , is there a way to change the default shape(i.e rectangle) of swing's JButton to some other shape preferably rounded rectangle or ellipse/oval ; i have heard of something called paintComponent method (overriding the jComponent method)which can be used to draw any shape and set any color using a Graphics object....but i cant get a proper simple example.......can anybody please give me simple example on how to change the the shape of a JButton , and it must change its color when it is clicked...THANKS IN ADVANCE :)

Is This A Good Question/Topic? 0
  • +

Replies To: Change the shape of JButton

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Change the shape of JButton

Posted 26 December 2010 - 09:50 PM

Not an easy task
Basically you can overload the paintComponent() method of any component as in the example below.

	class MyButton extends JButton implements ActionListener{
		
		MyButton(String text) {
			super(text);
			addActionListener(this);
		}
		
		public void actionPerformed(ActionEvent e) {
			// button as beeen clicked I change it's color
			setBackground(Color.RED);
		}
		public void paintComponent(Graphics g) {
			Rectangle r = getBounds();
			int x = r.x + 20;
			int y = r.y + 20;
			int width = r.width - 40;
			int height = r.height- 40;
			g.setColor(Color.BLACK);
			g.fillOval(x, y, width, height);
			x += 2;
			y += 2;
			width -= 4;
			height -= 4;
			g.setColor(getBackground());
			g.fillOval(x, y, width, height);
			g.setColor(getForeground());
			y += (height / 2) - 10;
			g.drawString(getText(), x, y);
		}
	}



For JButton it is more complicated. You will have to test if the button as focus or not.
Centering the text (and shrinking it if it does not fit) won't be an easy task
But you have something to start with.
Was This Post Helpful? 3
  • +
  • -

#3 Paars  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 39
  • Joined: 11-October 10

Re: Change the shape of JButton

Posted 29 December 2010 - 04:07 AM

Wouldn't it be easier if you use a JPanel, add a mouseListener to it(Make it function as a button). Then change the shape for that?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Change the shape of JButton

Posted 29 December 2010 - 12:18 PM

View PostPaars, on 29 December 2010 - 05:07 AM, said:

Wouldn't it be easier if you use a JPanel, add a mouseListener to it(Make it function as a button). Then change the shape for that?

JPanel or even easier JLabel, the problem stays the same. You will have to implement yourself JButton behaviour.
Was This Post Helpful? 1
  • +
  • -

#5 jeremejazz  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 48
  • Joined: 23-April 10

Re: Change the shape of JButton

Posted 29 December 2010 - 09:39 PM

View Postpbl, on 29 December 2010 - 11:18 AM, said:

View PostPaars, on 29 December 2010 - 05:07 AM, said:

Wouldn't it be easier if you use a JPanel, add a mouseListener to it(Make it function as a button). Then change the shape for that?

JPanel or even easier JLabel, the problem stays the same. You will have to implement yourself JButton behaviour.


Yeah, then you can draw your button using an image manipulation program (like Photoshop) then use it as an image for JLabel :bananaman:
Was This Post Helpful? 3
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Change the shape of JButton

Posted 29 December 2010 - 10:13 PM

View Postjeremejazz, on 29 December 2010 - 10:39 PM, said:

Yeah, then you can draw your button using an image manipulation program (like Photoshop) then use it as an image for JLabel :bananaman:

Good show :^: +1 for that one
This is true
Make your own class MyButton that extends JButton
Overload paintBorder() method to do nothing
overload paintComponent() to only draw the Image
Was This Post Helpful? 2
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Change the shape of JButton

Posted 29 December 2010 - 10:23 PM

View Postpbl, on 29 December 2010 - 11:13 PM, said:

View Postjeremejazz, on 29 December 2010 - 10:39 PM, said:

Yeah, then you can draw your button using an image manipulation program (like Photoshop) then use it as an image for JLabel :bananaman:

Good show :^: +1 for that one
This is true
Make your own class MyButton that extends JButton
Overload paintBorder() method to do nothing
overload paintComponent() to only draw the Image


In my DreamInCodeClient program, I never used a single JButton. I needed a button-like interface that allowed for very precise painting of text - as I did not just want it layed out anywhere. So, I emulated mouseOver events, and the background and border colors change, just like a standard JButton would.

/*
Copyright (C) 2010  Stephen Schwahn

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>
 */
package dicapi.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JComponent;

import dicapi.data.Category;

public class CategoryComponent extends JComponent {

    private String[] text = new String[2];
    private BufferedImage img;
    private Color componentColor = new Color(232, 143, 23);

    public CategoryComponent(final Category c, final ForumFrame forumFrame) {

        addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                componentColor = new Color(255, 143, 23);
                repaint();
                forumFrame.showCategory(c);
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                componentColor = componentColor.brighter();
                repaint();
            }

            @Override
            public void mouseExited(MouseEvent e) {
                componentColor = new Color(232, 143, 23);
                repaint();
            }
        });
        setMinimumSize(new Dimension(500, 100));
        setPreferredSize(new Dimension(500, 100));

        setBorder(BorderFactory.createLineBorder(componentColor.darker(), 2));

        text[0] = c.getName();
        text[1] = c.getForums().size() + "";

        try {
            img = ImageIO.read(getClass().getResourceAsStream("/images/forumIcon.gif"));
        } catch (IOException e) {
            System.err.println("Could not find forum icon.");
        }
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setColor(componentColor);
        g.fillRect(0, 0, getWidth(), getHeight());

        g.drawImage(img, 5, 5, null);

        g.setColor(Color.black);
        Font small = new Font("Arial", Font.PLAIN, 12);
        Font large = new Font("Arial", Font.BOLD, 24);

        g.setFont(large);
        g.drawString(text[0], 80, 29);

        g.setFont(small);
        g.drawString("Forums: " + text[1], 80, 50);
    }
}




I really need to update these files... :/ ... and add comments...but it's an example anyway.
Was This Post Helpful? 1
  • +
  • -

#8 jeremejazz  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 48
  • Joined: 23-April 10

Re: Change the shape of JButton

Posted 29 December 2010 - 11:06 PM

*snip*

cool.. you made your custom component by overriding the methods

Edited by Dogstopper: Long quotes are annoying ;)

This post has been edited by Dogstopper: 29 December 2010 - 11:14 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1