Solved

Keeping track of what checkboxs are checked

Posted on 2004-04-10
12
227 Views
Last Modified: 2010-03-31
Hello everyone out there hope all is going well.

With the statement below, how would I be able to create a way of listing each checkbox checked as it is checked into something like a combo box so the user could keep track of the checkboxs they have checked. At the bottom of it would be 2 buttons 1 except other remove  the remove button would remove the checkboxs that were checked from the list. Example the third and fifth one they want to remove. They can highlight them as they go through the list and at the end they press the remove button and the order will be the new order.

I will also have to have a way of replacing the text in the combo box or whatever works with new text not the checkbox name.  How could this all be tighted together?


I have 50 checkboxs named (kp1-kp50) these checkboxs are in a seperate JPanel named KP'S, In another JPanel next to it is another 50 checkboxs named (dm1-dm50) and others with names (kk1-kk20) and so on.

0
Comment
Question by:Drop_of_Rain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10796361
A couple of JList examples that may help:
http://www.objects.com.au/java/examples/swing/CheckBoxList1.do
http://www.objects.com.au/java/examples/swing/CheckBoxList2.do

The same renderers can be used with a combo box if preferred.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 10796377
You would simply have to iterate over the checkboxes and save their state in another collection
0
 

Author Comment

by:Drop_of_Rain
ID: 10797530
I can see this is way over my head! I will have to break this up into many questions, each one with points.


If it is OK to do this, what I would like to do is put up all the questions here. You guys tell me which questions need to be answered first, then I will post all the different questions so they all have points.


Don't answer any of the questions unless you think it is the first. I want this to feel fair to everyone.

Question #1 How would the #1 Code below be modified so the CheckListRenderer (the list portion)  could  be in a seperate JPanel so it can go next to the checkboxs I have with the Remove buttom and OK button at the bottom?
(please provide code)


Question #2 How could the items in the list (CheckListRenderer) from the #1 Code below be able to be clicked so it highlights them, so the user could go through the list and not the checkbox to uncheck them.  (please provide code)


Question #3 How could the seperate list be created so it is linked to many different chechboxs?  (please provide code)
   

Question #4 How would all of the chechboxs that remain checked after the OK buttom is pressed go into an Arraylist that will be connected to the #2 Code below because this is how the checkboxs and audiofiles are linked together?


Code #1

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class CheckListExample2 extends JFrame {
 
  public CheckListExample2() {
    super("CheckList Example");
    String[] strs = {"swing", "home", "basic", "metal", "JList"};    
                                           
    final JList list = new JList( createData(strs) );
   
    list.setCellRenderer(new CheckListRenderer());
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setBorder(new EmptyBorder(0,4,0,0));
    list.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        int index = list.locationToIndex(e.getPoint());
        CheckableItem item = (CheckableItem)list.getModel().getElementAt(index);
        item.setSelected(! item.isSelected());
        Rectangle rect = list.getCellBounds(index, index);
        list.repaint(rect);
      }
    });  
    JScrollPane sp = new JScrollPane(list);  
     
    final JTextArea textArea = new JTextArea(3,10);
    JScrollPane textPanel = new JScrollPane(textArea);
    JButton printButton = new JButton("print");
    printButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        ListModel model = list.getModel();
        int n = model.getSize();
        for (int i=0;i<n;i++) {
          CheckableItem item = (CheckableItem)model.getElementAt(i);
          if (item.isSelected()) {
            textArea.append(item.toString());
            textArea.append(System.getProperty("line.separator"));
          }
        }
      }
    });
    JButton clearButton = new JButton("clear");
    clearButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        textArea.setText("");
      }
    });
    JPanel panel = new JPanel(new GridLayout(2,1));
    panel.add(printButton);
    panel.add(clearButton);
   
    getContentPane().add(sp,    BorderLayout.CENTER);
    getContentPane().add(panel, BorderLayout.EAST);
    getContentPane().add(textPanel, BorderLayout.SOUTH);
  }
 
  private CheckableItem[] createData(String[] strs) {
    int n = strs.length;
    CheckableItem[] items = new CheckableItem[n];
    for (int i=0;i<n;i++) {
      items[i] = new CheckableItem(strs[i]);
    }
    return items;
  }
 
  class CheckableItem {
    private String  str;
    private boolean isSelected;
   
    public CheckableItem(String str) {
      this.str = str;
      isSelected = false;
    }
   
    public void setSelected(boolean b) {
      isSelected = b;
    }
   
    public boolean isSelected() {
      return isSelected;
    }
   
    public String toString() {
      return str;
    }
  }
 
  class CheckListRenderer extends JCheckBox implements ListCellRenderer {
   
    public CheckListRenderer() {
      setBackground(UIManager.getColor("List.textBackground"));
      setForeground(UIManager.getColor("List.textForeground"));
    }

    public Component getListCellRendererComponent(JList list, Object value,
               int index, boolean isSelected, boolean hasFocus) {
      setEnabled(list.isEnabled());
      setSelected(((CheckableItem)value).isSelected());
      setFont(list.getFont());
      setText(value.toString());
      return this;
    }
  }

  public static void main(String args[]) {
    CheckListExample2 frame = new CheckListExample2();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {System.exit(0);}
    });
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}



Code #2

public class CheckBoxesToAudioFiles {
Map checkToAudio;

public CheckBoxesToAudioFiles {
checkToAudio = new HashMap();
}

public void mapCheckboxToAudioFile(JCheckbox cb, AudioFile af) {
checkToAudio.put(cb, af);
}

public AudioFile getAudioFileForCheckbox(JCheckbox cb) {
(AudioFile)return checkToAudio.get(cb);
}

}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Drop_of_Rain
ID: 10800015
The problen now is that I just changed everything to JButtons instead I hope this can still be done?
0
 
LVL 92

Expert Comment

by:objects
ID: 10802666
If you change to use AbsractButton, most of your code would support both JButton's and JCheckBox's.
0
 

Author Comment

by:Drop_of_Rain
ID: 10802714
I just did a search for AbsractButton and found nothing, I have no idea what to do you would have to help me here!
0
 

Author Comment

by:Drop_of_Rain
ID: 10802732
objects here is some button code I have if you want to help with your suggestion
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
 * @author Javatm
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class Kempos extends JPanel {
     JButton check;
     JLabel label;
     JPanel gridPanel;
     ArrayList myArrayList;
     /**
      *Constructor for the MyClass4 object
      */
     public Kempos() {

          myArrayList = new ArrayList();
          label = new JLabel();
          //Call it like this . . .
          gridPanel = new JPanel();
          gridPanel.setLayout(new GridLayout(0, 3));
          // Setting MY layout...
          setLayout(new BorderLayout());
          add(label, BorderLayout.NORTH);
          add(gridPanel, BorderLayout.CENTER);
          label.setText("           Kempos");
          label.setForeground(Color.blue);
          setBackground(Color.white);
          looprow : for (int row = 0; row < 20; row++) {
               for (int col = 0; col < 3; col++) {
                    int n = (20 * col + row + 1);
                    if (n > 50) {
                         break looprow;
                    }
                    check = new JButton("" + n);
                    //check = new JCheckBox("" + n);
                    myArrayList.add(check);
                    gridPanel.add(check);
                    check.addMouseListener(new ColorChanger(check));
                   
               }
          }
     }

         class ColorChanger extends MouseAdapter {

         JButton check;
          public ColorChanger(JButton check) {
          this.check = check;
          }

          public void mouseEntered(MouseEvent e) {
          // To change the color of the panel . . .
          check.setBackground(Color.green);  
          check.setForeground(Color.black);
          }

          public void mouseClicked(MouseEvent e) {
          check.setBackground(null);
          check.setForeground(Color.black);
          }
     }

     public static void main(String args[]) {

     JFrame x = new JFrame();
     x.getContentPane().add(new Kempos());
     x.setSize(400,300);
     x.setVisible(true);  
     }
     }
0
 
LVL 92

Expert Comment

by:objects
ID: 10802792
sorry meant AbstractButton
0
 
LVL 92

Assisted Solution

by:objects
objects earned 300 total points
ID: 10802798
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
 * @author Javatm
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class Kempos extends JPanel {
     AbstractButton check;
     JLabel label;
     JPanel gridPanel;
     ArrayList myArrayList;
     /**
      *Constructor for the MyClass4 object
      */
     public Kempos() {

          myArrayList = new ArrayList();
          label = new JLabel();
          //Call it like this . . .
          gridPanel = new JPanel();
          gridPanel.setLayout(new GridLayout(0, 3));
          // Setting MY layout...
          setLayout(new BorderLayout());
          add(label, BorderLayout.NORTH);
          add(gridPanel, BorderLayout.CENTER);
          label.setText("           Kempos");
          label.setForeground(Color.blue);
          setBackground(Color.white);
          looprow : for (int row = 0; row < 20; row++) {
               for (int col = 0; col < 3; col++) {
                    int n = (20 * col + row + 1);
                    if (n > 50) {
                         break looprow;
                    }
                    check = new JButton("" + n);
                    //check = new JCheckBox("" + n);
                    myArrayList.add(check);
                    gridPanel.add(check);
                    check.addMouseListener(new ColorChanger(check));
                   
               }
          }
     }

         class ColorChanger extends MouseAdapter {

         AbstractButton check;
          public ColorChanger(JButton check) {
          this.check = check;
          }

          public void mouseEntered(MouseEvent e) {
          // To change the color of the panel . . .
          check.setBackground(Color.green);  
          check.setForeground(Color.black);
          }

          public void mouseClicked(MouseEvent e) {
          check.setBackground(null);
          check.setForeground(Color.black);
          }
     }

     public static void main(String args[]) {

     JFrame x = new JFrame();
     x.getContentPane().add(new Kempos());
     x.setSize(400,300);
     x.setVisible(true);  
     }
     }
0
 

Author Comment

by:Drop_of_Rain
ID: 10803382
objects  check.addMouseListener(new ColorChanger(check)); this is not recognized for AbstractButton, I just changed them back to JButtons, its ok.

0
 
LVL 92

Expert Comment

by:objects
ID: 10803458
> public ColorChanger(JButton check) {

sorry missed one, you also need to change this to:

public ColorChanger(AbstractButton check) {
0
 

Author Comment

by:Drop_of_Rain
ID: 10803826
I needed to close this question.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jboss wildfly 10.1 10 623
junit example issue 2 59
Bitwise and to sum elements 2 54
listing all the respondents to a twitter feed - Java 5 49
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question