Insert list of values to JTextField with Java2sAutoTextField - java

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);

Related

javax.swing.JList<String> don't refresh with Apache Cassandra data

I'm trying to display data from a Apache Cassandra database. I have this code:
private void jButtonTimelineActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
List<Row> l = cassandra.queryTimeline();
JList<String> listaTweets= new JList<String>();
for(Row r : l) {
JLabel label = new JLabel(r.getString(3));
System.out.println(r.getString(3));
jList1.add(label);
}
jList1.revalidate();
jList1.repaint();
}
I have tried many things and the UI don't display the labels. I'm sure that the list of Rows is correct. What can I do?
You have post an unfinished code. its hard to to understand thses type of code. though i have something in my mind. hope this works.
DefaultListModel<String> model = new DefaultListModel<>();
JList<String> jList1 = new JList<>( model );
now add those items to "model" like this
model.addElement(lavel.getString());
no need to add these codes jList1.revalidate();
jList1.repaint();

Java: Get List<> Items from one class into JComboBox of another class

Here's the thing:
I created a 'cocktailbar' software, and I have the following classes:
Cocktail,
CocktailBar,
CreateNewCPanel,
HelloPanel,
SearchCPanel,
ShowAllCPanel,
CocktailMixerGUI,
Ingredients.
Now: When adding a new Cocktail in the CreateNewCPanel, I add the cocktail to a List in the CocktailBar class.
Box buttonBox = Box.createHorizontalBox();
JButton speicherButton = new JButton("Speichern");
speicherButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
neuerC.setCName(cName.getText());
neuerC.fuegeZubereitungHinzu(zubereitungTextArea.getText());
CocktailBar.addCocktail(neuerC);
Now I need to see all created cocktails in a 'dropdown' menu in the ShowAllCPanel. I've got the following:
//Adding the DropDown Menu, first a Box, then a ComboBox inside.
Box cDropDownBox = Box.createHorizontalBox();
cDropDownBox.add(Box.createHorizontalGlue());
JComboBox cChoose = new JComboBox();
groesseEinsetzen(cChoose, 500, 20);
cChoose.setAlignmentX(SwingConstants.LEFT);
cDropDownBox.add(cChoose);
But now I am wondering how do I get my List from the CocktailBar class into the ShowAllCPanel?
edit: forgot to mention: i have a getter in the CocktailBar class, and i already tried:
cChoose.addItem(CocktailBar.getCocktails());
within the comboBox in the ShowAllCPanel, but it doesnt show up anything in the dropdown.
thanks to #Do Re, i inserted this:
//Adding the DropDown Menu, first a Box, then a ComboBox inside.
Box cDropDownBox = Box.createHorizontalBox();
cDropDownBox.add(Box.createHorizontalGlue());
JComboBox cChoose = new JComboBox();
if (CocktailBar.getCocktails() != null){
for (Cocktail c : CocktailBar.getCocktails())
cChoose.addItem(c);
}
but still - when running, the dropdown list stays empty.
As you mentioned in the comments, you created a getter for the cocktails.
Try something like this
for (Coctail c : CocktailBar.getCocktails())
cChoose.addItem(c);
This iterates over the list of cocktails and adds each item seperately rather than adding a list of Cocktails at once.
Edit
try
cDropDownBox.revalidate();
cDropDownBox.repaint();
or
cChoose.revalidate();
cChoose.repaint();

how to make JDialog with checkboxes

I am writing a Java program and I ran into a problem. I have an ArrayList<JCheckBox> and I want to show some dialog window with these checkboxes, so I can choose some of them and I want another ArrayList<> of the selected objects as an outcome after closing that dialog. I think I can get results by adding ActionListener to those checkboxes, but I donĀ“t know how to pass that ArrayList<JCheckBox> to the dialog window..
So far I tried something like this:
ArrayList<JCheckBox> al = new ArrayList<JCheckBox>();
for (MyClass mc : sr.getFields().values())
{
JCheckBox box = new JCheckBox(mc.getType());
al.add(box);
}
JOptionPane.showConfirmDialog(null, al);
If I try to print the text in the checkbox, it is ok, but the dialog shows only a long line of some text that does not make any sense..
So, is there a way how to do that?
Thanks in advance..
The showConfirmDialog method has to interpret the message object in order to render it correctly and it doesn't know how to interpret an ArrayList, you have to add all your elements to an JPanel eg:
JPanel al = new JPanel();
for (MyClass mc : sr.getFields().values()){
JCheckBox box = new JCheckBox(mc.getType());
al.add(box);
}
JOptionPane.showConfirmDialog(null, al);
or an Object[] eg:
ArrayList<JCheckBox> al = new ArrayList<JCheckBox>();
for (MyClass mc : sr.getFields().values()){
JCheckBox box = new JCheckBox(mc.getType());
al.add(box);
}
Object[] obj = (Object[]) al.toArray(new Object[al.size()]);
JOptionPane.showConfirmDialog(ui, obj);

I have an ArrayList<JCheckBox> that i want to convert to an ArrayList<String>

I have an ArrayList<JCheckBox> that i want to convert to an ArrayList<String>
First I do like this. I get all my titles from a file and put them into a new ArrayList. Afterwords I make a new JCheckBox Array that contains all the Strings from the StringArray.
ArrayList<String> titler = new ArrayList<String>();
titler.addAll(FileToArray.getName());
ArrayList<JCheckBox> filmListe = new ArrayList<JCheckBox>();
for(String titel:titler){
filmListe.add(new JCheckBox(titel));
}
for(JCheckBox checkbox:filmListe){
CenterCenter.add(checkbox);
}
This is what I am trying to do: First I make a new ArrayList (still in the JCheckBox format), that contains alle the selected Checkboxes. Afterwords I want to add the to a new ArrayList in a String format.
The main problem is italicized (with **):
ArrayList<JCheckBox> selectedBoxes = new ArrayList<JCheckBox>();
for(JCheckBox checkbox: filmListe){
if (checkbox.isSelected()){
selectedBoxes.add(checkbox);
}
ArrayList<String> titlesArr = new ArrayList<String>();
for(JCheckBox titel:selectedBoxes){
*titlesArr.add(titel);*
}
A lot of code and text for a little problem! But I really appreciate your help! :)
You can't add a JCheckBox to a List<String>, the types JCheckBox and String are incompatibles.
I guess you want to add the text of the check box into your list, so you have to retrieve it manually, using:
titlesArr.add(titel.getText());
Assuming the checkbox's label is exactly the same as what you originally had in your list of titles, just use the checkbox's getText method (which gets the String label). You don't need to make a separate list of checkboxes that are checked - just put an if-block inside your first loop like this:
ArrayList<String> titlesArr = new ArrayList<String>(filmListe.size());
for (JCheckBox checkbox : filmListe) {
if (checkbox.isSelected()) {
titlesArr.add(checkbox.getText());
}
}
Try this:
ArrayList<String> titlesArr = new ArrayList<String>();
for(JCheckBox checkbox: filmListe)
if (checkbox.isSelected())
titlesArr.add(checkbox.getText());
Now titlesArr contains what you wanted.

How to use EXT-GWT ComboBox

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);

Categories

Resources