public void valueChanged(TreeSelectionEvent event) {
//Add images depending on selection.
String selection = navigation.getLastSelectedPathComponent().toString();
if (selection == "Sigma") {
try {
Style style = document.addStyle("StyleName", null);
StyleConstants.setIcon(style, new ImageIcon("sigma.png"));
document.insertString(document.getLength(), "ignored text", style);
} catch (BadLocationException e){
}
}
}
Hey all, so I've debugged everything and everything is working correctly except for the ACTUAL INSERTION of the icon.
Can anyone explain to me why this isn't working? I have a try and catch statement, yet it still seems to fail on me.
PS: Don't ask for more code, my code compiles perfectly without this code. document is a global variable, and, I used styling to insert the icon (correct me if I'm wrong).
You have one problem:
selection == "Sigma"
that's not how you compare strings, change it to:
"Sigma".equals(selection)
Also don't swallow the exception:
} catch (BadLocationException e) {
//do something here
e.printStackTrace();
}
Related
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.
I have a problem with displaying certain KMLs in Google Maps, it happens that after going through the method addLayerToMap, it is not rendered on the map.
Funny is that when I step it in Google MyMaps the same works normally and even if I export from there and set to display in Google Maps of the application, it displays normally.
I noticed that MyMaps greatly changes the structure of the KML and it is even smaller (in number of lines and consequently the size).
KML file (original): https://drive.google.com/file/d/1Z4AZMP1xNMgVNNXjK11-kD0gwlPLmJmR/view?usp=sharing
PS: On invalid paths of images, I changed manually and there were no results.
KML file (parsed by Google MyMaps): https://drive.google.com/file/d/1WPT3ZogzjTNa9ITeZze1cYf3ly4JFpUZ/view?usp=sharing
Method that I'm using to read KML (works with most of the KMLs I tried, including Google's own example):
private void retrieveFileFromResource() {
try {
KmlLayer kmlLayer = new KmlLayer(mMap, R.raw.teste3, getActivity());
kmlLayer.addLayerToMap();
moveCameraToKml(kmlLayer);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
I'm trying to add the components to the map manually (polylines, polygons, markers, etc) but did not succeed.
try using the below code to check if the kml file is present in the folder and if present, show them in the google maps.
private void retrieveFileFromResources()
{
try
{
int check = this.getResources().getIdentifier(teste3,"folder name", this.getPackageName());
if(check != 0)
{
InputStream inputstream = this.getResources().openRawResource(this.getResources().getIdentifier(teste3,"folder name",this.getPackageName()));
KmlLayer kmlLayer = new KmlLayer(mMap, inputStream, getApplicationContext());
kmlLayer.addLayerToMap();
}
else
{
Toast.makeText(this,"Request KML Layer not available",Toast.LENGTH_LONG).show();
}
}catch(IOException e)
{
e.printStackTrace();
}
catch(XmlPullParseException e)
{
e.printStackTrace();
}
}
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?
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.
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());
}
}