How to use EXT-GWT ComboBox - java

How do I use ComboBox in EXT-GWT with static data.
For example I just want to hard code (for demo purposes) list of First Names and display it to the user.
I don't want to use any dummy objects that they are using in their samples. Where can I find simple example with Strings?

Here is the code I use in my project:
SimpleComboBox combo = new SimpleComboBox();
combo.add("One");
combo.add("Two");
combo.add("Three");
combo.setSimpleValue("Two");

Maksim,
I am not sure whether it helps you or not. It was based on the GWT-EXT for combobox.
As I remember that, it wraps the String[] with SimpleStore object.
//create a Store using local array data
final Store store = new SimpleStore(new String[]{"abbr", "state", "nick"}, getStates());
store.load();
final ComboBox cb = new ComboBox();
cb.setForceSelection(true);
cb.setMinChars(1);
cb.setFieldLabel("State");
cb.setStore(store);
cb.setDisplayField("state");
cb.setMode(ComboBox.LOCAL);
cb.setTriggerAction(ComboBox.ALL);
cb.setEmptyText("Enter state");
cb.setLoadingText("Searching...");
cb.setTypeAhead(true);
cb.setSelectOnFocus(true);
cb.setWidth(200);
I hope it helps.
Tiger
ps) Did you try this example ?
// create store
ListStore<String> store = new ListStore<String>();
store.add( Arrays.asList( new String[]{"A","B","C"}));
ComboBox cb = new ComboBox();
cb.setStore(store);

Related

Adding items into tableview without using an instance of any class

I'm designing a project about cataloging something. In this project user must be able to create his own table as he wish. Therefore I do not have any static class and instance of it.
I'm creating a diaglog pane and I can create textfields for user inputs according to column names of database table dynamically but how can i add those user's inputs into the tableView ?
As I can add any String input into the ListView can I add user String inputs into tableView columns?
ListView<String> listView = new ListView();
public ObservableList<String> listCatalogNames = FXCollections.observableArrayList();
listCatalogNames.add("Books");
More details with an example;
There is listview that contains all catalog names and according to lisview selection tableview will be created dynamically center of borderpane.
User have books(name, author, page) and movies(name, year, director, genree) catalogs.
Lets say user selected movies and tableView appeared with 4 columns and clicked add button. Diaglog pane created with 4 textfield. I built everything until that point but I cannot add user's input into the tableView because i dont have any static class for Movies or Books etc.
Is there any way to create dynamic class ?
Please give me an idea and help me about that situation.
here is the github link of our project
Just use String[] storing the Strings for every column of a row (or a similar data structure) as item type for the TableView. It should be simple enough to create a String[] from the inputs and add it to this TableView:
static TableView<String[]> createTable(String... columnNames) {
TableView<String[]> table = new TableView<>();
for (int i = 0; i < columnNames.length; i++) {
final int index = i;
TableColumn<String[], String> column = new TableColumn<>(columnNames[i]);
column.setCellValueFactory(cd -> new SimpleStringProperty(cd.getValue()[index]));
table.getColumns().add(column);
}
return table;
}
Adding a String[] userInputs to a TableView<String[]> table would be done like this:
table.getItems().add(userInputs);
A similar issue (creating a TableView based on the metadata of a ResultSet) can be found here: How to fill up a TableView with database data
Easiest solution that comes to my mind is to make use of polymorphism. You can create a super class of both Book and Movie, let's call it Item. Then you can declare your table to contain Item and cast to one of the concrete classes when you need to.

How to add lists to SelectBox in libgdx

I have a selectbox in libgdx , I am trying to add a list of values to it . Is the a way i can add the list such as selectbox.addAll(list1) ?
Due to SelectBox reference the method you are looking for is
public void setItems(T... newItems)
You can use it for example like this:
SelectBox.SelectBoxStyle boxStyle = new SelectBox.SelectBoxStyle();
//creating boxStyle...
String[] values = new String[]{"value1", "value2", "value3", "value4"};
SelectBox<String> selectBox = new SelectBox<String>(boxStyle);
selectBox.setItems(values);
Of course instead of creating the style manually you can use Skin with predefined .json file for this purpose

java swing combobox

I have a combo box and a string array that holds all the values of the combo box in it. I erase the items from the combo box and then want to add in the values from the string array. It doesn't seem to let me just add in a string array. And I tried to itterate through the string adding items one by one but won't let me do that (or atleast the way I wrote it, it won't work).
May seem like a stupid question but I am new to working with swing in java.
Here's the code where I want to "reload" the items from the combo box:
String str = JOptionPane.showInputDialog(null, "Enter Name: ", "", 1);
if(str != null){
JOptionPane.showMessageDialog(null, "New name added: " + str, "", 1);
nameCreator.addName(strNames, str);
strNames = NameLoader.getNames();
nameList.removeAllItems();
nameList.addItem(strNames);
}
EDIT: Made small typo and didn't realize what was wrong. Working now. Thanks for everyones help.
Did you used the method addItem(Object anObject)?
You should iterate your array an use that method:
String[] data = {a;b;c;d;e}
for(int i=0; i < data.length; i++){
comboBox.addItem(data[i]);
}
Luca
I'd suggest you to implement your own ComboBoxModel:
public class YourComboBoxModel implements ComboBoxModel{
#Override
public Object getSelectedItem() {
//return selected item Object;
}
#Override
public void setSelectedItem(Object anItem) {
//set selected item
}
#Override
public Object getElementAt(int index) {
//return the element based on the index
}
#Override
public int getSize() {
//return the size of your combo box list
}
}
And build your JComboBox passing that model as parameter:
ComboBoxModel yourModel = new YourComboBoxModel();
JComboBox yourComboBox = new JComboBox(yourModel);
Using a custom ComboBoxModel is the most flexible solution. It allows you to change the datastructure holding your datas, and the way you access it, modifying only the model you implemented instead of other unrelated part of code.
Whenever you need to work with editable models for these kinds of GUI elements it is always good to use a model. For the JComboBox you have an easy-to-use DefaultComboBoxModel.
It works easily:
DefaultComboBoxModel model = new DefaultComboBoxModel(new String[]{"Item1","Item2","Item3"});
JComboBox comboBox = new JComboBox(model);
in this way you have the model attached to the combobox, and it will display items from the array. Whenever you need to change them just do:
model.removeAllElements(); // if you need to empty it
model.addElement("New Item1");
model.addElement("New Item2");
model.addElement("New Item3");
model.fireContentsChanged();
and you'll have new items updated inside the GUI.
A bonus note: if you need to manage custom objects instead that strings you can easily add them to the JComboBox (in the sameway showed before), you just need to provide a custom public String toString() method that will manage the string representation.
In your example I don't get why you readd all the items everytime, you could just call addItem with the new String without removing everything and adding them back.
the best way to add some thing in your combo box in order that you can change it easily is to describe an array list first,if you would like to be dynamic you can user a text field then in a text field you can run an array list .
textField = new JTextField();
textField.setBounds(131, 52, 86, 20);
contentPane.add(textField);
textField.setColumns(10);
then you have to create an array list
ArrayList al=new ArrayList();
then you have to equal your text field text to a string
String str=textfield.getText();
then add it to your array
al.add(str);
then add al item to your combo box.
JComboBox comboBox = new JComboBox();
comboBox.setBounds(112, 115, 145, 20);
contentPane.add(comboBox);
comboBox.addItem(al);

WEKA: how to disable the filter from labeled data?

I'm applying the following filter to my data.
string[] options = new string[2];
options[0] = "-R";
options[1] = "1-2";
Remove remove = new Remove(); // new instance of filter
remove.setOptions(options);
remove.setInputFormat(train);
train = Filter.useFilter(train, remove);
unlabeled = Filter.useFilter(unlabeled, remove);
However I would like to add the removed fields when I print the labeled data in the end.
How can I re-add the columns ?
Thanks
P.S>. One more question, can I just ignore fields instead of removing them ?
Assuming that you want to run a classifier on the data and ignore the attributes you've been removing, you want to use a FilteredClassifier with the Remove filter.
// Untested Java, I use Weka through JRuby
NaiveBayes naiveBayes = new NaiveBayes();
Remove remove = new Remove();
remove.setOptions(Utils.splitOptions("-R 1-2"));
FilteredClassifier model = new FilteredClassifier(naiveBayes, remove);
// Use model to classify as normal

Insert list of values to JTextField with Java2sAutoTextField

I want th auto generate values for the empId (or empName) when inserting values to the jtextfield
I went through the Java2sAutoTextField class but I can't add it in to a jframe.
Here's a part of code I extracted from here:
List possible = new ArrayList();
possible.add("Austria");
possible.add("Italy");
possible.add("Croatia");
possible.add("Hungary");
Java2sAutoTextField autoCompleter = new Java2sAutoTextField(possible);
i dont know how to put the above coding into the jtextfield
Also I tried out JSuggestField, but I can't add it from netbeans palette.
Any assistance is appreciated.
Done! i just had to implement the jtextfield as
List possible = new ArrayList();
possible.add("");
possible.add("Austria");
possible.add("Italy");
possible.add("Croatia");
possible.add("Hungary");
jTextField2 = new Java2sAutoTextField(possible);

Categories

Resources