Control of JLabel - java

I have built, with a GUI builder, a set of JLabels and 4 arrows in a JFrame. I want, when I press one of the arrows to be able to perform operations on the correspondent label. I.e when the control is on the first label the "right" arrow would "bring" the control on the right label. I also want to mention that due to GUI builder I can't(??) use array and increase/decrease the pointers. Any ideas?:)

It sounds like you are trying to pair your GUI too closely to your data. When someone clicks on a button, it should perform some action on your data. Once that action is complete, the GUI should be updated to reflect the new data. This is much easier than moving controls within the window. This is known as the Model View Controller pattern.

Read the section from the Swing tutorial on How to Use Key Bindings. In general, this allows you to define an Action that is executed when a KeyStroke is invoked. So you can have a different Action for each of the right/left/up/down arrow keys.

Related

Using the same button in multiple frames

I'm studying Java Swing, and I created some JFrame windows, in some of them, I created a button and they have the same solution (like search or save information), however, all the time I've had to create from the begining or use ctrl+c ctrl+v. Is there a way to create a JButton and use the same button in multiple frames?
And for JTextfield, there's a way to create only once and use for multiples frames?
There's an example using a unique button and showing a "Hello World"?
Swing components can only be contained in one container (JPanel, etc.). However, most Swing components are backed by models, and those models can easily be shared between multiple component instance. In case of JButton there are actually two models: a ButtonModel and an Action. You can use add ActionListeners to the ButtonModel and that should work. However, I've never used that, since I prefer to use Action for buttons.

Swing: perform action on main JFrame

Say I have various JFrames open in the same application. Is there a way to perform some action (like update a JTable) when the user changes the focus on one frame to another (like clicking the bar on the top)?
If not is there a way to perform an action on one JFrame when she closes another JFrame?
Please read: The Use of Multiple JFrames, Good/Bad Practice? to see why your program design could very well be improved
As for your main question,
Is there a way to perform some action (like update a JTable) when the user changes the focus on one frame to another (like clicking the bar on the top)?
It's really a specific example of a more general question:
How do I change the state of one object through an event that occurs in another object
and there are several possible solutions
Easiest would be to have the code that handles the event have a reference to the first object, here one of your JFrames, and simply call a method on it when the event is triggered.
Better is to structure your program with an MVC (Model-View-Controller) type structure, and in the event code (the control code), change the state of the model. View listeners to the model, including the JFrame you wish to change, would then be notified and would change their appearance based on the model.
Some general recommendations:
Having a bunch of windows displayed and swapped is very annoying to the user. Have a look at my link above to look for other possible options.
You'll probably want to avoid having class's extend JFrame, as that forces you to create JFrames with that code. Much better is coding to the JPanel, not the JFrame, and then placing the JPanels created wherever they are needed, be it within a JFrame, or in another JPanel, or swapped via a CardLayout, or in a JTabbedPane, a JDialog, a JOptionPane...

Puzzle game with KeyListener using Java GUI?

I want to implement key listener on my puzzle game.
i have done it with action listener but now want to move on with key listener.
My logic for action listener was that:
when a specific button is clicked
it checks if is adjacent button's icon is null
if it is null then their icons will be swapped
Now, how can I do it with key listener?
Thank you.
if( b1==e.getSource()){
if(b2.getIcon()==null){
b2.setIcon(b1.getIcon());
b1.setIcon(null);
}
else if(b5.getIcon()==null){
b5.setIcon(b1.getIcon());
b1.setIcon(null);
}
}
You tell us that you have implemented a KeyListener but it's not working. Without code, all we can do is guess, so here's mine:
KeyListeners require focus to work, and so if your GUI has any components that steal the focus, such as JButtons, all JTextComponents such as JTextArea or JTextField, JComboBoxes, JLists,... then your KeyListener won't work.
One kludge is to force the component with the KeyListener to have the focus and to greedily hold on to the focus. This is not advisable since it will force your program to behave abnormally since most users expect buttons to have and retain focus when pressed, and text components won't work if they're not allowed focus.
Again, often the best solution is to use Key Bindings since these can work without requiring focus and are a cleaner way to capture keystrokes. Please look at the Key Bindings Tutorial and then have a look at my example code for using Key Bindings here and here.
Again for better and more specific help, then please tell us more of the details and show us your pertinent code, preferably as a minimal example program or MCVE.

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.

What is this component name

What is the name of the component in java Swing shown in the following link
http://www.scriptocean.com/template3.html
It is known as extended ListView in Android. But I want to know the same in Java Swing.
Do you mean this component ?
If so, to display it in Java, you have some choices.
If you want your items to be easily clickable (that's to say action senders), you would tend to use JButtons in a vertical BoxLayout 'ed JPanel
If you simply want to display items, then customize their display, you would undoubtly go the JList way. Also take a look at Swing tutorial, which is always of great help.
EDIT
Accordint o comment, to have an area below the button displaying content, you'll use the second solution with a twist. As all elements in Swing are in fact JComponents and can be put in thers, you'll use JPanel as JList elements. in each JPanel, you'll have ione button always visible and one sub-panel that is hidden at startup. When clicking the JButton, you'll simply show or hide the associated sub-panel. If you want to have some kind of effect, you can either
wait for the upcoming JavaFX transitions effects
Use Filthy Rcih Clients animations library (take a look at their links page).
There is no standard Swing component that behaves like in your example. But you can find something similar in the SwingX project : the JXTaskPane and JXTaskPaneContainer components.
Unlike your example, the sections are not exclusive. But you can achieve this exclusivity with a few lines of code.

Categories