java swing GUI. Allow user to rearrange buttons - java

Many programs allow to freely rearrange buttons on gui. For instance Firefox and Chrome have "customize" option. How to do it in Java? Sure, I can bring GUI constrains to settings file and let user edit it, but this isn't user friendly.
Is there a better way to do this?

How to do it in Java?
There is no built in support, so you need to provide this yourself.
You might start with a configuration dialog. It could display a JList with the text of all the buttons in the order in which you want the buttons displayed.
Then the user would be able to drag each item in the list to a new location
When the user is finished they would click "Save" and you would store the order in a user properties file.
Then you would iterate through the list and change the order of each button on your panel.
Every time you start your application you would need to read the user properties file and perform step 4.
Check out the Drop Demo Project from the Swing tutorial on Drag and Drop. It will show you how to implement the JList so items can be dragged to a new location.
So basically you need to write the code yourself.

You can try creating a preference menu like you find in Chrome etc. This will provide for a familiar and user friendly method to do so. You can also go ahead and allow the use to change icon or size if you want to.
Another way could be that you allow the buttons to be dragable and resizable across a container and using events based on movements of the mouse to reorder the buttons in question.

Related

How can I create a popup menu that allows user to choose a certain elements from a list on Java?

I am writing a Java program to record some events and their status, i.e., done or not. I have implemented the recording and calculations, but I also want to have a button that will open another window that will list the events with a checkbox corresponding to each of them, so I can pick which ones' statuses I will change to done. I have only used Swing libraries so far.
I want this window to contain one event per line, with a checkbox next to the event. Each checkbox should determine whether the status of the even should be changed or not. I had trouble even making such a window open and list the items, let alone put the checkbox.
Note: The design is not too important. This won't be a published project, I will mostly use it for personal work. I can do with buttons instead of checkboxes, where any click changes the status of the event next to it.
Thank you in advance for your valuable responses.

How to create dynamic wizard?

I am making project with GUI. The thing is, that I have a button and what I need to do is that after clicking this button I need to change Frame layout. For example, like when you are installing some program and you click "next" button, the Frame layout changes and you can see some different content. Basicly, dynamic wizard.
I have tried use another Frame, but it opens in another window and that is not what I want. I want to open it in the same window.
Another thing I have tried is set visibility of these components I don't want to be displayed to false, but I find it unprofessional and it is overlook in making a desing, when I have components over themselfs.
So do you guys have any idea? Thank you.
Most of the times for a wizard like GUI, you should have JFrame and a set of JPanels. In each step you can pass the shared data as constructor arguments to each panel, and when you are making one of them invisible and make another one visible, you can get some date from the previous step panel and pass it to the next step panel(if needed).
It is very common that your panels extend the JPanel and have some argument in their constructor(s). You use these data for initializing your panel and managing the state of the overall progress.
There is no a total plan for all situations. So you should decide what to do which is best fit for your case.
Try not to have multiple JFrames.
Hope this would be helpful.

How do I refresh a GUI in Java?

I have a general question that is Java related.
I am writing an application that has a GUI menu. I am trying to change one part of the GUI menu based on the selection of a Radio button.
Do I need to:
Redraw the whole window or just update that part with:
setVisible(true)?
If I just use the statement from #1 above .. the GUI is fine -- until I move the mouse over it and then I see the previous button choice. What am I doing wrong?
Swing components have a repaint(), revalidate(), and doLayout() method. One of those should probably be able to redraw whichever pieces you want. However, doLayout is not something that you should be taking responsibility for, that's the layout engines responsibility.
You may also want to check out this post, the first response has a pretty good explanation, and links to an article with more detail.
In terms of the second part of your question, I'm not sure, but we may need to see some code to get an idea. Is the 'replaced area' actually being removed from the view?
..in my application the user select which device platform type they want top test (that choice is a set of two radio buttons on the left). When the user selects either Android or iOS, the center grouping of check boxes changes to reflect a group of android devices they can test or a group of iOS devices that they can test.
Put a panel in the 'center grouping'.
Use a CardLayout for the panel.
Add both iOS & Android controls to the panel with the card layout.
Flip between them as needed.
Call revalidate() on the top level component.

Should I create a new window or modify the old one?

I am programming a GUI application in Java. I do it for the first time.
I would like to have a form (with radio buttons and so on). After the form is filled in and the "Submit" button is pressed I would like to have a new window. I see two potential ways to do it:
Close the "old" window and open a "new" one.
Remove "old" elements from the existing window and put there "new" elements.
What is the standard way to go? If it is the first way, what is the command to close the window? If it is the second one, how can I remove elements from the existing window?
What you should do is create new JPanel for all the windows you want to show, then remove (or hide) the panel you want to hide and add or show the one you want to show.
I don't know too much about Java so I can't answer to your specific questions, but I want to remind you of the window opening/closing effect since Windows Vista: It looks kind of weird in some older setup wizards where everytime you click next the window fades out and in...
I think the most logical way is to have 2 objects("Close the "old" window and open a "new" one")
Anyway, I suggest you make an abstract class with the common elements, and then extend it with Window1 and Window2.
Java frames are destroyed with the dispose() method.

Java Menu issue

I have a menu with a few JCheckBoxMnuItems. How do I ensure that the Menu stays open until I have done all my selections (i.e. checked the menuitems) and does not close on just clicking one of them?
I'd rather not try to change the normal menu behavior for an application or for a part of the menu tree. A User expects that the menu closes automatically after a menu item is clicked. And, if you kept the menu expanded, what kind of action would you invent to close it manually after you've done your last selection?
If there's a requirement to change more then one setting within one use case, then you should consider to provide a small dialog where the use can apply the changes and confirm them at once. I believe, that's more consistent with typical behaviors of UIs.
And it declutters the menu bar, you'll have just one 'setup' menu item instead of a dozen (?) check box actions :)
I guess menu's aren't supposed to allow multi-selection.
But you may offer keyboard shortcuts to set the menuitems without using the menu at all.
If the set-operation of your flags is a central aspect in your application, I would tend to use a dialog here. These are all suggestions which do not require to change the internal implementation of the existing controls, even though I know, that it would be possible in swing.
I agree that it is better to do this with standard UI. However, if do you want to add checkboxes that do not close the menu it is surprisingly easy:
JCheckBox checkBox = new JCheckBox("Text");
checkBox.setOpaque(false);
checkBox.setRequestFocusEnabled(false);
menu.add(checkBox);
This may not work on every look and feel and the check boxes will not line up with menu items in the same manner as JMenuItems but it seems to be a reasonable place to start.

Categories

Resources