displayMessage instead of showMessageDialog - java

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.

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.

Java - A modeless waiting dialog with dynamic text

I need to create a dialog (message) box with the following requirements:
It should be able to change it text during it visibility period by adding periodically 1 dot to the current text it displays.
It should be modeless to avoid blocking the work of the main process. It is only a notification message for the user that a work is being processed.
It should be displayed when the process starts and disappeared when it ends. I don't mind to call it explicitly becuase I don't know how much time the task will take in advance.
The dialog should be displayed several times, each time with a different base text.
I have tried to create a modeless dialog like this:
try{
String laf = UIManager.getSystemLookAndFeelClassName();
UIManager.setLookAndFeel(laf); // tell the UIManager to change the look and feel
}
catch (Exception e)
{
System.out.println("Unable to change look and feel");
}
m_optionPane = new JOptionPane(a_sText, JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION, null, new Object[]{}, null);
m_dialog = new JDialog();
m_dialog.setSize(200, 50);
m_dialog.setTitle(a_sTitle);
m_dialog.setModal(false);
m_dialog.setContentPane(m_optionPane);
m_dialog.setLocationRelativeTo(null);
m_dialog.setAlwaysOnTop(true);
m_dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
m_dialog.pack();
public void showDialog(String a_sText, boolean a_bDynamic){
if(a_bDynamic){
m_runnable = new DynamicWait(m_optionPane, a_sText);
m_thread = new Thread(m_runnable);
m_thread.start();
}else{
m_optionPane.setMessage(a_sText);
}
m_dialog.setVisible(true);
}
public void hideDialog2(){
if (m_thread != null){
m_thread.interrupt();
m_runnable = null;
m_thread = null;
}
m_dialog.dispose();
}
and then use a thread to constantly add the dots..
public class DynamicWait implements Runnable {
public DynamicWait(JOptionPane a_optionPane, String a_sText){
Log4jWrapper.writeLog(LogLevelEnum.WARN, "DynamicWait - " + a_sText + " Thread ID " + Thread.currentThread().getId());
m_optionPane = a_optionPane;
m_sText = a_sText;
}
#Override
public void run() {
while(m_bRun){
try {
m_optionPane.setMessage(m_sText);
Thread.sleep(1000);
m_optionPane.setMessage(m_sText + ".");
Thread.sleep(1000);
m_optionPane.setMessage(m_sText + "..");
Thread.sleep(1000);
m_optionPane.setMessage(m_sText + "...");
Thread.sleep(1000);
} catch (InterruptedException e) {
Log4jWrapper.writeLog(LogLevelEnum.WARN, e.getMessage());
}
}
}
public void stopRun(){
m_bRun = false;
}
Well, all in all, I couldn't get the proper results.
I'll appreciate your advise.
Thank you.

Icons on JOptionPane

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:

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

Substance and MacOS MenuBar

I have a Class MainWindow which extends JFrame.
In MainWindow i have a JMenuBar.
I want to show the MenuBar in OSX on top (next to the Apple Symbol). This only works, when i dont set a Substance Skin. Is it possible to use a Substance Skin and use The MacOS MenuBar?
My Code:
//Set Menu for MacOS
System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty("com.apple.mrj.application.apple.menu.about.name", name);
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
SubstanceSkin skin = new GraphiteGlassSkin();
SubstanceLookAndFeel.setSkin(skin); //WORKS WHEN I COMMENT THIS (WITHOUT SUBSTANCE SKIN)
JFrame.setDefaultLookAndFeelDecorated(false);
MainWindow mainWindow = new MainWindow(name);
mainWindow.setVisible(true);
}
});
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
You can specify the UI for menu bar alone like this:
try {
UIManager.setLookAndFeel(new SubstanceBusinessBlackSteelLookAndFeel());
} catch (UnsupportedLookAndFeelException ex) {
// log...
}
JMenuBar menubar = frame.getJMenuBar(); // assuming you've set the menu bar already
String os = System.getProperty("os.name");
if (os.equals("Mac OS X")) {
try {
System.setProperty("apple.laf.useScreenMenuBar", "true");
menubar.setUI((MenuBarUI) Class.forName("com.apple.laf.AquaMenuBarUI").newInstance());
} catch (Exception ex) {
// log...
}
}
Yes, as shown below.
$ java -Xdock:name=MyApp -Dswing.defaultlaf=com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel -jar MyApp.jar

Categories

Resources