difference between Jlabel and noneditable JTextField - java

Forgive me if this question is trivial. I am not a GUI expert. However, i need to create a Swing control in which the text will only be inserted programmatically based on some calculation (user input never allowed). Are there any preferences here to use JLabel vs. noneditable JTextField? I understand that both will work, but just curious if it is better to use one or another in such circumstances.

Unlike a JLabel, a non-editable JTextField can still be scrolled horizontally to see text that would be replaced by ellipses in a JLabel. More on the latter can be seen here.

The difference is how they look and behave. Also you can cut(&paste) from JTextField.

Related

how to keep focus on jframe when jtextfield or button are selected

like the title says. I'm trying to figure out how to keep the focus on the JFrame or perhaps the window. So that when I press F1 on the keyboard, it activates a method. And it should also do it when I'm typing in a textfield.
I have read through the "How to Use the Focus Subsystem" but can't find what I'm looking for. Or maybe I just don't know where to look.
I tried using contentPane.setFocusable(true); but it looses focus when I activate another component. So how do I get it to keep focus?
You may be trying to solve the wrong problem caused by using a KeyListener, which requires focus. Instead, use key bindings or setDefaultButton(), found in the frame's root pane.
Addendum: I tried key bindings, and solved it almost. It still does not work when a JTextField is selected. Do you maybe have a solution for that?
Depending on your needs, consider a DocumentListener or DocumentFilter. See this Q&A for more on the two.

setting JLabel transparency

You may see this as a duplicate question but please here me out.
I have a JLabel with an image. This JLabel has a mouse listener binded to some function.
Is there any way to make the JLabel disappear (or technically, transparent) in order for the mouse listener function to be preserved?
I know I can set a transparent image as an icon to the JLabel, but I'm wondering if there's some kind of "toggle" function out there.
you could use yourLabel.setVisible(false); is not the same as dispose the frame; however, would you extend your question a little bit more or make it clearer?(I'm having a hard time trying to understand what do you want to do)

Unable to introduce Text in next line when using Header Checkbox

I'm definitely not the best at explaining problem but let me try.
I have an applet with me. In a JTabel I have one column as checkbox.
I want to have text added to it in the header only such that I have some text and than in the next line I have the checkbox. Using headerRenderer setText is not helping my cause as it appends the test to the checkbox instead of introducing a new line. Moreover I would like to avoid using JPanel because I think it will create lot more problems for me.
Can anyone suggest how I can get this done?
If JPanel is the only option can someone tell me how do I use that.
The Q&A How can I put a control in the JTableHeader of a JTable? includes several important caveats and shows an example using JToggleButton. In this case you want a two line JCheckBox without the complication of an enclosing JPanel.
You might look at implementing the Icon interface, as illustrated here for a TableCellRenderer. This would allow you to leverage the control offered by the component's horizontal and vertical alignment. For consistency, use the UI properties of the JCheckBox for the text, as shown in SelectAllHeader. I'd try rendering a JLabel in paintIcon (), but TextLayout, illustrated here, is an alternative way to get the correct metrics.

Selectable JLabel, without just a JTextfield

I need to be able to select the text in a JLabel. Ive read some guides on the net that talk about using a JTextfield to simulate a JLabel, however this is no use to me as I my JLabel will span multiple lines. So any ideas on how to do this or if it's even possible?
You can use a non-editable JTextArea which allows for multiple lines and selectable text, and visually looks the same as a JLabel.
I do not think it is possible with a JLabel (at least I do not know how)

on click add dynamic text area -like object in Java

The title is a bit confusing, but I will be using Java and Jframe. Basically, I want to be able to click anywhere on the form and have a "text area/box" show up (maybe use a JTextField or JTextArea ?). I want the user to be able to edit, delete and move this string around as well.
I am thinking I need an actionlistener to listen for clicks on the form. Each click will call for a new text"box" to be created. I am not sure how to make this "box" editable, deleteable, or moveable by the user though.
I need a way to store the string and co-ordinate data too. Would it be a good idea to simply extend the JTextField or JTextArea to add co-ordinate information to them? I see that swing is event based, so I need some kind of trigger to "save" the text (was thinking the enter key, but I realize I'd like the user to be able to enter multi-line strings).
Any thoughts would be appreciated. I am familiar with Java but only have a bit of experience with the UI portion.
Instead of an ActionListener you will need a MouseListener to track clicks.
Sounds like you need an undecorated JInternalFrame with a text box in it on JDesktopPane. However, I don't think you can create an undecorated JInternalFrame, maybe start with a normal JInternalFrame with a TextBox in it and create new frames on mouse clicks on the Desktop Pane. Then see if you can make the JInternalFrame more like a Window.
Another route is a custom component that does everything you need. This is possible, just a lot more custom code.

Categories