Get selected `JCheckBox` associated `id` on swing - java

Before I asked this question, I went through many examples, some of them are related, but did not answer my needs:
How to get the selected index of JCheckbox?
Java- Reading whether a checkbox is checked or not
I have a list as keyMessageList which includes id as Long and associated keyWord as String.
I displayed keyWord's name on my jpanel using following code:
for (KeyMessage obj : keyMessageList) {
checkBox = new JCheckBox(obj.getSmsKey());
displayKeywordPanel.checkBooxPanel.add(checkBox);
}
I got the following output:
Now, My requirement is: If I select keywords, I need to get the id associated with the selected keyword. I have done similar things many times in web applications like this, but now I need to do the same thing on swing. I used the following code on web application to fulfill the requirements.
Code for web:
<h:selectOneMenu id="branch" value="#{createUser.branchId}" required="true" requiredMessage="Please choose branch">
<f:selectItems value="#{allBranch}"/>
</h:selectOneMenu>
Can any swing expert please help me.
Note I may select multiple checkBox and keyMessageList returns from JPA query.

I have a list as keyWordsList which includes id and associated keyWord.
Don't use a List. Instead use a Map with the "keyword" as the key and the "id" as the value. Then when you select a check box you get the ID from the Map.
Another option would be to create you JCheckBox as follows:
JCheckBox checkBox = new JCheckBox(keyword);
checkbox.setActionCommand( id );
Then later you access the id of the selected checkbox by using the getActionCommand() method.

You can get displayKeywordPanel.checkBooxPanel.getComponents() array. Iterate through the array and store indexes. Then use the indexes to get elements from the keyMessageList

I think you can extend your own JCheckBox (let say JCheckBoxWithID) which allow you keep the id. Then use a List to store/remove ids everytime a checkbox is selected/unselected using an ItemListener
This way you'll be avoiding iterate over your checkboxPanel asking who is selected and keeping separated responsibilities:
JCheckBox just shows a keyword and holds an id
List just stores selected ids
ItemListener handles the event when a JCheckBox becomes selected/unselected and adds/removes its id to/from List
Hope this example be helpful:
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Tests {
private void initGUI(){
/* This list will store selected ids */
final List<Integer> selectedIds = new ArrayList<>();
ItemListener itemListener = new ItemListener() {
#Override
public void itemStateChanged(ItemEvent e) {
if(e.getSource() instanceof JCheckBoxWithID){
JCheckBoxWithID checkBoxWithID = (JCheckBoxWithID) e.getSource();
if(checkBoxWithID.isSelected()){
selectedIds.add(checkBoxWithID.getId());
} else {
selectedIds.remove(checkBoxWithID.getId());
}
}
}
};
String[] keyWords = new String[]{"Help 1", "Help 2", "Help 3", "Help 4", "Help 5", "Help 6"};
Integer id = 0;
JPanel checkBoxesPanel = new JPanel(new GridLayout(3,3));
/* Add many checkbox as you keywords you have */
for(String keyWord : keyWords){
JCheckBoxWithID checkBoxWithID = new JCheckBoxWithID(keyWord, id);
checkBoxWithID.addItemListener(itemListener);
checkBoxesPanel.add(checkBoxWithID);
id++;
}
JButton submit = new JButton("Submit");
submit.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, Arrays.toString(selectedIds.toArray()), "Selected IDs", JOptionPane.INFORMATION_MESSAGE);
}
});
JPanel content = new JPanel(new FlowLayout(FlowLayout.LEADING));
content.add(checkBoxesPanel);
content.add(submit);
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(content);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Tests().initGUI();
}
});
}
class JCheckBoxWithID extends JCheckBox {
/* I use Integer but the id could be whatever you want, the concept is the same */
private Integer _id;
public JCheckBoxWithID(String text, Integer id) {
super(text);
_id = id;
}
public void setId(Integer id){
_id = id;
}
public Integer getId(){
return _id;
}
}
}
Here's a print screen:

Related

I wanted to add ItemListener for jList but i m unable to do so ,suggest me how can i add itemListener for listbox [duplicate]

I have a JList, where i am displaying some ID's. I want to capture the ID the user clicked and dis play it on a JLabel.
String selected = jlist.getSelectedItem().toString();
The above code gives me the selected JList value. But this code has to be placed inside a button event, where when i click the button it will get the JList value an assign it to the JLabel.
But, what i want to do is, as soon as the user clicks an item of the JList to update the JLabel in real time. (without having to click buttons to fire an action)
A simple example would be like below using listselectionlistener
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class JListDemo extends JFrame {
public JListDemo() {
setSize(new Dimension(300, 300));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
final JLabel label = new JLabel("Update");
String[] data = { "one", "two", "three", "four" };
final JList dataList = new JList(data);
dataList.addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent arg0) {
if (!arg0.getValueIsAdjusting()) {
label.setText(dataList.getSelectedValue().toString());
}
}
});
add(dataList);
add(label);
setVisible(true);
}
public static void main(String args[]) {
new JListDemo();
}
}
Why don't you put a ListSelectionListener on your JList, and add your above code in to it.
I'm assuming you already know how to create listeners on JButtons, based on your question, so you just need to tweak it to create a ListSelectionListener instead, then assign the listener to your JList using jlist.addListSelectionListener(myListener);
There is a nice tutorial here that should get you started, or refer to the documentation
You should be aiming for something like this...
jlist.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
if (!event.getValueIsAdjusting()){
JList source = (JList)event.getSource();
String selected = source.getSelectedValue().toString();
}
}
});
Use a ListSelectionListener:
JList list = new JList(...);
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent evt) {
if (!evt.getValueIsAdjusting()) {
// code here
}
}
});

Adding New Item to JComboBox Does Not Display

I have a JComboBox that is editable. When the user enters a new item, I want that added to the list and display it as the selected item. I am able to add it to the list but I cannot seem to make it display as the selected item. By default I display an empty string ("") which is what the user would edit to add the new item.
public class EventComboBoxListener implements ActionListener {
private JComboBox<String> eventBox=null;
public EventComboBoxListener(JComboBox<String> event_) {
eventBox=event_;
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Selected: " + eventBox.getSelectedItem());
System.out.println(", Position: " + eventBox.getSelectedIndex());
if (eventBox.getSelectedIndex() < 0) {
eventBox.addItem(eventBox.getSelectedItem().toString());
eventBox.setSelectedItem(eventBox.getSelectedItem().toString());
}
}
}
It doesn't make sense to me that I have to use setSelectedItem with the getSelectedItem. That it does not work is no surprise but I don't know what else to do. The newly added item shows up in the list as it should but how do I make it the selected item in the display at the same time? I can select it after but that should not be necessary.
Added MVCE:
Main
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
public class Test {
public static void main(String[] args) {
String[] list= {"","A","B","C"};
TestTableModel model=new TestTableModel(null,new String[] {"col1","col2"});
JTable table=new JTable(model);
JDialog dialog=new JDialog();
JScrollPane scroller=new JScrollPane(table);
JComboBox<String> box=new JComboBox<String>(list);
box.setEditable(true);
box.setSelectedIndex(0);
box.addActionListener(new EventComboBoxListener(box));
JTextField field=new JTextField();
field.setPreferredSize(new Dimension(75,30));
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setLayout(new FlowLayout());
dialog.setSize(new Dimension(400,100));
dialog.add(scroller);
dialog.pack();
dialog.setVisible(true);
table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(box));
table.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(field));
model.insertRow(0,new Object[] {"","placeholder"});
}
}
TestTableModel class
import javax.swing.table.DefaultTableModel;
public class TestTableModel extends DefaultTableModel {
/**
*
*/
private static final long serialVersionUID = 1L;
public TestTableModel(Object[][] data_,String[] columnNames_) {
super(data_,columnNames_);
}
}
First of all some comments about the MCVE (since you will be including one with every question in the future).
We expect the code so be in a single source file so we can easily copy/paste compile and test. We don't want 3 files lying around on our machine that we need clean up after testing.
Only relevant code directly related to the problem should be included. Why do you have the TestTableModel class. Are the "column names" relevant to the problem? The point is always test your MCVE using standard JDK classes when possible.
Regarding the EventComboListener class. Again, this can be added to the combo box by using and annoymouse inner class or a lambda. This keeps the code in a single class.
The newly added item shows up in the list as it should but how do I make it the selected item in the display at the same time?
I found that playing with your MCVE the ActionListener of the combo box is invoked at different times.
So my suggestion is to add the ActionListener to the editor of the combo box. Then we know for sure the ActionListener is only invoked when you press the Enter key. Once you press the Enter key the editor is stopped and the value is saved to the model.
So the logic would be something like:
//box.addActionListener(new EventComboBoxListener(box));
ComboBoxEditor editor = box.getEditor();
JTextField textField = (JTextField)editor.getEditorComponent();
textField.addActionListener( new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
String item = textField.getText();
DefaultComboBoxModel model = (DefaultComboBoxModel)box.getModel();
if (model.getIndexOf(item) == -1)
{
box.addItem(item);
box.setSelectedIndex( box.getItemCount() - 1 );
}
}
});
So the trick is to set the select index (not the selected item). But first the logic checks to make sure the item has not already been added to the combo box.

Populating a JComboBox with an ArrayList from another class

I am trying some self teachings and am working on a small app that reads details from a text file and shows them in a JComboBox for selection. The plan is to be able to select an item from the combobox, push the button and have a pop-up message appear with text depending on the selection, but I digress.
Currently, when it compiles the combobox is showing what I think to be some kind of exception. It says [Ljava.lang.Object;#175078b.
What am I doing wrong here?
This is the class that has the ArrayList:
import java.io.File;
import java.io.FileNotFoundException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Scanner;
public class Stuff {
private ArrayList<String> list;
private String name;
private ArrayList<String> getList() {
return list;
}
private void setList(ArrayList list) {
this.list = list;
}
public Stuff() {
readNames();
}
public void readNames() {
File file = new File("Names.txt");
try {
ArrayList<String> list = new ArrayList<>();
Scanner in = new Scanner(file);
while (in.hasNextLine()) {
list.add(in.nextLine());
}
Collections.sort(list);
// for (int i = 0; i < list.size();++i){
// System.out.println(list.get(i));
// }
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
This is the class with the GUI:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.ArrayList;
import javax.swing.*;
public class GUI extends JFrame{
private JLabel label = new JLabel("Who is the most awesome?");
private JComboBox box = new JComboBox();
private JFrame frame = new JFrame();
private JTextArea text = new JTextArea();
private JButton button = new JButton("Press Me");
private JPanel panel = new JPanel();
private ArrayList<Stuff> list = new ArrayList<>();
private JFrame getFrame(){
return frame;
}
private ArrayList<Stuff> getList(){
return list;
}
private void setList (ArrayList<Stuff> list){
list = list;
}
public GUI(){
buildGUI();
}
private void buildGUI(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setLayout(new FlowLayout());
panel.add(label);
panel.add(box);
panel.add(button);
box.addItem(list.toArray());
frame.add(text);
frame.add(panel, BorderLayout.CENTER);
frame.setSize(400,100);
frame.setVisible(true);
}
}
Your problem is in the line: box.addItem(list.toArray());
The addItem() method for a JComboBox requires an Object as a parameter. Typically, a String object is used as the parameter. Instead, what you are doing is trying to add the actual list reference to the combo box when you really meant to add every item in the list.
Instead, you should add the individual items in the list as follows:
for(Stuff stuff : list) {
box.addItem(stuff.getName());
}
EDIT: Reading your original worded problem again, I think your second code snippet regarding the GUI should not be using another ArrayList of Stuff if you actually intended to use the ArrayList in one instance of a Stuff object. Therefore, you should first change:
private ArrayList<Stuff> list = new ArrayList<>();
to
private Stuff = new Stuff();
and change the for loop to iterate through stuff.getList() instead of list. To clarify, the for loop might look like:
for(String detail : stuff.getList()) {
box.addItem(detail); // add each extracted detail from the text file that was stored in the list of the stuff object
}
The main problem is you're adding a single array to the combo box...
box.addItem(list.toArray());
You either need to build a ComboBoxModel around the list the list of items...
DefaultComboBoxModel model = new DefaultComboBoxModel(list.toArray(new Stuff[list.size()]));
box.setModel(model);
Or add each item from the list to the combo box
for (Stuff stuff : list) {
box.addItem(stuff);
}
See How to use combo boxes for more details
The responsibility for rendering values in JComboBox comes down to the ListCellRenderer.
Take a look at Providing a Custom Renderer for more details.
This approach allows you to custom how the object is rendered without having to modify the underlying object itself. It also means that you maintain the object relationship. Rather then having to provide some kind of mapping between the view and the original list of objects, you can simply get a direct reference of the object from the combo box, because the ListCellRenderer is performing the transformation for you.
Now, having said that, this will prevent the user from been able to use combo boxes built in search feature.
To that end, check Combo Box With Custom Renderer for a possible work around. This is authored by our very own camickr

Create different JTable while keeping generics

I have a working program using a Model composed by a list of Item.
public class Model {
private List<Item>;}
public abstract class Item{}
public class A extends Item{}
public class B extends Item{}
public class C extends Item{}
But now i need to create a view with some action buttons (add,del,edit) with a JTable on the center showing Specific Item (A,B,C).
Here is my first problem because each specific item will have a column for one of its field, so i need a different table for each item.
A solution could be to have a cardlayout with one table for each type of item, but it comes a new problem how can my view determine how many type of item there is in the model without using instanceof() ?
Moreover i will have others problems after this one, if i have x jtable in my view how will my view get the model of this table ? i can implements for each item an interface like that :
public interface MyModel{
AbstractTableModel getModel();
}
but i can only give to this function a List, so how each item will fill the data array with only its type of item?
PS : if i go further in my reflexion i have a bonus question, i'm wondering how the listener of my action button can simply know which JTable is currently being modify, should i put the listener in the view for simplicity of access to the cardlayout ?
If something is needed to improve my question, ask for it ! Not sure the question is currently clear.
EDIT : adding SSCCE, this is what i'm aiming but it currently doesn't use items of my program and doesn't implement the TableModel/TableModelListener.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
public class View extends JPanel implements TableModelListener{
private static final long serialVersionUID = 1L;
private JTabbedPane card;
public View() {
Object rowData[][] = { { "1", "one", "I" }, { "2", "two", "II" }, { "3", "three", "III" } };
String columnNames[] = { "#", "English", "Roman" };
Object rowData2[][] = { { "1", "B" } };
String columnNames2[] = { "#", "type" };
setLayout(new BorderLayout());
JPanel actionbutton = new JPanel();
JButton but = new JButton("fire");
but.addActionListener(new ButtonListener());
actionbutton.add(but);
add(actionbutton,BorderLayout.SOUTH);
card = new JTabbedPane();
//something should determine how many type of object in a List<Item> w/o using instanceof
//should fill jtable with a specific TableModel for each item type
JTable card1 = new JTable(rowData,columnNames);
JTable card2 = new JTable(rowData2,columnNames2);
card.addTab("Item A",new JScrollPane(card1));
card.addTab("Item B",new JScrollPane(card2));
add(card,BorderLayout.CENTER);
}
private class ButtonListener implements ActionListener{
#Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if ("fire".equals(cmd)) {
//do something on the model
}
}
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setSize(800, 600);
f.add(new View());
f.setVisible(true);
}
#Override
public void tableChanged(TableModelEvent e) {
//update jtable with the model's modification
}
}
TableModel supports generic types using Class Literals as Runtime-Type Tokens. Your implementation of getColumnClass() (below) defines the types available to the table's columns, and it defines the selection of any non-default renderers and editors. While DefaultTableModel offers considerable convenience, AbstractTableModel is only slightly more difficult and substantially more flexible. As a concrete example, this EnvTableTest illustrates a table modeled on a Map<String, String>.
#Override
public Class<?> getColumnClass(int columnIndex) {
// return a token for the specified column
}

Java - updating textFields from JList

I am making an address book GUI with Java and I have a JList that displays all the names of the people in my ArrayList (this is populated by the updateinfo method mentioned below). I want it so when I click an item on the JList, the TextFields are then updated with that persons details. Before I have only used buttons and therefore actionListeners. I think I understand that a JList must use ListSelectionListener but I cannot seem to implement this. I have added a snippet of my code below. Can somebody please help?? For continuity with my actionlisteners I would like to have it as an inner class but this is not vital
JList jl;
DefaultListModel list;
list = new DefaultListModel();
this.jl = new JList(this.list);
//add ListSelectionListener????
updateList();
this.add(this.jl, layout);
You can add the listener and then just query the currently selected index.
I did a sample for you, I hope you find it useful.
This is the relevant section:
private JComponent list() {
final JList list = new JList( data);
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
int i = list.getSelectedIndex();
nameTextField.setText( i >= 0 ? data.get( i ) : "" );
}
});
return new JScrollPane( list );
}
Bear in mind that's not the only way to go, this is just an starting point for you.
Here's the complete working sample:
import java.util.Vector;
import java.util.Arrays;
import java.awt.BorderLayout;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JComponent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
public class JListSample {
private Vector<String> data = new Vector<String>(
Arrays.asList( new String [] {
"one", "two", "three"
})
);
private JTextField nameTextField;
public static void main( String [] args) {
JListSample s = new JListSample();
s.run();
}
public void run() {
JFrame frame = new JFrame("Selection test");
frame.add( list(), BorderLayout.WEST );
frame.add( editPanel() );
frame.pack();
frame.setVisible( true );
}
private JComponent list() {
final JList list = new JList( data);
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
int i = list.getSelectedIndex();
nameTextField.setText( i >= 0 ? data.get( i ) : "" );
}
});
return new JScrollPane( list );
}
private JComponent editPanel() {
JPanel panel = new JPanel();
panel.add( new JLabel("Name:") );
nameTextField = new JTextField(10);
panel.add( nameTextField );
return panel;
}
}
This is what is displayed:
sample http://img177.imageshack.us/img177/6294/capturadepantalla200911k.png
You just add the selection listener to the list, like that:
jl.addSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
// evaluate e if necessary and call a method
// in your class to write the text in the textfield
int selectedRow = e.getFirstIndex(); // more complicate for multiselects
updateTextFieldWithName(selectedRow); // to be implemented
}
});
Using an anonymous class like here is the quickest way. It's a bit hard to read but a typical pattern.
(just read you preferred an inner class, but I can't code that here on the fly with no IDE at hand...)
Yes you will want to use a ListSelectionListener for this, you will also probably want to set the list to single selection(ListSelectionModel.SINGLE_SELECTION). This will allow the user to only select one item in the list. You can then add you listSelectionListener, and in the valueChanged of the event do something like the following(not exact syntax).
valueChanged(ListSelectionEvent e){
int idx = e.getFirstIndex();
int idx2 = e.getLastIndex(); //idx and idx2 should be the same if you set SingleSel
if(idx==idx2){
//here you can get the person detail however you have them stored. You can get them from the model like so,
Object personObj = MYLIST.getModel().getElementAt(int index);
}
}
I think I understand that a JList must
use ListSelectionListener but I cannot
seem to implement this
Well, then start by reading the JList API. You will find a link to the Swing tutorial on "How to Use Lists", which contains a working example.
Also in the tutorial you will find a section on "How to Write a List Selection Listener" which contains a second example.
Start with the tutorial for your basic questions.

Categories

Resources