Create shortcut with minimized window - java

Good morning, I have a Java code using JShortcut, the problem is I can not create the shortcut with the minimized window, try looking for properties as windowstyle, but find nothing.
Code.
public void createDesktopShortcut() {
try {
link.setFolder(JShellLink.getDirectory("desktop"));
link.setName("ie");
link.setPath(filePath);
//windowstyle = 7
link.save();
} catch (Exception ex) {
ex.printStackTrace();
}
}
How can I add this option?

Related

How to implement your own efficient OS listener?

How to create your own listener that listens to the OS efficiently in Java? For example like the ActionListener in Swing, that reacts to button clicks.
I want to make a program that logs the users clipboard, but I don't want to use a loop like this:
while (flag)
{
Transferable t = Toolkit.getDefaultToolkit()
.getSystemClipboard()
.getContents(null);
if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor))
{
try
{
String text = (String) t
.getTransferData(DataFlavor.stringFlavor);
System.out.println(text);
}
catch (UnsupportedFlavorException e1)
{
e1.printStackTrace();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
Thread.sleep(1000);
}
I want the processor only to work when the clipboard changed and not check every second if that is the case.
Secondly I found a Method that might work
this.clipboard = Toolkit.getDefaultToolkit()
.getSystemClipboard();
this.clipboard.addFlavorListener(new FlavorListener()
{
#Override
public void flavorsChanged(FlavorEvent e)
{
// work with the clipboard contents
}
});
But still I need a strange while loop that consumes processor time to keep the program active.
I think that I should use wait and notfiy, but I'm not sure how.

Why this program got terminated in Eclipse?

There is nothing wrong with the code but when i compiles it, the header of console shows this
<terminated> CopyFileToNewFile[Java Application]C:\Program Files\Java\jre1.8.0_45\bin\javaw.exe
package com.princess;
public class CopyFileToNewFile {
public static void main(String[] args)
{
try(
FileInputStream in = new FileInputStream("myfile.txt");
FileOutputStream out = new FileOutputStream("new.txt");
) {
int c;
while((c=in.read())!=-1)
{
out.write(c);
}
}catch (FileNotFoundException e) {
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
}
}
I am using JAVA 1.7 version also i'm not used to with Eclipse
<terminated> in Eclipse simply means the program finished executing, not that something went wrong.
If you want to keep the program "alive", you have to make it wait for something, like user input, Thread.sleep(), or something else.

MusicXML files in java swing - visual representation and dynamic editing

I'm developing a desktop application with java swing which should be able to display the visual content (notes, clefs, measures) defined in a MusicXML file in the frame. All .xml parsers that I found allow me to only create trees. I couldn't display the content with the JEditorPane, too.
Can I do it or will I need to first transform it dynamically to some other format such as .pdf? If so - how can I do it in java?
Use a JTextArea to let the user edit raw XML. Load the file in the background using SwingWorker, as shown here, to mitigate the effect of any latency.
#Override
protected Integer doInBackground() {
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader("build.xml"));
String s;
try {
while ((s = in.readLine()) != null) {
publish(s);
}
} catch (IOException ex) {
ex.printStackTrace(System.err);
}
} catch (FileNotFoundException ex) {
ex.printStackTrace(System.err);
} finally {
try {
in.close();
} catch (IOException ex) {
ex.printStackTrace(System.err);
}
}
return status;
}
By visual representation, I meant that I needed a way to display this type of visual output.
You might look at the JMusicXML project, which has "some Java awt/swing graphics player." More resources may be found here.

using java awt robots on browser

Trying to use java awt robots in the deployed application, initially got headless exception
thought changing headless property will do, but still the issue exists
try {
Field defaultHeadlessField = java.awt.GraphicsEnvironment.class.getDeclaredField("defaultHeadless");
defaultHeadlessField.setAccessible(true);
defaultHeadlessField.set(null,Boolean.FALSE);
Field headlessField = java.awt.GraphicsEnvironment.class.getDeclaredField("headless");
headlessField.setAccessible(true);
headlessField.set(null,Boolean.FALSE);
System.out.print(GraphicsEnvironment.isHeadless());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
try {
Robot robot = new Robot();
but now it says
java.awt.HeadlessException
at sun.java2d.HeadlessGraphicsEnvironment.getDefaultScreenDevice(HeadlessGraphicsEnvironment.java:64)
basic use case is to simulate keypress(0) on browser window, need some help in solving this issue.

Java Applet In Web Page Does Not Respect Set Look And Feel

I am trying to set the look and feel (LAF) of a Java applet that is used via a web browser. I wish to set the system default LAF, but when loaded in a browser, the applet returns to the Metal LAF. When I run it as a stand-alone applet, the LAF is applied correctly. The only item I am showing the user is a JFileChooser. I have tried a number of methods to overcome this including:
1) Override the applet's start() method:
#Override
public void start() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
System.out.println("LOOK AND FEEL SET!");
}
catch (Exception ex) {
System.out.println(ex);
}
}
2) Set it in the static initializer of the applet class:
static {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
System.out.println("LOOK AND FEEL SET!");
}
catch (Exception ex) {
System.out.println(ex);
}
}
3) Set it in the constructor of the applet:
public MyApplet() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
System.out.println("LOOK AND FEEL SET!");
}
catch (Exception ex) {
System.out.println(ex);
}
}
I am using Java 6, but targeting Java 5 on Windows. In every case, LOOK AND FEEL SET! gets printed to the console, so I know that it set it without throwing an exception. This happens irrespective of browser (using Firefox 3.6 and IE7). Why is it doing this and how can I get it to respect the LAF I designate?
I used this code in an applet I developed recently:
public void init() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
// Just accept the default L&F
}
SwingUtilities.updateComponentTreeUI(this);
super.init();
// Now add components...
}
See also Look-and-feel of an applet window changes on subsequent displays (I have not solved this problem because my applet did not need to open pop-up windows.)
So I tried finnw's answer and marked it accepted without realizing that I had also made some other modifications to my code. When I was cleaning out code I removed my mods and left finnw's, but then it was broken again.
These were the changes that had made that worked:
JFileChooser chooser = new JFileChooser();
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(chooser);
}
catch (Exception ex) {
System.out.println(ex);
}
So what I ended up doing here is setting the look and feel for the file chooser outright, instead of trying to force the LAF for the whole applet. It's kind of a hack, but the file chooser is the only part of the UI that the user even sees anyway.
There does appear to be one obscure mistake that virtually every applet ever makes. Swing (also AWT components) is being used off the AWT Event Dispatch Thread (EDT). The applet threading model is a little eccentric.
This is the one time when invokeAndWait should be used with this extreme boilerplate:
#Override public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() { public void run() {
initEDT();
}});
} catch (java.lang.InterruptedException exc) {
Thread.currentThread().interrupt();
} catch (java.lang.reflect.InvocationTargetException exc) {
throw new Error(exc.getCause());
}
}

Categories