how do i go do this, based on input in textfield, you get some results inside jlist, after you select option in jlist you then get an action, code examples would be appreciated... this is what i got so far:
final DefaultListModel<String> locations = new DefaultListModel<String>();
getTextFieldSearch().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for(int i=0;i<10;++i) {
locations.add(i, "blah");
}
}
});
JList<String> list_racentRaces = new JList<String>(locations);
Start by taking a look at How to Use Lists, which has lots of awesome code examples.
The basic idea would be to...
When your actionPerformed method is triggered, create a new DefaultListModel, assuming you don't have your own implementation, fill it with all the new items you need and apply it to the instance of list_racentRaces
If you want to maintain what was previously in the list, you should consider starting with a DefaultListModel and simply add the new items to it as you need to...
Then, attach a ListSelectionListener to list_racentRaces and when the valueChanged event is triggered, find the selected item(s) and do what ever you need to based on these result(s)
You can find more details and examples through How to Write a List Selection Listener
Related
I'm growing desperate here ...:
I do have a FRAME with 40 JComboBoxes. At first, they offer all the same items.
If I choose on item in one specific ComboBox, it should be selected there and not be available in all the other 39 Boxes anymore.
I tried to use a ComboBoxModel for a long while, but now I think it doesnt make any sense: the Moment i remove the selected item from the model, it gets removed from the Box that it got selected in, too.
So does it make sense to do it like this:
Have 40 MyComboBoxes in the class GUI
Every MyComboBox implements an ItemListener
If an item is selected, the item gets removed from the other 39 lists;
if its deselected, it gets added to the other 39 lists
(but if I want to do it like that, the listener mustnt be an own class but the
itemEventChanged-method must be implemented anonymously in the GUI?!)
There's a better way, isnt it? Thanks a lot for your help!
for(int x =0;x<YourComboBoxArray.length();x++
{
if(e.getSource()==YourComboBoxArray[x])
{
try
{
ArrayList <String> OptionsList = new ArrayList();
for(int i=0;i<YourComboBoxArray[x].getItemCount();i++)
{
OptionsList.add(TeamPlayercmbx[x].getItemAt(i).toString();
}
DefaultComboBoxModel DCMB = new DefaultComboBoxModel(OptionsList.toArray());
YourComboBoxArray[x+1].setModel(DCMB);
YourComboBoxArray[x+1].removeItem(YourComboBoxArray[x].getSelectedItem());
}
catch(Exception ex)
{
//Log your errors or whatever you want to do if it's the last ComboBox in the Array
}
}
}
This assumes you have an action listener on each index of the Combo Box Array
i am stuck with a new problem, don't know if this works but here i have list of JCombobox as follow.
JCombobox comboBox = new JComboBox();
comboBox.addItem("UserName");
comboBox.addItem("Password");
comboBox.addItem("DLNo 20 b");
comboBox.addItem("DLNo 20 b");
i want to print my database column names which are more than 40!
when i select the Combobox it must internally print my custom item here.
Here i tried with this code but i am not satisfied with this
if(comboBox.getSelectedIndex()==0)
{
System.out.println("U_NAME");
}
if(comboBox.getSelectedIndex()==1)
{
System.out.println("P_NAME");
}
if(comboBox.getSelectedIndex()==2)
{
System.out.println("DL_NO_20_b");
}
if(comboBox.getSelectedIndex()==3)
{
System.out.println("DL_NO_20_b");
}
is there any better way to over come this, like mapping objects
You could create a class ComboBoxItem with a name- and a columnName-attribute.
Use instances of this class for the ComboBox.
In the ComboBoxItem-class, overwrite the toString()-method to return the name, so it gets displayed as wished in the ComboBox. Add a getColumnName()-method to return the columnName, so you could invoke getSelectedItem().getColumnName().
This is my first post here, and I am very green with Java. This is something I'm trying to make to improve my java knowledge.
I have a button, which when clicked produces a shuffled card deck as a Jlist.
When pressed again, I would very much like it to refresh the JList, or recreate it somehow. Instead, it simply
creates a new list, so I now have 2 JLists.
button1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
cards.choseCards(); //Creates an ArrayList with the suffled cards
JList<String> displayList = new JList<>(cards.deck.toArray(new String[0]));
frame.add(displayList);
frame.pack();
cards.cleanUpDeck(); //Removes all the cards from the ArrayList
}
});
The key here is that Swing uses a model-view type of structure similar to model-view-controller (but with differences) where the model holds the data that the view (the component) displays.
What you are doing is creating an entirely new JList, but what you want to do is to update the model of the existing and displayed JList, either that or create a new model for this same existing JList. JLists use a ListModel for their mode, often implemented as a DefaultListModel object, and so you will want to update or replace this model such as by creating a new DefaultListModel object and then inserting it into the existing JList by calling its setModel(ListModel model) method.
For example your code could look something like this (made with lots of guesses since we don't know what your real code looks like):
button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// create new model for the JList
DefaultListModel<String> listModel = new DefaultListModel<>();
cards.choseCards(); //Creates an ArrayList with the suffled cards
// add the cards to the model. I have no idea what your deck field looks like
// so this is a wild guess.
for (Card card : cards.deck) {
listModel.addElement(card.toString()); // do you override toString() for Card? Hope so
}
// Guessing that your JList is in a field called displayList.
displayList.setModel(listModel); // pass the model in
cards.cleanUpDeck(); //Removes all the cards from the ArrayList
}
});
I have a Jlist which is populated with books, however, what I would like to do is that once one of the books is selected I press a button called return book that should make the book removed from the list.
I have a members class that has a return book method as follows
public void returnBook(Book aBook)
{
currentLoans.remove(aBook);
aBook.setBorrower(null);
}
On my main application I have the following code under the return book button
private void theButtonActionPerformed(java.awt.event.ActionEvent evt) {
//!!!Return book
DefaultListModel model = (DefaultListModel) BooksOnLoan.getModel();
Book selectedBook;
selectedBook = (Book)BooksOnLoan.getModel();
model.remove(selectedBook);
}
As you can see I am quite not sure how to remove the item from the list once the button is clicked.
The method "remove" from DefaultListModel works with index, so you first need to get the index of the element that you want to remove and provide that to the remove method. You can use methods on your list for that: getSelectedIndex method for single selection mode (you will get -1 if there is no selection), or getSelectedIndices for multiselect.
If in any case your list stays the same after this, you need to refresh GUI after the model has been changed. Although I am almost certain that you need not do that, but keep this principle in mind for future.
I am trying to search for UserName and return values onto jComboBox, here is the code
public void actionPerformed(java.awt.event.ActionEvent e) {
sr = new Search(((String) jComboBoxReceiver.getSelectedItem()));
usrList = sr.searchUser();
String[] userList = new String[usrList.size()] ;
for(int i=0;i<usrList.size();i++){
userList[i]= usrList.get(i).getUserName();
}
model = new DefaultComboBoxModel(userList);
jComboBoxReceiver.setModel(model);
}
after you click to somewhere else or click enter,it will conduct the search, however, it will go search for the first item again, which is very confusing... then i tried using key Pressed
if(e.getKeyCode()==13){
sr = new Search(((String) jComboBoxReceiver.getSelectedItem()));
usrList = sr.searchUser();
String[] userList = new String[usrList.size()] ;
for(int i=0;i<usrList.size();i++){
userList[i]= usrList.get(i).getUserName();
}
model = new DefaultComboBoxModel(userList);
jComboBoxReceiver.setModel(model);
}
And this one does not react at all.
You need to set the listener(s) on the Editor not the ComboBox itself. See the answer here:
Detecting when user presses enter in Java
Wow, you're rebuilding a ComboBoxModel each time ? Isn't it a little expensive ? You know there is a MutableComboBoxModel, also implemented by DefaultComboBoxModel that would allow you to add/remove elements from you combobox without rebuilding its model each time ?
Concerning your question, I don't understand the statement
However, if i do that, it does perform correctly, however, it will go search for the first item again
Do you mean your JComboBox starts to blink with content being modified each time ?
if so, maybe is it because your ActionListener is linked to JComboBox, which content changes continuously.
Anyway, i suggest you add some logs, like
sr = new Search(((String) jComboBoxReceiver.getSelectedItem()));
DefaultComboBoxModel model = (DefaultComboBoxModel) jComboBoxReceiver.getModel();
model.remvoeAllElements();
usrList = sr.searchUser();
String[] userList = new String[usrList.size()] ;
for(int i=0;i<usrList.size();i++){
String username = usrList.get(i).getUserName();
System.out.println(username); // feel free to instead use one loger
model.addElement(username);
}
Besides, i would tend to suggest you an other approach, in which combo box model don't contain simple Strings, but rather User objects, with a ListCellRenderer displaying only the user name.
IMO, what will really be confusing for your users is to have the content and selection of a combo box changed as soon as they select one of its options.
Anyway, if you really want to do that, then you should remove the action listener (or deactivate it) before changing its content, and re-add it (or reactivate it) after :
public void actionPerformed(java.awt.event.ActionEvent e) {
sr = new Search(((String) jComboBoxReceiver.getSelectedItem()));
usrList = sr.searchUser();
String[] userList = new String[usrList.size()] ;
for(int i=0;i<usrList.size();i++){
userList[i]= usrList.get(i).getUserName();
}
model = new DefaultComboBoxModel(userList);
jComboBoxReceiver.removeActionListener(this);
jComboBoxReceiver.setModel(model);
jComboBoxReceiver.addActionListener(this);
}