I am working with java applets inorder to embed this applet into one web page. In this applet i used a Choice class to show dropdown list. I know ItemListener helps to know which item is selected once we select an item but Is this possible to know which item has the foucs currently??. i.e, i used the following code
choice = new Choice();
choice.addItem("");
choice.addItem("Choice1");
choice.addItem("Choice 2");
choice.addItem("Choice 3");
Now if i place cursor on choice2 instead of click on it,it has to perform some action. What are the ways to do this?
If I understand your question correctly, you want to add an ItemListener. After you do that, the item listener will be called when something is selected by user input, and then you can take the appropriate action.
One question though, is there a particular reason you're using awt instead of swing?
Related
I want to create something similar to a listpreference menu item with a single selectable option out of four options. The problem is that descriptions are necessary for each selectable option and they are rather long. Shortening descriptions further isn't an option.
The Android app I'm working on currently uses a custom method for displaying a menu item's summary field. The summary calls an int which points to a string reference stored in a different file that displays a string. I tried using %s as the summary to update the summary with my selected option but that literally returned "%s". I don't think the current method supports dynamically loading summaries.
My next idea is to create a submenu with four selectable choices (maybe checkboxes?) and only allow one to be chosen. I think this is essentially a listpreference menu item but allows more space for separated descriptions. Is there a preferred way to accomplish this? I'm trying to avoid re-writing the original custom method for displaying descriptions as it works well for the rest of the app's menu needs.
EDIT 1: An image will help explain what I want. See Android sample settings menu. I want to create a section similar to the "embedded frame buffer" section in a submenu except only one of the three options are selectable. This approach will allow me to have more room for separated, long descriptions. Perhaps I'm looking for radio buttons with no popup and one selectable choice? Is this a thing in Java?
Not sure if I understand the question correctly but instead of a list of checkboxes where only one checkbox is checkable use a RadioGroup with Radiobuttons. You can have multiple RadioButtons which you add to a RadioGroup and then only one RadioButton can be selected.
I have added checkboxes inside combo box in Java. But when I open drop down menu and check one check box, the drop down menu closes. So to select each check box I have to open it every time.
Is there any way so that I can keep the drop down list opened till the time I dont click outside so that I can select any number of check boxes at one time only.
Please help!!
I have added checkboxes inside combo box in Java. But when I open drop
down menu and check one check box, the drop down menu closes. So to
select each check box I have to open it every time. Is there any way
so that I can keep the drop down list opened till the time I dont
click outside so that I can select any number of check boxes at one
time only.
no there isn't, this is default property of (BasicXxx)Popup implemented in Swing API, workaround for series of Bugs in Java1.4_xxx
no_way, only by using dirty hacks, usage of can be Java Version sensitive, or required left mouse button as accelerator
don't do that, another way (and proper of possible ways) is usage of JWindow/undecorated JDialog but required to override ESC Key and Focus lost in Windows three (as you can see in good Java JCalandars/JDatePickers),
I recommend that you to use the Japura API to deal with this, check this link:
http://www.japura.org/checkcombobox
Best Regards :)
I am looking for a way to create a popup dialog box when a user double clicks a textinput field that will contain a scroll-able list (from database table) where the user can select a field, hit ok, and have it placed into the textbox when popup closes.
The other major requirement is to have a filter/ or search field in the popup to aid the user in finding the correct option to select from quicker.
What is the best way to implement this?
Modification to gwt's popup panel? maybe a JOptionPane? are there any simple solutions already designed for free commercial use?
You could implement this with a com.google.gwt.user.client.ui.PopupPanel. You can make a PopupPanel that contains a ListBox with your data from the database, along with a OK button. When a user selects a value and hits OK, you should utilize an EventBus along with a custom Event that will pass the value to the field on the page. The page will have an event handler that will catch the event and put it into the field.
Another option is to use a com.google.gwt.user.client.ui.SuggestBox. It is a box that autocompletes / suggests values as you type, kind of like the Youtube search bar.
I can offer more resources to help you accomplish this, if you'd like.
I have a JComboBox. I add a ActionListener using the following code:
addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
textComboActionPerformed(evt);
}
});
This is what it looks like when it is first displayed. It has a couple options in it.
You will notice that Basic Text Box is the first element, and so it will be the element that shows when the JComboBox is first displayed.
If you click on the JComboBox, you will see the options that are available under it.
However, if I select "Basic Text Box" it will not cause the ActionEvent to fire. It has something to do with it being the first element in the list. If I select any other JComboBox, then the ActionEvent is fired.
EDIT: On Linux, say you select an element, and the event fires. Then you select that element again, it will not fire the second time. It is not isolated to just the first element. It has to do with selecting the already selected element twice.
This behavior only happens on Linux. On Windows, the Event fires not matter which element I click on, even the first. Any ideas on why this would be? Does behavior like this vary from JVM to JVM?
Thanks
First, I think the correct listener to use is ItemListener (instead of an ActionListener).
As you state in your comment this gives you consistent behavior across platforms: you don't get an event at all when the already selected item is "re-selected". This is exactly how an ItemListener is supposed to work according to the JavaDocs:
aListener will receive one or two
ItemEvents when the selected item
changes.
When you select the same value that's already selected, obviously you don't change the value, so no event is fired. However, that's not quite what you want apparently. As an alternative, I suggest displaying the JComboBox without a preselected item:
JComboBox comboBox = new JComboBox(model);
comboBox.setSelectedItem(null);
I don't know if that's possible for your application but this way you'll definitely get an event whenever an actual value is selected. (It also makes more sense from a usability point of view, I think, because why would a non-expert click on a combo box to select the value that's already selected?)
I have a JTree which displays a JPopupMenu when I right click nodes in the JTree. What is the best way to hide/show or enable/disable certain JMenuItems in my JPopupMenu based on the node selected in the JTree?
The first solution that came to mind was to add a TreeSelectionListener to the JTree for the 'value changed' event. In the event handling code I would use the TreeSelectionEvent's getNewLeadSelectionPath() method to get the path of the most recent selection, and use the resulting TreePath object's getLastPathComponent() to get the selected node. From here I would have a series of IF statements that access my JPopupMenu object and perform the modifications necessary to hide/show specific JMenuItems.
However, something feels off about this, and so I decided I would ask SO if there was a better approach.
The way that I chose to tackle this within my own app was to use the "userObject" property of the DefaultMutableTreeNode class which allows you to just store any data you want along with your node. I have a variety of types of things that extend from an abstract base class which defines a "createPopupMenu()" method. Then, in the selection listener (just as you described in your question) I get the user object and ask it to create a popup menu appropriate for the selected object and display that.
Getting the selected tree node is straight forward and should work as you described it. To modify the popup menu I would recommend using Actions. This way you wouldn’t have to modify your live menu and could also add e.g. a JToolBar that contains the same actions that react the same way the items in your menu do.