Icons on JOptionPane - java

Sorry, this is really basic. My first experience with Java Swing using Eclipse. I'm trying to write a very simple JOptionPane. I want the question mark icon to appear, but all I am getting is the Java coffee cup icon. What am I doing wrong? Thanks!
Object[] options = {"Encrypt", "Decrypt"};
int n = JOptionPane.showOptionDialog(new JFrame(),
"What Do You Want to Do?",
"Crypto",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, //do not use a custom Icon
options, //the titles of buttons
options[0]); //default button title

Please have a look at How to Set the Look and Feel
Try with different themes
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
// Set Motif L&F on any platform
// UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
// Set cross-platform Java L&F (also called "Metal")
// UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
// Set System L&F
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
Object[] options = { "Encrypt", "Decrypt" };
JOptionPane.showOptionDialog(new JFrame(), "What Do You Want to Do?", "Crypto",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, // do not use a
// custom Icon
options, // the titles of buttons
options[0]); // default button title
}
});
With different themes:

Related

How can I set the backgorund color for a JFrame using a JComboBox from another JFrame?

Actually, I'm trying to do a "Setting" window for a game and I want to set the background color of another window. I have no idea what to do. Some ideas pls?
You can implement it in different places. One of the ways is constructor, for example:
public YourClassPanel() {
// to set Look&Feel
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (ClassNotFoundException ex) {
Logger.getLogger(ControlPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(ControlPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(ControlPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(ControlPanel.class.getName()).log(Level.SEVERE, null, ex);
}
SwingUtilities.updateComponentTreeUI(this);
this.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
ControlPanel.tgp = null;
}
});
this.setBounds(0, 0, 710, 580);
this.setTitle("Buffer Allocation Panel");
this.setPreferredSize(null);
this.setResizable(false);
this.setBackground(Color.yellow); //to set Background
this.setForeground(Color.magenta); // to set Foreground
this.setOpaque(a!=255); // to set Opaque
}
Set a different color for JPanel background from Properties.

displayMessage instead of showMessageDialog

I'm creating my first program in java (in Netbeans), it is basically a SystemTray Icon with a popup menu with different functions for every item. In the following Item I want to use "displayMessage" instead of "showMessageDialog" after that the image was saved. I've tried with trayIcon.displayMessage("Message", "message", TrayIcon.MessageType.INFO); but it doesn't work and I don't know why, do you have any idea?
item5.addActionListener(new ActionListener () {
#Override
public void actionPerformed(ActionEvent e) {
BufferedImage image = null;
try {
Thread.currentThread().sleep(500);
} catch (InterruptedException ex) {
Logger.getLogger(SupTray.class.getName()).log(Level.SEVERE, null, ex);
}
try {
image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
} catch (AWTException ex) {
Logger.getLogger(SupTray.class.getName()).log(Level.SEVERE, null, ex);
}
try {
SimpleDateFormat formatter = new SimpleDateFormat("ddMMyyyy_HH.mm.ss");
Calendar now = Calendar.getInstance();
ImageIO.write(image, "png", new File(System.getProperty("user.home")+"\\Desktop\\"+formatter.format(now.getTime())+".png"));
} catch (IOException ex) {
Logger.getLogger(SupTray.class.getName()).log(Level.SEVERE, null, ex);
}
JOptionPane.showMessageDialog(null, "L'immagine รจ stata salvata nel desktop.", "Cattura schermata", JOptionPane.INFORMATION_MESSAGE);
}
});
Im not sure if it makes any difference in this case, but in general you should specify the component the dialog should be displayed on (null in JOptionPane.showMessageDialog).
I had some unnecessary issues not doing this some time ago.

JOptionPane button size (Nimbus LAF)

Recently I've been working on a Swing project with Nimbus Look and Feel. I want to set all buttons in a JOptionPane to have the same size, but in vain.
import javax.swing.*;
public class NimbusTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
try {
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if (("Nimbus").equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
UIManager.put("OptionPane.sameSizeButtons", true);
String[] options = new String[]{"--------------------","short","1"};
int option = JOptionPane.showOptionDialog(null, "Nimbus problem", "JOptionPane", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
}
});
}
}
What I want is a JOptionPane with three buttons having the same size. However, I got the following result:
My code UIManager.put("OptionPane.sameSizeButtons", true); seems to be ignored. What should I do to create a JOptionPane with same size buttons if I don't want to recreate a JOptionPane-like dialog?
Replace
UIManager.put("OptionPane.sameSizeButtons", true);
with
UIManager.getLookAndFeelDefaults().put("OptionPane.sameSizeButtons", true);
And it works like a charm

Custom created button on JOptionPane does not work?

I have added my own JPanel and JButton to JOptionPane as below.
When I click on the "OK" Button nothing shows up. Is there any alternative to it? i want to just get user's Username and Password but with my button not default from JOptionpane.
Can any body see what is wrong with this code?
final WebTextField user = new WebTextField();
final WebPasswordField password = new WebPasswordField();
WebButton ok = new WebButton("OK");
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
System.out.println("Zip file exist, continuing with extraction of zip file");
}
});
}
});
WebButton cancel = new WebButton("Cancel");
WebPanel panel = new WebPanel(new GridLayout(2, 2));
panel.setOpaque(false);
panel.add(new WebLabel("User:"));
panel.add(user);
panel.add(new WebLabel("Password:"));
panel.add(password);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (InstantiationException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IllegalAccessException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
UIManager.put("OptionPane.background", Color.WHITE);
UIManager.put("Panel.background", Color.WHITE);
int o =JOptionPane.showOptionDialog(bcfiDownloadPanel,
new Object[]{panel},
"Authorization Required",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,
new WebButton[]{new WebButton("OK"), new WebButton("Cancel")}, // this is the array
"default"
);
It's quite unusual JOptionPane though... I do hope that WebButton is something that extends JButton;
int o =JOptionPane.showOptionDialog(bcfiDownloadPanel,
new Object[]{panel},
"Authorization Required",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,
new WebButton[]{new WebButton("OK"), new WebButton("Cancel")}, // this is the array
"default"
... so, as for any JButton, you should add action listener to it to make it listent to click event etc;
modify your code in something like this way:
int o =JOptionPane.showOptionDialog(bcfiDownloadPanel,
new Object[]{panel},
"Authorization Required",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,
new WebButton[]{this.ok, this.cancel}, // this is the array
"default"
Report that helps
Good luck

How to make a window look like this in Java?

How do I create a window which looks like this in Java:
I want that window layout, instead of the standard Windows-borders, and I don't know how this is called.
Edit: look and feel doesn't work for me:
If you want your Look and Feel to draw the window decoration (that's what the "border" is called), then you need to call JFrame.setDefaultLookAndFeelDecorated(true) before creating your JFrame objects and JDialog.setDefaultLookAndFeelDecorated(true) before creating your JDialog objects.
that's called look and feel, you can find a detailed explanation here http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
You will need to first set the look and feel to use the cross platform look and feel (As someone commented before it's called metal). Then before you create the Frame you need to request that the borders are drawn by the look and feel.
try
{
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());
}
catch (Exception e) { }
This will set the look and feel to the one you want. As the cross platform look and feel is metal in Sun's JRE.
// Get window decorations drawn by the look and feel.
JFrame.setDefaultLookAndFeelDecorated(true);
// Create the JFrame.
JFrame frame = new JFrame("A window");
And this will make the created JFrame have borders like you describe.
Add this to your main() method:
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(testjframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(testjframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(testjframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(testjframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
To set windows look and feel for swing write following code in main method.
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("windows".equalsIgnoreCase(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception ex) {
System.out.println(e);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
MainFrame mainFrame = new MainFrame();
mainFrame.setExtendedState(MAXIMIZED_BOTH);
mainFrame.setVisible(true);
InitDatabaseDialog initDatabaseDialog = new InitDatabaseDialog(mainFrame, true);
initDatabaseDialog.setVisible(true);
}
});
}

Categories

Resources