List with checkbox using LWUIT - java

I am using LWUIT for getting a search facility for selection in the List.
Now I want to know how can I display the list with CheckBoxes?
list=new List(vector);
cform.addComponent(list);
cform.addComponent(t);
cform.show();

I don't know if there is a more simple solution then mine, but mine is highly customizable and can serve for a lot of purposes.
List l = new List;
Vector v = new Vector();
for(int i = 0; i < 10; ++i){
v.addElement(new CheckItem("itemtekst"));
}
l.setListCellRenderer(new CheckItemRenderer());
l.setModel(new CheckItemModel(v));
the code above makes it work. As you can guess you have to make a new class and override two to make it work.
CHECKITEM: this class has a string and an image. as well as setters and getters. it also has a boolean that shows if it is checked or not.
CHECKITEMRENDERER: has a label for the string and the image of the checkitem it extends Container and implements ListCellRenderer
CHECKITEMMODEL: this extends the defaultlistmodel. it has methods to get the checkeditems and setthem checked or unchecked.
to recap:
set the correct items in the vector
set the correct renderer
set the correct model
and to use it add an actionlistener or it will al be for nothing.

Related

Updating a LinkedList

My case is very specific here:
First, I have an already-defined array of Strings that contains the default options (which are always the same for all users) of a JComboBox:
private static final String[] JOB_TYPE = {options go here};
These options are loaded into the JComboBox as follows:
private JComboBox jobType = new JComboBox(JOB_TYPE);
Then, there are other options that can vary from one user to another that I have to load inside the JComboBox as well. Since arrays have a fixed number of elements, I had to find another way to add the specific options besides the default ones into the JComboBox. And so what I did was, I created a LinkedList and used the asList method from Arrays to load the default options of the array inside it, then add the other options which vary from one user to another:
private List<String> allJobs = new LinkedList<String>(Arrays.asList(JOB_TYPE));
allJobs can now be passed on as an argument for the JCombobox using the toArray() method:
private JComboBox jobType = new JComboBox(allJobs.toArray());
Now, I have all the default options in allJobs, and since it's no longer an array, I can also add to it the specific options which will be loaded as follows:
for (int j = 0; j < modelJobCustomType.getSize(); j++) {
allJobs.add(((XmlJobCustomElem) modelJobCustomType.getElementAt(j)).getName());
}
Now, here's my problem:
When I check the content of the JComboBox, I only find the default options, and not the specific ones even though I can see using the debugger that the list size has increased and it contains the specific options as well.
My guess is, since jobType, JOB_TYPE and allJobs are global variables, the ComboBox is being populated way before the compiler gets to the part of the code where it loads the specific options as well, and that's probably why I can only see the default options.
Is this correct? And if so, how can I fix this problem. Keep in mind that those global variables have to stay global because they are also being used in many other parts of the class.
Thanks for your help
The combobox is given the array derived from a list. Afterwards adding to the list will not change the array value (arrays are fixed length values),
There also is a JComboBox with a Vector parameter, that allows adding (as opposed to an array).
However the nicest is a JComboBox with a ComboBoxModel<E> parameter that is the most high-level. There is a default implementation DefaultComboBoxModel:
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(JOB_TYPE);
model.addElement("custom0");
model.addElement("custom1");
combobox = new JComboBox(model);
You can initialize your class like this to let you keep all the jobs in allJobs at initialization rather than the custom ones only existing in the JComboBox as in Jamie's solution. You could also do all the initializing of the instance fields inside the constructors:
class Main {
private static final List<String> FIXED_OPTIONS = Arrays.asList("fixed0", "fixed1", "fixed2");
private List<String> allJobs = new ArrayList<>(FIXED_OPTIONS);
{
int count = 6;
for (int i = 0; i < count; i++) {
allJobs.add("custom" + i);
}
}
private JComboBox jobType = new JComboBox(allJobs.toArray());
}
You can look at the source: JComboBox
181: /**
182: * Constructs JComboBox with specified list of items.
183: *
184: * #param itemArray array containing list of items for this JComboBox
185: */
186: public JComboBox(Object[] itemArray)
187: {
188: this(new DefaultComboBoxModel(itemArray));
189:
190: if (itemArray.length > 0)
191: setSelectedIndex(0);
192: }
The constructor creates a new instance of the DefaultComboBoxModel.

How to add elements to an already created JList (Netbeans generated)

I am developing an application in Netbeans using Java and have been told to use the GUI creation features that Netbeans offer. Due to this I cannot edit the initComponents(); method to edit the creation of the JList and add a default list model to it.
I have tried creating a new JList but that resulted in an infinite loop. I haven't ever created controls through coding them myself, only by an IDE's GUI creation tools.
This is what I have currently:
private void formWindowActivated(java.awt.event.WindowEvent evt) {
//String to hold current patients data
String patientDetails;
//Take the arraylist from the model
ArrayList<IAccountStrategy> unapprovedPatients;
unapprovedPatients = model.getObservers();
//Create default list model to store the patients details
DefaultListModel<String> unapprovedPatientModel = new DefaultListModel<>();
IAccountStrategy xx;
//For loop to iterate through each element of unapprovedPatients
for(int i = 0; i < unapprovedPatients.size(); i++){
//get the current patients details and store them in a string variable
xx = unapprovedPatients.get(i);
patientDetails = xx.getAccountID() + xx.getUsername() + xx.getFirstname() + xx.getLastname();
//Add string variable to list model
unapprovedPatientModel.addElement(patientDetails);
}
//add list model to existing JList
listPatients.addElement(unapprovedPatientModel);
}
I would like to output all the elements from the list model into the actual JList and then let the user interact with the list itself.
Thanks in advance!
is it not possible to use the list as I want
You just wrote code to create the DefaultListModel.
So now all you need is to add:
list.setModel( unapprovedPatientModel );
so the JList can use the newly created model.
Although the problem with this code is that the code will be executed every time the window is activated.
But the point is that all you need to do is update the list using the setModel() method. How you do this in the IDE is up to you.

GWT getElementbyId to equivalent widget / Panel

I'm trying to build a dynamic web app in GWT, when widgets are added to the screen I remember their 'name' by setting the Widget.setId, when I want to replace part of the page, I can find the element in question via DOM.getElementById('name'), and its parent using DOM.getParentElement(), and then remove its children.
Now I have a com.google.gwt.dom.client.Element object (the parent). What I want to do is turn this back into a GWT object - in fact it'll be something derived from Panel, so I can add additional Widgets.
How do I go from the Element object back to a Panel object ?
I totally accept I could be going about this the wrong way, in which case is there a better way?
I think your approach to remove widgets from the DOM using DOM.getElementById('name') is not the proper one.
On your case (I am just figuring out what you do), I would keep Java Objects references instead of accessing to them using the DOM.
For instance:
HorizontalPanel panel = new HorizontalPanel();
Widget w = new Widget();
//We add the one widget to the panel
panel.add(w);
//One more widget added
w = new Widget();
panel.add(w);
//Now we remove all the widgets from the panel
for(int i = 0; i < panel.getWidgetCount(); i++){
panel.remove(panel.getWidget(i));
}
UPDATE
Based on your comments, I would propose the following solution.
I suppose that you are storing widgets on HorizontalPanel, just apply this solution to your concrete case.
I propose to use customized class which inherits from HorizontalPanel and add a Map there to store relationship between names and widgets.
public class MyHorizontalPanel extends HorizontalPanel {
private Map<String, Widget> widgetsMap;
public MyHorizontalPanel(){
super();
widgetsMap = new HashMap<String, Widget>();
}
//We use Map to store the relationship between widget and name
public void aadWidget(Widget w, String name){
this.add(w);
widgetsMap.put(name, w);
}
//When we want to delete and just have the name, we can search the key on the map.
//It is important to remove all references to the widget (panel and map)
public void removeWidget(String name){
this.remove(widgetsMap.get(name));
widgetsMap.remove(name);
}
}

Use a reference inside an ArrayList to call a method, and change the current state of the referenced object?

Maybe this just isn't possible but what I'm trying to do is change the visibility of multiple elements in a GUI at the same time using an ArrayList to reference to them sort of dynamically. The objects are created by themselves in another method.
Both oldScreen.setVisible(false); and oldScreen<1>.setVisible(false); statements cause errors. I had a hunch my idea wouldn't work out so well.
Here is basically what i have, any way i can achieve this?
private void initScreens() {
// I create some ArrayLists as "screensets" of sorts and put some GUI elements in there
ArrayList startScreen = new ArrayList();
ArrayList lostScreen = new ArrayList();
ArrayList playScreen = new ArrayList();
startScreen.add(startB);
startScreen.add(exitB);
lostScreen.add(yl1);
lostScreen.add(yl2);
lostScreen.add(yl3);
lostScreen.add(yl4);
lostScreen.add(yl5);
}
private void changeScreen(ArrayList oldScreen,ArrayList newScreen) {
// now i try to create a handy method to handle the length of the arrays itself, so if
i need to make changes to screens I just add them to there array. They are then easily
displayed, and hidden when told.
int os = oldScreen.size();
int ns = newScreen.size();
for (int i = os; i > 0; i--){
oldScreen<i>.setVisible(false);
oldScreen<1>.setVisible(false);
}
That's invalid syntax.
You're trying to write
oldScreen.get(i)
You should also use generics (ArrayList<Screen>) to avoid casting.

Swing ComboBox with the choice of "none of the below"

I have a perfectly normal ArrayList<MyObject> that I need to edit and pick an object from.
In the application window, I have a JComboBox to select the appropriate choice from the list. I'm writing an editor dialog for these objects, which just includes a JList of these objects and editor fields. It's easy enough to do; I'll just have a ListModel implementation of some kind. Stick the ArrayList in, access it through the usual fields. The stuff in the GUI list is 1:1 representation of the stuff in the actual list. Easy.
But the combo box in the main application window is giving me a bit of a headache, because I need a special value. Ideally, the first item in the list should be "(None)", and return a null.
Do I just need to write some sort of weird ComboBoxModel implementation for this, or is there an easier, already implemented way to do this? I'd definitely imagine this sort of situation has cropped up before.
Implementing your own ComboBoxModel should be quite easy.
Since this solution creates a new Vector from your ArrayList, changes to yourArrayList after creating Vector won't be visible in your JComboBox. If you need this, then you'll have to implement your own ComboBoxModel (see DefaultComboBoxModel implementation).
You would have to do this anyway, since there is no DefaultComboBoxModel constructor that takes a List.
class SpecialComboBoxModel extends DefaultComboBoxModel {
public final static String NULL_ELEMENT = "<None>";
public SpecialComboBoxModel(Vector v) {
super(v);
}
#Override
public int getSize() {
return super.getSize() + 1;
}
#Override
public Object getElementAt(int index) {
if( index == 0) {
return NULL_ELEMENT;
}
return super.getElementAt(index - 1);
}
}
ArrayList<String> yourArrayList = new ArrayList<String>();
yourArrayList.add("Value1");
yourArrayList.add("Value2");
Vector<String> v = new Vector<String>(yourArrayList);
dropdown.setModel(new SpecialComboBoxModel(v));
You might want to use a null-object. For example
public class MyObject {
public static final MyObject NULL_OBJECT = new MyObject();
..
}
and then in your ArrayList just call:
arrayList.add(0, MyObject.NULL_OBJECT);
You null-object should have all of its properties set to null (or to some reasonable defaults), and your toString() method (if you are using it), should return "(none)" if all the fields are null.

Categories