I have a Java application which displays results on a JPanel. The results are displayed using HTML by using a JLabel.
Now I want to let the user interact with the local application by allowing local methods to be called when the user clicks on a link in that HTML document.
Is this possible?
To answer you question, then, it is possible, however you cannot use a JLabel, you need to insert a JavaFX component, and then you can set your class as a window variable on the DOM, and thus your methods can be called from JavaScript.
Have a look at this answer on this question. It looks like they are doing exactly what you want.
Related
I'm currently learning how to create a program with Java and jFrame. One problem I have is that I cant create new "forms" (how they are called in visualbasic) or windows. I'm using "Java-Editor", a usually very simple editor for things like that. Can anyone help me create a new form?
Thanks for your help in advance,
Till
From JFrame I assume that you are using Swing, and that you want multiple windows for your application. Your app should only use one JFrame object, if you need more windows (usually these are popup messages) then you can use dialogs. The class for this is JOptionPane.
Here is an example:
JOptionPane.showMessageDialog(null,"title","content",JOptionPane.INFORMATION_DIALOG);
As you can see you can control the title and the content displayed. Note that for the content parameter you can pass any JPanel object, so you dialog can display a lot of things.
The first parameter is the owner of the dialog, now I just set it to null, which means the dialog will have no owner. If you store a reference to your JFrame object then you can pass this for example and the dialog will always appear above your main window.
The last parameter is just for the general styling of the dialog. You can set it to other message types like ERROR MESSAGE too.
More info about displaying simple dialogs: https://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html
In windowsbuilder, I would like to change information from a seperate window, more specifically a table, from my main window. Is it possible to use my information from one seperate window to change it in another box?
Basically, take information from here:
And put it into this table:
Is it possible? If so, how? Thank you in advance.
Yes, it is possible - but not in Windows Builder.
Create an object which will hold the information from the first window and pass the information from the window to the object. Next, pass the object to the second window (how you do this will depend on how you've configured your window objects). Finally, populate the table with the information from the object.
As far as I know, there is no direct way to do this in Windows Builder, although the above method will work fine provided you can pass objects from the first window to the second.
Okay so I'm making a Library admin program and I have created a special frame where the user would enter details about a new book. However my method for adding a new book is in a separate class (methods). My question is how can I get the information the user enters in the text fields? Do I have to use something like getters, or is there an easier way. Also keep in mind that I am using the GUI layout (thing) in netbeans, and that I have already actually made the form. (I know it's frowned upon but I'm pressed for time and this is how we were taught.) This is a school project by the way. Thanks.
Okay so I'm making a Library admin program and I have created a special frame where the user would enter details about a new book.
Usually, a detail window should be a dialog, and likely a modal dialog. I suggest that you display this information in a modal JDialog, not a JFrame. Do this and it will make extracting information from the detail window much easier.
However my method for adding a new book is in a separate class (methods). My question is how can I get the information the user enters in the text fields? Do I have to use something like getters, or is there an easier way.
This begs the question -- what's so hard about using getters? And in fact his is exactly what I suggest that you use! Please note that your question essentially boils down to, "how can I get information on the state of one class's object from within another class's object", and for this getter methods are almost mandatory.
Also keep in mind that I am using the GUI layout (thing) in netbeans, and that I have already actually made the form. (I know it's frowned upon but I'm pressed for time and this is how we were taught.) This is a school project by the way.
This is unrelated to your current problem and should have little effect on its solution other than if you've hard-coded your "form" as a JFrame, then scrap it and re-do it as a JPanel.
I suggest:
Create an addEditBook modal JDialog
Give it getter methods to allow outside classes to be able to query its textfields for their contents.
Display the dialog from the main program.
Since it is modal the main program's code flow will pause until the dialog has been dealt with.
In your OK and Cancel button, set the dialog's state (OK_STATE or CANCEL_STATE) and close the dialog. The easiest way to do this actually is to use a JOptionPane as your modal dialog since it has mechanism for just this sort of thing. This is easily accomplished if your addEditBook is geared to create a JPanel, one that you display in the JOptionPane.
Program flow will then resume in your main program from right after where you showed the dialog
query the dialog for the contents of its fields.
For examples of the JOptionPane solutions, including option panes that request information from multiple fields similar to your window above, please see:
How can I make a JFrame modal like a JOptionPane?
Multiple input in JOptionPane.showInputDialog
Edit
You state in comment:
Oh and I was wondering how can I make the field of a normal JOptionpane input dialogue come up with a word already in it like for editing it will show the information stored already?
Please see the example answers that I have listed above as you'll see that they're not examples of a "normal JOptionPane" but rather JOptionPanes that display a GUI that you create. And just the same as it's easy to query the state of this GUI after it is displayed, it's just as easy to set the state of the GUI via setter methods before it is displayed.
My question is how can I get the information the user enters in the
text fields? Do I have to use something like getters, or is there an
easier way
You need to add actionListeners for you buttons, which means you will be overriding a method called actionPerformed. You basically need to associate your actionListeners with your 'Ok' and 'Cancel' buttons. When the 'ok' button is pressed, you should get a callback in the associated actionPerformed method. Then you should try to fetch the values of your textfiled using the getText method. Collect all the fileds and set the bean you have created to store that data. Then you can call your business logic to save/modify the books info.
I am trying the save the state of a previous frame and then carry it over to the other frame. More like saving the data of the textfields and areas so that when i press next button some text fields,and variables will be initialized in the next forms Labels and when you press back your previous data will still remain in your form inteface.Pls help
More like data binding but across different forms
You can use any varible for that, and pass it as parameter to every JFrame you create.
But it sounds more like you want to use a CardLayout for your JFrame and use different cards that can be shown for the user. See How to use CardLayout.
If you are trying to create a wizard like UI, you should look up Sun(oracle)tutorial here.
Use something like Java's XMLEncoder.
I have a problem which I am currently trying to wrap my head around, and any advice or nods in a good direction would be greatly appreciated.
I want to display a Google Map within my Java Swing project, (the map will be a URL specified within an HTML document I think).
I also want to be able to communicate and interact with the map using JavaScript, injected via buttons in java swing, etc. So for example, I could have java buttons 'Satellite', 'Hybrid', and 'Earth' next to the map, and clicking them would perform the corresponding javascript action on the map. JavaScript methods would probably already be created within the HTML file (such as 'switchToSatelliteMap'), it would just be a matter of calling them within Java.
Thanks in advance for any help whatsoever,
tre.
I don't know whether this answers your question at all, but I think you will find these links helpful:
http://today.java.net/article/2007/10/24/building-maps-your-swing-application-jxmapviewer
http://swinglabs.org/downloads.jsp
GoogleEarth inside Java Swing
Is there a Swing component for Google Maps?
http://code.google.com/p/gameplan/source/browse/trunk/src/org/crazydays/gameplan/map/swing/JMapFrame.java?spec=svn62&r=62
I am more of a SWT fan, so I would have used a browser control as it allows me to execute javascript on the browser component. But again its a design choice.
Hope this will help.
I am not entirely clear on the scope of your issue, but you may find it helpful to attach MouseListener interfaces to your swing buttons
http://download.oracle.com/javase/7/docs/api/java/awt/event/MouseListener.html
http://download.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
Implementing the mouseClicked method to run the required java script.
Additionally you could use a MouseAdapter if all you need is the mouseClicked functionality.