java class property binding to swing component - java

I'm coming from .net world, but I have to make a small GUI application in Java.
I would like to achieve that I have a class with some properties and I want to bind the properties to swing components (textfield, combo box) on a JDialog, JFrame.
There are many threads in the net talking about this, but I was not able to achieve the stuff to be working on my case.
I have a class with a property PropertyChangeSupport, and addPropertyChangeListener and removePropertyChangeListener methods.
In the GUI I could bind the JTextField text attribute to the class instance property. the JDialog is implementing the PropertyChangeListener
but if I write something to the text box nothing is updating in the class property.
Can you point me a complete tutorial about this issue?
thanks!

"but if I write something to the text box nothing is updating in the class property."
You're probably going to want to implement a DocumentListener to listen for changes in the underlying document of the text field. Every time the document changes, for instance the typing in of a letter, a DocumentEvent will be fired. You can see How to Write a DocumentListener

Related

Finish input into JEditorPane on ENTER

I am using a JEditorPane as a component to show code. The JEditorPane resides in my custom PropertyEditorSupport for my Netbeans Platform app and is show in an OutlineView and the Properties window.
I've already limited the JEditorPane to be one line only, using a DocumentFilter. However I am not able to rebuild the funcionality a JTextField has, to finish input into the component by hitting the ENTER key.
I have already thought about adding a KeyListener event to my JEditorPane but am not sure what to do in case of the event. Is there a method to be called to "finish input of text"?
I am using a JEditorPane as a component to show code.
I've already limited the JEditorPane to be one line only
Those two statements seems to contradict one another. Usually codes consists of multiple lines, not one line.
In any case if you are simply display text then I would use a JTextPane as it supports text attributes. A JEditorPane is used to display HTML.
I have already thought about adding a KeyListener event
Don't use a KeyListener. Swing was designed to be used with Key Bindings. Basically you create an Action (in your case you would extend TextAction) and you bind the Action to a KeyStroke. Check out Key Bindings which contains more information as well as a link to the Swing tutorial on How to Use Key Bindings.

can Java Swing JPanel Hold a String Value?

Can a Java Swing JPanel hold a String value than can be modified/accessed where I can save some information?
I see no other option but to implement my own class holding a JPanel and a String... was just trying to save some space/coding..
I like the setName/getName of the Component super class... is there any inconvenience in using that ??
Yes it can. Any Swing component can hold client properties for the specific component.
See the putClientProperty(...) and getClientProperty(...) methods of JComponent. Using this approach you can define any number of client properties:
panel.putClientProperty("Title", "Panel1");
panel.putClientProperty("Description", "some text for the description");
You can also use the setName(..) and getName() methods if you just want to uniquely identify the panel with a string name. Many IDE's will use this property.
Of course if you are creating a panel with multiple Swing components and related instance variables then you would probably extend JPanel and customize its behaviour.
I like the setName/getName of the Component super class... is there any inconvenience in using that ??
If you feel the "name" property adequately describies the data you want to store then this is the most efficient way to store the data. However, if the data is not really the name of the component then don't force the data just because it is easy to use. Also it is possible some IDE's may use this property for generic debugging or messaging. That it may check display this value in an error message to help identify a specific component.
Several solutions, and it's hard to know what you're looking for here. You could create a MyJPanel class that extends JPanel and is identical except including a String field with getter/setter. You could also store information in silly ways like by setting/getting the name of the JPanel. (That is use setName and getName of the Component superclass.) Another solution is to add a JLabel or some other component with that information to the JPanel, and if necessary, making it invisible or hidden.
No, I think it cannot hold a String value.
You can see all getters/setters here:
http://docs.oracle.com/javase/7/docs/api/javax/swing/JPanel.html
Also, you can check the source code (if you want to go that far).
There's no getter/setter useful for holding a String value
(I mean ... e.g. no setText or setTitle).
Of course, you can add e.g. an invisible JTextField to your JPanel and
set the String into the JTextField. But that doesn't seem very nice to me.
JPanels hold JComponents, i.e. JButtons, JLabels, etc. A String is not a component. It would be best if you'd just use a JLabel with a String as its parameter, then add that to a JPanel.
You can add a Component like JLabel/JTextField and use setVisible(false). That object can hold strings.
note: Only for Buttons.
You could use setAtionCommand() but it is indented for something very specific. It would be horrible practice for anything else.

Listener on text within JLabel

I'm building a program in Java, using Swing, that will act as an interactive presentation.
I have paragraphs I need to display (presumably in JLabels) , and within each paragraph are certain words and phrases that need to be formatted differently (have a different color), and I need them to call a method that will display something else when clicked or hovered over.
I know there must be a way to accomplish this...
If you want to apply some style, you can use HTML in your JLabel's content.
If you need some custom behavior and you want to handle it in a different way for some parts of your JLabel, you need your component to be split in a more detailed way.
Create a container(JPanel) and arrange you components within it. From now, your smaller components will be able to listen for events like mouseEntered and mouseClicked and handle them separately, not confusing the whole JLabel component.
In this way, every smart part of text will be a standalone component.

Calling JComboBox with JFrame

I´m new in creating GUI in Java, I would like to implement JComboBox (which uses GlazedLists), in my JFrame, which is in another class. Is it possible to call this JComboBox in my JFrame? I´m asking because I use NetBeans (some parts of code are forbidden to edit). Thanks
You can access the reference to the JComboBox like any other variable. The name of the combo boxes variable will depend if your reamed the object or not (you can do this from the form inspector, usually found under the projects window when in design mode)
While you can't modify the code in the protected blocks, you still effect he combo box (or any other component) within your code normally, just make sure initComponents has been called first

ontextchanged event in java

i am looking al events in java http://java.sun.com/docs/books/tutorial/uiswing/events/handling.html
i want ontextchanged event like in C# that when text changed event is executed.
Which event is the right one in java?
First, you should say which Swing widget you're looking at,a s behaviour is quite different. But, to make long things short, here is a primer.
Most of text widgets use, internally, a model which is a Document. This Document is updated by the widget and sends, in turn, Document events. So, to listen text change whichever widget you use, you can create a DocumentListener.
I also suggest you take a look at some lessons of the Swing tutorial :
How to Use Text Fields
How to Use Formatted Text Fields

Categories

Resources