JOptionPane: Installing Custom icon has no effect when look and feel set - java

I would like to install a custom icon in place of the standard JOptionPane information icon..
I tried
ImageIcon myCustomIcon = ...;
UIManager.put("OptionPane.informationIcon", myCustomIcon);
However, apparently this has no effect due to the following line:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Commenting out this line gives the correct behaviour. I have of course tried putting the icon into the UIManager both before/after the look and feel is being set.
Can I somehow combine the two to override the information icon globally?
I am working on Ubuntu 10.04 right now..
Regards,
Morten

Works fine for me using the Metal and Windows LAF's.
Maybe your LAF doesn't support the UIManager property. Check out UIManager Defaults for a list of properties.
If you need more help the post your SSCCE that demonstrates the problem.

Have you tried to specify your icon after setting look&feel, in such a way:
JOptionPane.showMessageDialog(frame,
"Eggs are not supposed to be green.",
"Inane custom dialog",
JOptionPane.INFORMATION_MESSAGE,
myCustomIcon);
Update:
The following code works fine on my Windows 7:
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class OptionPaneIcon {
public static void main (String[] args) {
ImageIcon myCustomIcon = loadImageIcon("image.png");
UIManager.put("OptionPane.informationIcon", myCustomIcon);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JOptionPane.showMessageDialog(null, "Hello!");
}
/** Returns an ImageIcon, or null if the path was invalid. */
private static ImageIcon loadImageIcon(String path) {
URL imgURL = OptionPaneIcon.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
}
P.S. Sorry for my impatience.

Related

Disabling a menu item in Eclipse during startup

I am developing an Eclipse plugin. I need to check if the nvcc is present at start-up, and if not, then disable a particular menu item. Here is my code till now:
package Startup;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.eclipse.ui.IStartup;
public class StartCheck implements IStartup {
public void earlyStartup() {
// This method checks for presence of nvcc when Eclipse starts-up.
String command="nvcc -version";
Runtime run = Runtime.getRuntime();
Process pr;
try {
pr = run.exec(command);
pr.waitFor();
BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
//print-out the nvcc version
System.out.println(buf.readLine());
}catch (IOException e) {
//disable all buttons since no further task can be done, prompt user to install nvcc.
System.out.println("nvcc not present, freezing the energy estimation plugin...");
disableMenu();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void disableMenu(){
}
}
As you can see, the disable menu method does not contain anything. Can someone please help me with this? I have to basically disable a particular menu item if an IOException occurs in the above code.
Thanks!

Multi-Catches Problems

I copied something from a different class, and yet it gave me an error even though it didn't in the original. Here's the code:
package com.dinobuilding;
import java.awt.EventQueue;
import java.awt.FontFormatException;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.ImageObserver;
import java.io.IOException;
import javax.swing.*;
public class Window {
public static int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
public static int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
public static ImageIcon background = new ImageIcon("resources/images/Background.png");
public static ImageObserver observer;
public Window() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("DinoBuilding");
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.pack();
frame.setSize(screenWidth, screenHeight);
frame.setVisible(true);
}
});
}
public static void main(String[] args) {
new Window();
}
}
I've probably messed something up really trivial, but it will not stop telling me "Multi-catches are not supported at this language level". If you need more information that I probably forgot to mention, just ask. Thanks in advance!
Multi catch statements are only supported in Java 7 and higher versions, make sure that you are not running a version below that.
Check this out to see how you can change the Project language level in IDEA. I am assuming that you have Java 7 or a version higher installed on your system, if you don't then go and get the latest version of Java
Multi-catches like catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) are only supported from Java 7 and onwards. I believe your JDK/Java compliter is earlier than JDK1.7.
http://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html
Please check that. If possible, upgrade to JDK 1.7. Else use the catch block as following...
try {
// Do something
} catch (ClassNotFoundException ex) {
// Do something
} catch(InstantiationException ex) {
// Do something
} catch(IllegalAccessException ex) {
// Do something
} catch(UnsupportedLookAndFeelException ex) {
// Do something
}
Cheers!

Type full width colon (:) to setProperty in Java

I'm trying to set my database property, so in the future users could change it if they want to change the route of the database. The problem is when I try to set something with full width colon, which always add a backslash escape character .
I've tried normal and double escape, but it doesn't work.
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
public class SetProps {
public static void SetDefaultProps(){
Properties prop = new Properties();
OutputStream output = null;
try {
output = new FileOutputStream("./build/classes/configuracion.properties");
// Set the database property
prop.setProperty("url", "jdbc:mysql://192.168.1.192:3306/ordenestaller");
// Save Properties
prop.store(output, null);
} catch (IOException io) {
io.printStackTrace();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
SetDefaultProps();
}
}
Hardcoding the db url in the java code is not a good coding practice.
I would recommend you to put those urls in a properties files and read them as and when required using a ResouceBundle.
you can have "information.properties" file somewhere in your classpath and the contents of that file can be
dbURL:jdbc:mysql://192.168.1.192:3306/ordenestaller
and now in your code you can get this using a ResourceBundle
ResourceBundle bundle = ResourceBundle.getBundle("information");
String dbURL= bundle.getString("dbURL");
This will have an added advantage that you will not need to recompile your java class again if DB URl is changed.
Hope it helps

How to Change a .PNG file name through Java/Eclipse

I was wondering if anyone can help me solve this. I want to change/rename the name of a file .png as soon as I create it using java Eclipse through Linux. So when the file is saved into a folder I want the user to be able to change the name of it to whatever the user wants. How do I do that?
Here is my code...
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class adbPicture implements ActionListener {
#Override
public void actionPerformed(ActionEvent arg0) {
// exec.(points at whatever .sh file you want to run in the folder)
try {
Process proc = Runtime.getRuntime().exec("sh /home/local/ANT/arthm/Desktop/stuff/pics1.sh);
BufferedReader read = new BufferedReader(new InputStreamReader( proc.getInputStream()));
try {
proc.waitFor();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
while (read.ready()) {
System.out.println(read.readLine());
}
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Error 5! You Messed Up!");
}
int picreply = JOptionPane.showConfirmDialog(null, "Would you like to change the name?", null, JOptionPane.YES_NO_OPTION);
if(picreply == JOptionPane.YES_OPTION){
String namestuff = JOptionPane.showInputDialog(null, "Please Input Name");
// Here in this area is where I want to add the code to change the name of the file...
}
else {
System.exit(0); // also this makes me exit the whole program, how do I only exit the //showConfirmDialog box?
}
}
}
You can use File#renameTo
File original = new File("...");
original.renameTo(new File("..."));
Note. If you change the location that the file is stored in, this will act like a move
String fileRenamed = JOptionPane.showInputDialog(null, "Please Input New Name");
Process proc = Runtime.getRuntime().exec("mv "+ pathToFile+namestuff+" "+pathToFile+fileRenamed);

Java clipboard as stack in paste override

I have done my research, no useful results found.
Here is the deal, I'm writing a 'new' clipboard that works like a stack instead of a 'area'. And I'm brave or stupid enoght to do that in Java. So far in my tests to see if this is possible I have managed to create this stack behavior. The only problem I'm getting is that sometimes, mainly when I paste the top of the stack (pop operation), it doesn't pop or for some other reason it pastes twice.
Example:
If i copy this three words: Carlos, Lucas, Eastwood
The stack clipboard behaves like this at paste: Eastwood, Eastwood, Lucas, Carlos
I'm using JNativeHooks for reading the system keypresses and determining when the user is pasting.
I think what is happening is that the system is pasting before my code... Well, here is the code anyway (It is a test, that explains why it is badly commented):
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.Transferable;
import java.util.Stack;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.NativeInputEvent;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
public class Test3 implements NativeKeyListener {
Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
Stack<Transferable> clipStack = new Stack<>();
public static void main(String[] args) {
try {
GlobalScreen.registerNativeHook();
} catch (NativeHookException ex) {
System.err
.println("There was a problem registering the native hook.");
System.err.println(ex.getMessage());
System.exit(1);
}
Test2 t2 = new Test2();
// Construct the example object and initialze native hook.
GlobalScreen.getInstance().addNativeKeyListener(t2);
}
#Override
public void nativeKeyPressed(NativeKeyEvent ev) {
// Copy
if (ev.getKeyCode() == NativeKeyEvent.VK_C
&& NativeInputEvent.getModifiersText(ev.getModifiers()).equals(
"Ctrl")) {
// Clip the pop
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
clipStack.push(sysClip.getContents(null));
System.out.println("Ctrl+C : Stack(" + clipStack.size() + ")");
}
// Paste
if (ev.getKeyCode() == NativeKeyEvent.VK_V
&& NativeInputEvent.getModifiersText(ev.getModifiers()).equals(
"Ctrl")) {
// Clip the pop
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (clipStack.size() > 1) {
sysClip.setContents(clipStack.pop(), null);
} else
sysClip.setContents(clipStack.peek(), null);
System.out.println("Ctrl+V : Stack(" + clipStack.size() + ")");
}
}
#Override
public void nativeKeyReleased(NativeKeyEvent e) {
}
#Override
public void nativeKeyTyped(NativeKeyEvent e) {
}
}
I would think the same as you suggested.
Have a copy and paste key combination something different from ctrl+c and ctrl+v and you can even bypass using the system clipboard. Simply push the selected text and pop the text directly to your text pointer in your application provided of course that you are using this stack behavior for only a particular application.
The system retains it's own copy of latest clip and it gets a duplicate from your sysClip.setContents(clipStack.pop(), null) on paste operation.
I don't have much idea on disabling the system behavior. You can do research on that. But you can always make sure that it's the only problem by changing the key combination.

Categories