How to link JcheckBoxes to JtextFields when they are being dynamically generated? - java

i want my app to create jcheckboxes from an input that always changes.
I want to create a jtextfield near every checkbox, that will be set enabled, only when his checkbox is pressed.
I managed to create this code:
//Create checkboxes with textfileds
for (int i = 0; i < activeProjects.length; i++) {
projectPanels[i] = new JCheckBox(activeProjects[i]);
projectPanels[i].setSelected(false);
projectPanels[i].setComponentOrientation (ComponentOrientation.RIGHT_TO_LEFT);
projectPanels[i].setAlignmentX(RIGHT_ALIGNMENT);
projectPanels[i].addItemListener(this);
projectStorageNum[i] = new JTextField("");
// projectStorageNum[i].setEnabled(false);
projectStorageNum[i].setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
projectStorageNum[i].setMaximumSize(new Dimension(200,30));
projectStorageNum[i].setMinimumSize(new Dimension(200,30));
projectStorageNum[i].setPreferredSize(new Dimension(200,30));
projectStorageNum[i].setAlignmentX(RIGHT_ALIGNMENT);
tmppnl = new JPanel();
tmppnl.add(projectStorageNum[i]);
tmppnl.add(projectPanels[i]);
checkBoxPanel.add(tmppnl);
}
and this is my state change listener:
public void itemStateChanged(ItemEvent e) {
Object source = e.getItemSelectable();
JCheckBox myBox= (JCheckBox)source;
String bName = myBox.getText();
if (e.getStateChange() == ItemEvent.SELECTED)
{
// enable matching text field.
// add bName to projects list.
}
else
{
//disable matching textfield
// remove bName from list
}
when I access the checkboxes in a dynamic way I don't have access to the second array of textfields.
is there any way to link them , or any other idea ?
thanks
Dave.

One thing you could do is use the setName and getName methods of Component to save the index of the JCheckBox.
projectPanels[i].setName(Integer.toString(i));
Then, in your state change listener.
int i = Integer.valueOf(e.getName());
This gives you the index of the JTextField.

You could use a Map (Hashmap) the checkbox would be the key and the textField the value returned when you do the key loopup.

Related

How to add MouseListener to a table model

I have a JTable. When a user clicks on a cell another JTable is created that shows the data for the whole row of that cell, in a column format (ie the row is converted to a column).
This happens when the user clicks but its a bit irritating to happen every time so I want to make it only on a double click.
The problem is that the getSelection method of the table only takes a addListSelectionListener method and not a MouseListener. How can I do what I want?
Here is the code:
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
int selectedRow = table.getSelectedRow();
DefaultTableModel newModel = new DefaultTableModel();
String rowName = "Row: " + selectedRow;
newModel.setColumnIdentifiers(new Object[]{rowName});
for (int i = 0; i < table.getModel().getColumnCount(); i++) {
newModel.addRow(new Object[]{table.getModel().getValueAt(selectedRow, i)});
}
JTable newTable = new JTable(newModel) {
/**
*
*/
private static final long serialVersionUID = 1L;
#Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(140, 240);
}
};
// Apply any custom renderers and editors
JOptionPane.showMessageDialog(frame, new JScrollPane(newTable),
rowName, JOptionPane.PLAIN_MESSAGE);
}
}
});
This happens when the user clicks but its a bit irritating to happen every time so I want to make it only on a double click
You use a MouseListener, not a ListSelectionListener. You would check the Mouse event for a click count of 2.
Read the section from the Swing tutorial on How to Write a MouseLister for more information and working examples.
Also, a double click will start the editor by default so you want to make sure the cell is not editable. So you may need to override the isCellEditable(...) method of the table.

JList NullPointerException in valueChanged operation

here is my first JList:
JPanel classe = new JPanel();
class_list = new JList();
class_list.addListSelectionListener(this);
JScrollPane classeScrollPane = new JScrollPane(class_list);
classeScrollPane.setPreferredSize(new Dimension(175,405));
classe.setBorder(new TitledBorder("Classes"));
classe.add(classeScrollPane);
here is the second:
JPanel relation = new JPanel();
relation_list = new JList();
relation_list.addListSelectionListener(this);
JScrollPane relationScrollPane = new JScrollPane(relation_list);
relationScrollPane.setPreferredSize(new Dimension(220,130));
relation.setBorder(new TitledBorder("Relations"));
relation.add(relationScrollPane);
here is my valueChanged operation:
public void valueChanged(ListSelectionEvent e){
JList source = (JList) e.getSource();
String value = (String)source.getSelectedValue();
if (value.length() > 0){
if (source == class_list){
//if(!relation_list.isSelectionEmpty()) relation_list.clearSelection();
Class_object co = declaration.getClass(value);
attribut_list.setListData(co.getAttributes().toArray());
operation_list.setListData(co.getOperations().toArray());
if(declaration.getGeneralization(value) !=null){
subClass_list.setListData(declaration.getGeneralization(value).getSubClasses().toArray());
}
//clear JList if there is no correspondent subclass
else subClass_list.setListData(new String[2]);
//add relation content to relation panel
DefaultListModel model = new DefaultListModel();
related_association = declaration.getAssociation(value);
for(Association asso : related_association){
model.addElement("(R)"+asso.getName());
}
related_aggregation = declaration.getAggregation(value);
for(Aggregation agg : related_aggregation){
model.addElement("(A)"+agg.getName());
}
relation_list.setModel(model);
}
if (source == relation_list){
int focused_list = source.getSelectedIndex();
if(focused_list < related_association.size()){
details.setText(related_association.get(focused_list).toString());
}
else {
details.setText(related_aggregation.get(focused_list-related_association.size()).toString());
}
}
}
}
Basically, when we click on item in the first JList, it displays in the second JList something (for example a variable name of this item). Then we click on item in the second JList, it displays more information about this item in a JTextArea. So you have to click first jlist to display second jlist then click second jlist to display detail information.
Here is the problem, after I have clicked first jlist and second jlist, I click another item in first JList, a NullPointerException occurs in line "
if (value.length() > 0){
I understand that the error occurs because the variable value is null, but I don't know why value is null.
Could anyone tell me whats wrong with my code. Thanks alot.
During a change in selection, the currently selected item is "deselected", this means that, momentarily, nothing is selected, the ListSelectionListeners are notified of this change, this is where your null value is coming from.
Then the selection is changed to the newly selected item and the ListSelecitonListeners are notified again.
You can test for this through the use of ListSelectionEvent#getValueIsAdjusting
The reason this occurs the second time you select a value is because the deselection event notification does not occur, only the selection event...

How can I setSelected for a ButtonGroup where all its JRadioButtons were created in a loop?

I've got the following for loop creating JRadioButtons for the ButtonGroup btgrpDiff for each of the values in my Difficulty enum:
private enum Difficulty {
EASY("Easy"),
MEDIUM("Medium"),
HARD("Hard"),
EXTRA_HARD("Extra Hard");
public final String name;
private Difficulty(String name) {
this.name = name;
}
}
for (Difficulty diff : Difficulty.values()) {
JRadioButton radDiff = new JRadioButton(diff.name);
radDiff.setActionCommand(diff.name);
btgrpDiff.add(radDiff);
panDiff.add(radDiff, "align 50%, shrink 2, wrap");
}
How can I setSelected for the ButtonGroup btgrpDiff outside of the for loop? I'd like to either set it straight after they're all selected or have a boolean parameter for each difficulty level in my enum to determine whether or not they get set as selected in the for loop, but I'm not sure which would be best, or how exactly to get the ButtonModel for the JRadioButtons.
Upon creation put your buttons in an ArrayList.
as class variable you should have
ArrayList<JRadioButton> mylist = new ArrayList<JRadioButton>()
So inside the loop do:
mylist.add(new JRadioButton(diff.name));
then you can access them anytime you want using mylist.get(i)
Or put them in a Map, then you can get them directly:
Map<Difficulty,JRadioButton> rButtons = new HashMap<Difficulty,JRadioButton>();
for (Difficulty diff : Difficulty.values()) {
JRadioButton radDiff = new JRadioButton(diff.name);
rButtons.put(diff,radDiff);
btgrpDiff.add(radDiff);
...
}

Get text from JRadioButton that isn't part of a ButtonGroup

My program is a GUI. I have this method where when a button is clicked. It populates the next screen with JRadioButtons dynamically.
private void setExamButtonActionPerformed(java.awt.event.ActionEvent evt)
{
if(evt.getActionCommand().equals("Set Exam"))
{
CardLayout cL = (CardLayout)cardPanels.getLayout();
cL.show(cardPanels, "setExamPanel");
}
try
{
//InputStream code
String theMessage = myObject.getMessage();
String delims = "(?=(0*([0-9]{1,2}|100)))";
String[] questions = theMessage.split(delims);
System.out.println(Arrays.toString(questions));
for (int j = 1; j < questions.length; j++)
{
settingQuestionBoxes = new JCheckBox(questions[j]);
settingQuestionTextField = new JTextField("");
jPanel1.add(settingQuestionBoxes);
jPanel1.add(settingQuestionTextField);
jPanel1.revalidate();
jPanel1.repaint();
}
//close streams and socket code
}
catch(Exception e)
{
System.out.println(e);
}
}
Then I have this other method from another screen where the data that is populated from the previous method goes to.
private void setExamQuestionButtonActionPerformed(java.awt.event.ActionEvent evt)
{
if(evt.getActionCommand().equals("Set Exam Question"))
{
ArrayList<JToggleButton> settingQuestionBoxes = new ArrayList<JToggleButton>();
for(JToggleButton questions: settingQuestionBoxes)
{
if(questions.isSelected())
{
System.out.println(questions.getActionCommand());
}
}
CardLayout cL = (CardLayout)cardPanels.getLayout();
cL.show(cardPanels, "instructorPanel");
}
}
So basically when i call this System.out.println(questions.getActionCommand()) I'm trying to see the text from the JRadiobutton that was clicked on.
Right now when I run the program and select a button. Nothing happens.
Put the buttons into a List<JToggleButton> such as an ArrayList<JToggleButton> and then iterate through the list when the information is needed.
for (JToggleButton btn : myButtonList) {
if (btn.isSelected() {
String actionCommand = btn.getActionCommand();
// use the actionCommand here
}
}
Note that JToggleButton is the parent class for JRadioButton and using it would allow you to add JRadioButtons, JCheckBoxes, and JToggleButtons to the list. Since your JRadioButton is not part of a ButtonGroup, perhaps you should be using a JCheckBox instead.
Edit
You now have posted this code, stating it doesn't work:
// Section (A)
ArrayList<JToggleButton> settingQuestionButton = new ArrayList<JToggleButton>();
// Section (B)
for(JToggleButton questions: settingQuestionButon)
{
if(questions.isSelected())
{
System.out.println(questions.getActionCommand());
}
}
Is this code, both (A) and (B), all together in your program? If so, it would make sense that it doesn't work. You should have (A) in a constructor or some set up method. You should follow (A) with code that creates your JRadioButtons or JCheckBoxes, that sets their actionCommand String, that places them in the GUI, and that adds them to the ArrayList.
The part (B) code, the enhanced for loop would need to be in code that is called in response to an event, perhaps in a JButton or radio button's ActionListener.
Please check out this information and fill us in on the details. Please consider creating and posting an sscce illustrating your problem for us.
Edit 2
Your code is confusing in that you appear to have two completely variables of different types with the exact same name, and you appear to be assuming that this will give the variable magical properties that will allow it to know what it's "twin" might be doing. Java doesn't work that way, and in fact variable names are not nearly all that important or smart to allow them any such functionality. Rather your code must be smart.
I'm assuming that more than one of your JCheckBoxes will be checked, and that you want to check which ones are checked at some point in your program. If so, then in your class you should have a List or ArrayList field, something like
private List<JToggleButton> questionsList = new ArrayList<JToggleButton>();
This way this field will available throughout the class.
Then where you create your JCheckBoxes, you add them to this list:
private void setExamButtonActionPerformed(java.awt.event.ActionEvent evt)
{
if(evt.getActionCommand().equals("Set Exam"))
{
CardLayout cL = (CardLayout)cardPanels.getLayout();
cL.show(cardPanels, "setExamPanel");
}
try
{
String theMessage = myObject.getMessage();
String delims = "(?=(0*([0-9]{1,2}|100)))";
String[] questions = theMessage.split(delims);
for (int j = 1; j < questions.length; j++)
{
settingQuestionBox = new JCheckBox(questions[j]); // *** renamed to make more sense
settingQuestionBox.setActionCommand(questions[j]); // **** add actionCommand String
questionsList.add(settingQuestionBox); // ****** add JCheckBox to List
settingQuestionTextField = new JTextField("");
jPanel1.add(settingQuestionBox);
jPanel1.add(settingQuestionTextField);
jPanel1.revalidate();
jPanel1.repaint();
}
//close streams and socket code
}
catch(Exception e)
{
// System.out.println(e);
e.printStackTrace(); // ***** more informative
}
}
Then elsewhere in your code
setExamQuestionButtonActionPerformed(java.awt.event.ActionEvent evt)
{
if(evt.getActionCommand().equals("Set Exam Question"))
{
// ArrayList<JToggleButton> settingQuestionBoxes = new ArrayList<JToggleButton>();
for(JToggleButton questions: questionsList)
{
if(questions.isSelected())
{
System.out.println(questions.getActionCommand());
}
}
CardLayout cL = (CardLayout)cardPanels.getLayout();
cL.show(cardPanels, "instructorPanel");
}
}
And of course you'll need to take care that the ActionListener is added to a button

Help implementing JComboBox[] Listener

1) In the following method (actionListener) a user select a grade (e.g. A-F) from a JComboBox.
2) There are multiple JComboBoxes, and each selection made gets stored into a single String[] array.
PROBLEM:
Here is the dilemma, if a user goes back and changes a selection made from a random JComboBox the previous grade selection does not get replaced in the array, however the new selection made gets stored at the next array index.
How can I make the program replace the previous grade selection and not just add the new selection?
relevant variables:
int counter;
private JComboBox[] gradeField;
//grade.userGrades[] is array of grades taken from selected combo boxes
Action Listener anonymous class:
gradeField[counter].addActionListener(new ActionListener () {
#Override
public void actionPerformed(ActionEvent e) {
Object holder = e.getSource();
JComboBox tempGradeBox = (JComboBox)holder;
String theGrade = (String)tempGradeBox.getSelectedItem();
grade.userGrades[grade.getNext()] = theGrade;
grade.updateNext();
}
});
Thanks in advance for any help.
I save the grade in an array and increment the index,
Well you should not be incrementing the index. This assumes that the user selects the grades from the combo box in a sequential order. As you have discovered users can often work randomly.
Instead you need to know which combo box has been changed and then update the appropriate entry in your array.
Or a different solution might be to update your array at the end. So maybe you have a "Process Results" button. Then you can sequentually loop through all the combo boxes to get the selected value.
Update the user grade being at the same index as the combo box:
final int index = counter;
gradeField[counter].addActionListener(new ActionListener () {
#Override
public void actionPerformed(ActionEvent e) {
Object holder = e.getSource();
JComboBox tempGradeBox = (JComboBox)holder;
String theGrade = (String)tempGradeBox.getSelectedItem();
grade.userGrades[index] = theGrade;
}
});
Here's another variation of JB Nizet's answer:
class OuterClass
{
...
gradeField[counter].addActionListener( new GradeSettingActionListener( counter ) );
...
class GradeSettingActionListener implements ActionListener
{
// -- Doesn't have to be final here (it does in JB's answer), but I like to be restrictive.
private final int index;
public GradeSettingActionListener( int index )
{
this.index = index;
}
#Override
public void actionPerformed( ActionEvent e )
{
Object holder = e.getSource();
JComboBox tempGradeBox = (JComboBox) holder;
String theGrade = (String) tempGradeBox.getSelectedItem();
grade.userGrades[index] = theGrade;
}
}
}
This approach removes the anonymous class by adding an inner class. The inner class will still have access to grade. You don't gain much here unless there's a chance you'll be splitting out the inner class later.
Of course, camickr's suggestion to process all the grades at once may also be valid, depending on other requirements (i.e., whether additional processing is done after the grades are stored in the array, which seems likely).

Categories