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

