When I'm using JFileChooser application in my program on Windows 7 it display such window:
But when I run the JWS File Chooser Demo it displays much better window:
Why?
Because the demo doesn't use JFileChooser; it uses javax.jnlp.FileOpenService, which uses the native OS's file dialog. The source code for that demo is here, check it out.
The Oracle Java Web Start app, is actually using the JNLP API instead of Swing's JFileChooser.
Here is a link: http://download.oracle.com/javase/tutorial/deployment/doingMoreWithRIA/jnlpAPI.html
The major difference could be solved by using the native look and feel. See the main() of FileBro for how to do that.
use this code
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (ClassNotFoundException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (InstantiationException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (IllegalAccessException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (UnsupportedLookAndFeelException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
Related
Hi guys I open in my java application I open a browser page with this code:
String URL = "https://www.google.com/";
try {
java.awt.Desktop.getDesktop().browse(java.net.URI.create(URL));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Now I have to turn off the browser, Is there a code to do that?
No, there is no Java API to close the browser.
There sort-of is but it's not very reliable. But if you have to, you can try with keyboard shortcuts and mouse clicks using Java Robot API.
public class RobotTest {
public static void main(String[] args) {
String URL = "https://www.google.com/";
try {
java.awt.Desktop.getDesktop().browse(java.net.URI.create(URL));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Thread.sleep(3*1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Robot javaRobot;
try {
javaRobot = new Robot();
javaRobot.keyPress(KeyEvent.VK_ALT);
javaRobot.keyPress(KeyEvent.VK_F4);
javaRobot.keyRelease(KeyEvent.VK_ALT);
javaRobot.keyRelease(KeyEvent.VK_F4);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I have a requirement where i need to mock the com.sun.deploy.security.DeployManifestChecker and return null when accessing printWarningsIfRequired in that class. Since Deploy.jar is not in my build path, i couldnt directly mock it up. I'm looking at a way to implement it using Java reflection API. but i'm not sure how to invoke the mock method with the Class argument.
method.invoke(null, new Class[]{claz1}); is failing with NP exception.
Here is the code
Mockery context ;
final Class<?> claz1;
try {
Class mclaz = Class.forName("org.jmock.Mockery");
context = (Mockery) mclaz.newInstance();
claz1 = Class.forName("com.sun.deploy.security.DeployManifestChecker");
final Method method = mclaz.getDeclaredMethod("mock",
new Class[]{Class.class} );
method.invoke(null, new Class[]{claz1});
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Your stated goal in to use reflection but if I had to do this i'd just create a test-only jar that includes an interface that mimics the com.sun.deploy.security.DeployManifestChecker class. Then use jmock to mock the stand-in.
I've taken this approach before when I couldn't include the actual class in my testing setup like you and it works like a champ. Just be sure that your stand-in doesn't make it to the runtime classpath and remains in test scope only.
The default exception handling code generated by Eclipse looks as follows:
try {
methodThrowsACheckedException();
} catch (SomeCheckedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Would it not be better if Eclipse generated the following code instead?
try {
methodThrowsACheckedException();
} catch (SomeCheckedException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
You can configure eclipse to do that its your choice. Check Code Template-> Catch Block Body in Preferences->Java->Code style
I am trying to clear the app cache of other android apps besides my own. To do this, I am using reflection on the PackageManager class. However, whenever I initialize the method before I invoke it, it always ends up being null.
private void initiateClearUserData() {
// Invoke uninstall or clear user data based on sysPackage
String thePackageName;
PackageManager pm = speedy.this.getPackageManager();
List<ApplicationInfo> installedApps = pm.getInstalledApplications(0);
ApplicationInfo ai;// = installedApps.get(0);
ActivityManager.RunningAppProcessInfo process;
for(int x=0; x<4; x++){
ai = installedApps.get(x);
Here is where my problem is:
thePackageName = ai.packageName.toString();// mAppEntry.info.packageName;
Method deleteApplicationCacheFiles = null;
mClearCacheObserver = new ClearCacheObserver();
try {
deleteApplicationCacheFiles = pm.getClass().getMethod(
"deleteApplicationCacheFiles", String.class, PackageManager.class);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(deleteApplicationCacheFiles!= null){
try {
deleteApplicationCacheFiles.invoke(thePackageName, mClearCacheObserver);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
Toast.makeText(speedy.this, "Hell naw",
Toast.LENGTH_SHORT).show();
}
}
}
Because Method deleteApplicationCacheFiles is null, my toast message shows up. Any suggestions?
Take a look at the docs for Security on Android: http://developer.android.com/guide/topics/security/security.html
A central design point of the Android security architecture is that no application, by default, has permission to perform any operations that would adversely impact other applications, the operating system, or the user. This includes reading or writing the user's private data (such as contacts or e-mails), reading or writing another application's files, performing network access, keeping the device awake, etc.
It sounds like the system will block you from doing this (through reflection too).
I am new to J2ME. I want to play an audio song in my application. I have written
Player p = null;
try {
p = Manager.createPlayer(getClass().getResourceAsStream("aa.wav"),"audio/x-wav");
p.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
where "aa.wav" is a wav format song placed in resource folder. when i debug this code
getClass().getResourceAsStream("aa.wav")
it returns null.
Can you please Help me thanks
if resource folder is under src then.
make it
getClass().getResourceAsStream("/resource/aa.wav")