Display issue with custom shaped Buttons !!

Hello all,

While implementing the custom shaped buttons in swing, i met with some display issues for the created buttons. Please help me out.

Please find the code which creates graph of bubble shaped buttons in the JScrollPane.

/* Main class */

import javax.swing.*;

public class VersionGraphDemo extends JFrame {

private JScrollPane _GrapbScrollPane = null;

public VersionGraphDemo (){

this.setSize(500, 400);

this.setTitle("VersionGraphDemo");

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

_GrapbScrollPane = getGraphScrollPane();

this.add(_GrapbScrollPane);// LINE 1

//this.add(new GraphicsDemo (6)); // LINE 2

setVisible(true);

}

private JScrollPane getGraphScrollPane() {

if (_GrapbScrollPane == null){

_GrapbScrollPane = new JScrollPane ();

_GrapbScrollPane.setViewportView(new GraphicsDemo (6));

}

return _GrapbScrollPane;

}

public static void main (String args []){

new VersionGraphDemo();

}

}

/* Graphics class */

import java.awt.*;

import java.awt.geom.*;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

public class GraphicsDemo extends JPanel {

final static Color gray = Color.LIGHT_GRAY;

private int numVersion = 0;

private JScrollPane jpane = null;

public GraphicsDemo(int num){

this.numVersion = num;

}

public void paint(Graphics g){

Graphics2D D2 = (Graphics2D) g;

this.createShape(D2);

}

public void createShape (Graphics2D D2){

int x = 10, y = 10;

for (int num =0; num < numVersion; num ++){

D2.setColor(gray);

if (num > 0){

D2.draw ( new Line2D.Double( (50/2)+ x, y+(num*40/2),

(50/2)+x, y+(num*2*40) ));

}

this.add(new MyButton ( x, y + (num*2*40), num ));

}

}

}

/* Custom button class */

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.BorderFactory;

import javax.swing.JButton;

public class MyButton extends JButton implements ActionListener {

final static Color gray = Color.LIGHT_GRAY;

final static Color black = Color.BLACK;

private int xPosition = 0;

private int yPosition = 0;

private int versionNum = 0;

public MyButton ( int xButton, int yButton, int versionNum ){

this.xPosition = xButton;

this.yPosition = yButton;

this.versionNum = versionNum;

this.setBorder(BorderFactory.createEmptyBorder());

this.addButtonSetting(xPosition, yPosition);

}

public void paint( Graphics g ){

Graphics2D gr = (Graphics2D) g;

gr.setColor(gray);

gr.fillOval(0,0 , 50, 40);

gr.setColor(black);

gr.drawString("V"+(versionNum+1), 19, 25);

}

public void addButtonSetting ( int xPosition, int yPosition) {

this.setBounds(xPosition, yPosition, 50, 40);

this.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {

if (e.getSource() == this ){

}

}

}

I am facing three issues,

1) As soon as i run the code, i am not able to see the custom buttons (bubbles) displayed in the window. It will get displayed only when i move the mouse over the position of custom buttons.

Any idea?

2) Even if i implement JScrollPane, i am not able to scroll the window. If i implement it for a table in the same way i will get the scroll bar.

Any idea?

3) Once the custom buttons are displayed (i have customized a squire button into an oval shaped button), i can see the shade of the squire button behind my oval button. this happens only when the JPanel from GraphicsDemo is added to a JScrollPane or a JSplitPane.

I can see the oval's perfectly if i add the JPanel to a JFrame.

The problem here is, i have to add the graph in a JSplitPane with the scrollable feature.

As of now it seems like an oval shape residing over a squire button.

Any idea ?

Please let me know if i have to give any more informations. ( I am not able to attach screen shot here..)

Thanks

[4336 byte] By [sweatingbulletsa] at [2007-11-27 8:52:07]
# 1

For topic 1):

Don't override the paint method, override the paintComponent method in your Graphics Demo class and MyButton class add an updateUI() or repaint() call at the end of your paintComponent() method. (Needed only in GraphicsDemo, MyButton is called directly)

For topic 2):

You'll have to calculate the size of your panel and set via setPreferredSize().

marco@dea at 2007-7-12 21:06:58 > top of Java-index,Desktop,Core GUI APIs...
# 2

Don't forget to use the "Code Formatting Tags",

see http://forum.java.sun.com/help.jspa?sec=formatting,

so the posted code retains its original formatting.

> add an updateUI() or repaint() call at the end of your paintComponent() method

No, that will result in an infinite loop.

> 1) As soon as i run the code, i am not able to see the custom buttons

Your painting logic is completly wrong. Swing determines when a compnent needs to be repainted and invokes the paint() method which in turn invokes the paintComponent() method. There these methods can be invoked many time while the GUI is active. Therefore you don't add components to the GUI inside of this method. Components are added to the panel when you create the panel. There is no need to override either of these methods to add a component to the panel.

camickra at 2007-7-12 21:06:59 > top of Java-index,Desktop,Core GUI APIs...
# 3
Hello Marco,can you please elaborate the second answer. Do you mean, if i set a preferred size for the panel and add it to the scroll pane it will work?Thanks for your reply
sweatingbulletsa at 2007-7-12 21:06:59 > top of Java-index,Desktop,Core GUI APIs...
# 4
Your code is all messed up as I explained. So fix that first and then see what happens.
camickra at 2007-7-12 21:06:59 > top of Java-index,Desktop,Core GUI APIs...
# 5

> Hello Marco,

> can you please elaborate the second answer.

> Do you mean, if i set a preferred size for the panel

> and add it to the scroll pane it will work?

Some friendly advice: Please do yourself a favor and ignore Marco's advice. Concentrate instead on what Camickr had to say.

petes1234a at 2007-7-12 21:06:59 > top of Java-index,Desktop,Core GUI APIs...
# 6

Hello Camickr,

Let me explain you the issue once again. In my code i am trying to create "custom buttons" in round shape, that is why i tried to override the paint function.

When i add the panel which is having the custom buttons to a JFrame (comment the line mentioned as "LINE 1" instead of "LINE 2"), i can see perfect round buttons though i am facing issue 1 and issue 2.

When i add the same panel to a scroll pane or a tabbed pane (comment the line mentioned as "LINE 2" instead of "LINE 1"), i am facing all the three issues.

If possible please tell me what change i need to make to solve this. A code snippet will be highly appreciated.

Thanks.

sweatingbulletsa at 2007-7-12 21:06:59 > top of Java-index,Desktop,Core GUI APIs...
# 7

First of all sorry for the crappy tip with the infinite loop.

Second i hope my next tip is better ...

If I would want to create custom buttons I would first check if i can't get the buttons i want with swing methods, maybe by placing an icon or altering borders.

If I'm absolutely sure i want to custimize by painting I would override the paintComponent(Graphics g) method and at least the getPreferredSize() method. If I don't implement the get*Size() methods they won't return proper results which fit the custom painting. At the beginning of the paintComponent() method I would have to take care of clearing the current graphics if requested by the opaque property. I would do that via

if (isOpaque())

{

g.setColor(getBackground());

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

}

then i would do the custom painting for this component.

Just setting the preferred size of the panel would also result in getting the sliders but i doubt they will be very useful without the accurate size of the underlying components.

And keep in mind was camickr said about adding other components in the paintComponent method. So don't add your buttons to the GraphicsDemo panel within the paintComponent method.

Read

http://java.sun.com/docs/books/tutorial/uiswing/painting/practice.html

to get a basic overview over the customized painting.

Message was edited by:

marco@de

marco@dea at 2007-7-12 21:06:59 > top of Java-index,Desktop,Core GUI APIs...
# 8

> i am trying to create "custom buttons" in round shape, that is why i tried to override the paint function.

As you have been told, you override the paintComponent() method, not the paint() method. Any you only override this method for your "MyButton" class. You don't override the paint() or paintComponent() method of the panel you add the buttons to. I gave you the reason for this.

Read the tip on "Creating Round Swing Buttons":

http://java.sun.com/developer/TechTips/txtarchive/

camickra at 2007-7-12 21:06:59 > top of Java-index,Desktop,Core GUI APIs...
# 9
Hello All,All of my issues got fixed. And the graph is working perfectly fine. Marco, all your tips worked perfectly.camickr, your link is also very good, though i got the issue fixed by that time.thanks once again,Prajeen
sweatingbulletsa at 2007-7-12 21:06:59 > top of Java-index,Desktop,Core GUI APIs...