I have created a drop down list using JAVA Swing. When I select "Keep track of status of RCM:", I want to create another drop down list next to the option selected. should I use mouseactionlistener instead? I trying to accomplish something like in this, when I click menu options, there is another list which I can select under menu options category. Example : http://smoothjazztampabay.com/wp-content/rockettheme/rt_metropolis_wp/menu-options/dropdownmenu.jpg
I tried using this code but couldn't.
if (state == ItemEvent.SELECTED)
{
ItemSelectable itemS = itemEvent.getItemSelectable();
String cmd = selectedString(itemS);
if ( cmd.equals("Keep track of status of RCM:"))
{
RCMCombo2.addItem(RCMCombo);
selectionPanel.add(RCMCombo2);
}
The full version of the code is shown as below:
String [] RCM2 = {"Keep track of status of RCM:", "Add and activate RCM", "Remove RCM",
"Display the usage statistics for RCM",
"Update capabilities of RCMs", "Show RCM used most frequently in the last n days",
"Display number of times the RCM was emptied in n hours"};
RCMCombo2 = new JComboBox(RCM2);
RCMCombo2.addItemListener(itemListener);
RCMCombo2.setEditable(false);
RCMCombo2.setBounds(10,10,10,30);
//"Updates capabilities of RCM"); Get the location of RCM");
selectionPanel.add(RCMCombo2);
ItemListener itemListener = new ItemListener()
{
public void itemStateChanged(ItemEvent itemEvent)
{
int state = itemEvent.getStateChange();
//System.out.println((state == ItemEvent.SELECTED) ? "Selected" : "Deselected");
//System.out.println("Item: " + itemEvent.getItem());
if (state == ItemEvent.SELECTED)
{
ItemSelectable itemS = itemEvent.getItemSelectable();
String cmd = selectedString(itemS);
if ( cmd.equals("Keep track of status of RCM:"))
{
RCMCombo2.addItem(RCMCombo2);
selectionPanel.add(RCMCombo2);
}
Any help will be appreciated. Thank you.
It looks like you are on a completely wrong track. I think you are mixing the usage of ItemListener with the setup of the lists model.
Check out this guide to ComboBox - it should contain all the information you need.
Related
I have these two methods:
private void bcsButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
DefaultListModel model = new DefaultListModel<>();
for (Subject s : cCores) {
coreSubjectsList.setModel(model);
model.addElement(bcs.displayCores(s));
}
record1 = new Record(bcs.getCourseName());
ArrayList cores = new ArrayList<>(Arrays.asList(cCores));
record1.enrolCores(cores);
DefaultListModel model2 = new DefaultListModel<>();
for (Major m : BCSMajors) {
majorsList.setModel(model2);
model2.addElement(bcs.displayMajors(m));
}
DefaultListModel model3 = new DefaultListModel<>();
for (Subject s : cEles) {
this.electivesList.setModel(model3);
model3.addElement(bcs.displayElectives(s));
}
}
private void enrolButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String text = this.studNumberField.getText();
int studentNumber = Integer.parseInt(text);
String selection = group.getSelection().getActionCommand();
Student stud1 = new UnderGraduate(this.studentNameTextField.getText(), studentNumber, selection, this.dobField.getText(), "");
if (stud1 instanceof UnderGraduate) {
((UnderGraduate) stud1).writeFileUnderGrad();
record1.setStatus(Record.Status.ACTIVE);
record1.writeRecordFile();
}
}
For the enrol button, I want to make a case that if the bcsButton is chosen, and I click enrol, it will execute what is inside the enrol method. Because later on I want to make another button called mcsButton, and make two cases, that if mcsButton is chosen it will perform "the enrol code for mcs", and if bcsButton is chosen, it will perform "the enrol code for bcs". Is there any particular way I can do this? Thank you.I've tried using something like:
if(evt.getSource() == bcsButton){
//put my code here
}
It doesn't run anything though.
and make two cases, that if mcsButton is chosen it will perform "the enrol code for mcs", and if bcsButton is chosen, it will perform "the enrol code for bcs".
You would generally use a JRadioButton for this. The user can only select one of two buttons that has been added to the ButtonGroup.
Then in your "Enroll" button you check to see which button has been selected and execute the appropriate logic. The ActionListener code would be something like:
if (mcsButton.isSelected())
processMCSEnrol();
else if (bcsButton.isSelected())
processBCSEnrol();
Read the section from the Swing tutorial on How to Use Radio Buttons for more information and working examples.
Edit:
You need a variable lets say, mcsSelected.
In the ActionListener for the "mcsButton" you add:
mcsSelected = true;
In the ActionListener for the "bcsButton" you add:
mcsSelected = false;
In the "Enrol" ActionListener you add:
if (mcsSeleced)
processMCS();
else
processBCS();
I have a treeView in my wizard application. Whenever i add or remove an object from my model , calling the update or refresh methods works as expected. What i want is , when i press a certain check button, two things may happen: If the new selection is false(unchecked) , i want to remove the treeView items , so they wont show in my UI, and when the new selection is true(checked) , i want to add the previously removed items (i have them stored in my application), so they can show up again. So i added this listener to my button :
oIsAuthorizableResourceButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
bIsResourceAuthorizable[intSelectedResourceIndex] = oIsAuthorizableResourceButton.getSelection();
//recursiveSetEnabled(grpPolicySetConfiguration,oIsAuthorizableResourceButton.getSelection());
if(!bIsResourceAuthorizable[intSelectedResourceIndex]){
System.out.println("Remove : " + oSelectedPolicySet.getHasResourceAccessPolicy().size());
oTreeViewer.remove(oSelectedPolicySet.getHasResourceAccessPolicy().toArray());
oTreeViewer.refresh(oSelectedPolicySet);
oTreeViewer.refresh();
}else{
System.out.println("Add : " + oSelectedPolicySet.getHasResourceAccessPolicy().size());
oTreeViewer.add(oSelectedPolicySet, oSelectedPolicySet.getHasResourceAccessPolicy().toArray());
oTreeViewer.refresh(oSelectedPolicySet);
oTreeViewer.refresh();
}
}
Well this code does absolutely nothing.Any help appreciated.
Implement a Viewer Filter, then add and remove it from your viewer: http://www.eclipse.org/articles/Article-TreeViewer/TreeViewerArticle.htm . What it filters, and whether it even cares about a specific property, is your choice.
I have a GWT DataGrid with a multi-selection model and check-boxes to show selection/select/deselect rows. That's all well and good.
But, I also want to have a second, independent selection model. If a user double-clicks on a row, I want to handle that event, and for the event handler to know which row was double-clicked. The double-clicking should not affect the check-box selection.
I tried this:
final SelectionModel<MyRecord> selectionModel = new MultiSelectionModel...
//Yes I need a MultiSelectionModel
dataGrid.addDomHandler(new DoubleClickHandler() {
public void onDoubleClick(DoubleClickEvent event) {
selectionModel.get??? //no suitable getter for double-clicked
}
}, DoubleClickEvent.getType());
But ran into a dead-end when I found now way to get the double-clicked row in the event handler. One way would be to register both a Multi- and Single- selection model, but doubt DataGrid will support that.
Neither can I work out how to get the clicked row from the DoubleClickEvent object.
I have implemented a button cell with a FieldUpdater. This works, but it's not ideal.
Any suggestions?
If I understand correctly you want to get the index of the row.
You could do it like this: (this way you'll get the "Real" index)
AbstractSelectionModel<T> selectionModel = (AbstractSelectionModel<T>)dataGrid.getSelectionModel();
ArrayList<Integer> intList = new ArrayList<Integer>();
List<Row> list = (List<Row>)_dataProvider.getList();
int i = 0;
for(Row row : list)
{
if( selectionModel.isSelected(row) )
intList.add(i);
i++;
}
UPDATE:
To get only the current row you could do that:
datagrid.getKeyboardSelectedRow()
I'm 3 years too late to the party, but I think the more correct solution would be:
dataGrid.addCellPreviewHandler(new CellPreviewEvent.Handler<YOUR_MODEL_TYPE>() {
#Override
public void onCellPreview(final CellPreviewEvent<YOUR_MODEL_TYPE> event) {
if (BrowserEvents.DBLCLICK.equalsIgnoreCase(event.getNativeEvent().getType())) {
int row = event.getIndex();
doStuff(row); // do whatever you need the row index for
}
}
});
I m working with GWT 2.4 on an new application. I made a docklayoutpanel and I inserted a celllist on the west section of it. I need to create an event, every time a user clicks on an element of celllist on the west side of page a specific widget will load at the content of the docklayoutpanel.
Any suggestions?
Thank you
The following example should be self explanatory
// Create a cell to render each value.
TextCell textCell = new TextCell();
// Create a CellList that uses the cell.
CellList<String> cellList = new CellList<String>(textCell);
cellList.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
// Add a selection model to handle user selection.
final SingleSelectionModel<String> selectionModel = new SingleSelectionModel<String>();
cellList.setSelectionModel(selectionModel);
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
public void onSelectionChange(SelectionChangeEvent event) {
String selected = selectionModel.getSelectedObject();
if (selected != null) {
Window.alert("You selected: " + selected);
}
}
});
Instead of Window.alert("You selected: " + selected); you will need to change the widget shown on the eastern side of your panel.
This can be done in several ways, one of which is to expose the Dockpanel to the Selection Change Event either by declaring the Panel as a field to the class (not a local Variable in the constructor of the class) or as a final local variable in the constructor.
Another way is to do this by event handling. The eventBus methodology on the MVP design pattern is the proper way to do all thesee here for more information.
I have several components, all inheriting JCheckBox without overwriting anything from it, stored in a vector, which is then traversed and each of the components is added to a dialog
CreateLists(); // initialises the checkbox vector
for(int i = 0; i < checkBoxes.size() ; i++){
myPanel.add(checkBoxes.elementAt(i));
}
Some of these checkboxes are already selected.
My problem now is: When I open the dialog and select or unselect any checkbox, the value of the checkboxes in the vector doesn't change. Selected stays selected and unselected stays unselected.
I also tryed to get the new values by using JPanels getCompoents(), but the values of these are wrong, too.
An ItemListener in the checkbox inheritors confirmes that the changes do happen, but whenever I try to get the new values, there just the same as those with which the checkboxes were initialised.
Here is a console output I used to keep track of the changes:
create lists
print values:
checkBox1 = true
checkBox2 = true
checkBox3 = false
checkBox2 clicked new value = false
checkBox3 clicked new value = true
print values:
checkBox1 = true
checkBox2 = true
checkBox3 = false
Here is some more code and information:
CreateList() compares a watchlist with the watchable things, creates the checkboxes accordingly (true = watched etc) and adds them to the new initalised vector.
To read the values i use this:
Component[] components = pnlGesamt.getComponents();
for(int i = 0; i < components.length; i++){
if(components[i] instanceof WLElementCheckBox){
WLElementCheckBox cb = (WLElementCheckBox) components[i];
System.out.println(cb.WlElement().Name() + " = " + cb.isSelected());
}
}
The JCheckBox inheritor:
private WatchListElement wlElement;
public WLElementCheckBox (WatchListElement wl, boolean selected)
{
super();
WlElement(wl);
setSelected(selected);
setText(wlElement.Name());
addItemListener(new MIL());
}
public WatchListElement WlElement ()
{
return wlElement;
}
public void WlElement (WatchListElement wlElement)
{
this.wlElement = wlElement;
}
public class MIL implements ItemListener{
public void itemStateChanged(ItemEvent arg0) {
System.out.println("Ckb " + wlElement.Name() +" geklickt Item neu = " + isSelected());
}
}
There is a small possibility that the changes to your checkboxes are not visible because you're querying them from another thread (such as main) instead of the Event Dispatch Thread (EDT). Swing is not thread-safe. When you check the checkboxes, the values are set on the EDT, but other threads might have the old state saved in memory and don't check to see if it's been updated. So, the first thing you need to do is ensure that when you're checking the states of your checkboxes, you're doing it from the EDT, using a call to SwingUtilities.invokeAndWait() or SwingUtilities.invokeLater().
For more information on why this happens, search for Thread Safety in Java.
(Also, don't be tempted to use synchronization to solve the problem or you might end up with a deadlock in your GUI).
One final pro tip: using new Java for-loop syntax makes code easier to read and write, as well as more efficient in some cases. Your code...
for(int i = 0; i < checkBoxes.size() ; i++){
myPanel.add(checkBoxes.elementAt(i));
}
... can turn into...
for(JCheckBox c : checkBoxes) {
myPanel.add(c);
}
This works on arrays and anything that is Iterable (which includes Collection).