Problem with JDialog box
Hi Guys,
I have a problem with the JDialog box. The close button (Cross mark of the box) is not displayed when I was trying to use my custom LookAndFeel and Custom theme. I am pasting the code sample here.
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import com.jgoodies.looks.plastic.Plastic3DLookAndFeel;
import com.jgoodies.looks.plastic.PlasticTheme;
import com.medplexus.looks.plastic.theme.SkyGreen;
public class TestTheDialog implements ActionListener {
JFrame mainFrame = null;
JButton myButton = null;
public TestTheDialog() {
mainFrame = new JFrame("TestTheDialog Tester");
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
try{
MyLookAndFeel.setCurrentTheme(new CustomLaF());
UIManager.setLookAndFeel(new MyLookAndFeel());
SwingUtilities.updateComponentTreeUI(mainFrame);
CustomDialog cd = new CustomDialog();
cd.setDefaultLookAndFeelDecorated(true);
}
catch(Exception e){
}
myButton = new JButton("Test the dialog!");
myButton.addActionListener(this);
mainFrame.setLocationRelativeTo(null);
mainFrame.getContentPane().add(myButton);
mainFrame.setSize(200, 150);
//mainFrame.pack();
mainFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(myButton == e.getSource()) {
System.err.println("Opening dialog.");
CustomDialog myDialog = new CustomDialog(mainFrame, true, "Do you like Java?");
System.err.println("After opening dialog.");
if(myDialog.getAnswer()) {
System.err.println("The answer stored in CustomDialog is 'true' (i.e. user clicked yes button.)");
}
else {
System.err.println("The answer stored in CustomDialog is 'false' (i.e. user clicked no button.)");
}
}
}
static class CustomLaF extends PlasticTheme {
protected ColorUIResource getPrimary1() {
return new ColorUIResource(255,128,0);
}
public ColorUIResource getPrimary2() {
return (new ColorUIResource(Color.white));
}
public ColorUIResource getPrimary3() {
return (new ColorUIResource(255,128,0));
}
public ColorUIResource getPrimaryControl() {
return new ColorUIResource(Color.GREEN);
}
protected ColorUIResource getSecondary1() {
return new ColorUIResource(Color.CYAN);
}
protected ColorUIResource getSecondary2() {
return (new ColorUIResource(Color.gray));
}
protected ColorUIResource getSecondary3() {
return (new ColorUIResource(235,235,235));
}
protected ColorUIResource getBlack() {
return BLACK;
}
protected ColorUIResource getWhite() {
return WHITE;
}
private Object getIconResource(String s) {
return LookAndFeel.makeIcon(getClass(), s);
}
private Icon getHastenedIcon(String s, UIDefaults uidefaults) {
Object obj = getIconResource(s);
return (Icon) ((javax.swing.UIDefaults.LazyValue) obj).createValue(uidefaults);
}
}
static class MyLookAndFeel extends Plastic3DLookAndFeel {
protected void initClassDefaults(UIDefaults table) {
super.initClassDefaults(table);
}
protected void initComponentDefaults(UIDefaults table) {
super.initComponentDefaults(table);
Object[] defaults = {
"MenuItem.foreground",new ColorUIResource(Color.white),
"MenuItem.background",new ColorUIResource(Color.gray),
"MenuItem.selectionForeground",new ColorUIResource(Color.gray),
"MenuItem.selectionBackground",new ColorUIResource(Color.white),
"Menu.selectionForeground", new ColorUIResource(Color.white),
"Menu.selectionBackground", new ColorUIResource(Color.gray),
"MenuBar.background", new ColorUIResource(235,235,235),
"Menu.background", new ColorUIResource(235,235,235),
"Desktop.background",new ColorUIResource(235,235,235),
"Button.select",new ColorUIResource(255,128,0),
"Button.focus",new ColorUIResource(255,128,0),
"TableHeader.background", new ColorUIResource(255,128,0),
"TableHeader.foreground", new ColorUIResource(Color.white),
"ScrollBar.background", new ColorUIResource(235,235,235),
"OptionPane.questionDialog.border.background", new ColorUIResource(Color.gray),
"OptionPane.errorDialog.titlePane.foreground", new ColorUIResource(Color.white),
"OptionPane.questionDialog.titlePane.background", new ColorUIResource(255,128,0),
"InternalFrame.borderColor", new ColorUIResource(Color.gray),
"InternalFrame.activeTitleForeground", new ColorUIResource(Color.white),
"InternalFrame.activeTitleBackground", new ColorUIResource(Color.gray),
"InternalFrame.borderColor", new ColorUIResource(Color.white),
"Table.selectionBackground",new ColorUIResource(255,128,0)
};
table.putDefaults(defaults);
}
}
public static void main(String argv[]) {
TestTheDialog tester = new TestTheDialog();
}
}
--
package CustomThemes;
import javax.swing.JDialog;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
public class CustomDialog extends JDialog implements ActionListener {
private JPanel myPanel = null;
private JButton yesButton = null;
private JButton noButton = null;
private boolean answer = false;
public boolean getAnswer() { return answer; }
public CustomDialog(){
}
public CustomDialog(JFrame frame, boolean modal, String myMessage) {
super(frame, modal);
setTitle("Guess?");
myPanel = new JPanel();
getContentPane().add(myPanel);
myPanel.add(new JLabel(myMessage));
yesButton = new JButton("Yes");
yesButton.addActionListener(this);
myPanel.add(yesButton);
noButton = new JButton("No");
noButton.addActionListener(this);
myPanel.add(noButton);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(frame);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(yesButton == e.getSource()) {
System.err.println("User chose yes.");
answer = true;
setVisible(false);
}
else if(noButton == e.getSource()) {
System.err.println("User chose no.");
answer = false;
setVisible(false);
}
}
}
Thanks and Regards
Kumar.

