Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to implement JCheckBox inside JButton?

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to implement JCheckBox inside JButton?

    I was trying some solutions, and every of them failed. Until now, I had thought that anything is possible in Java. One of my solution, that don't makes compilation warrning and runs (but checkbox is missing anyway) is:

    jbuttonjcheckbox.jpg

      class myCheckBox extends JCheckBox implements Icon {
          int w = 20;
     
          public myCheckBox() {
            super();
            setSelected(true);
            setFocusPainted(false);
            setOpaque(false);
            setToolTipText(" close route ");
            addItemListener(new ItemListener() {
               public void itemStateChanged(ItemEvent e) {
                  if (e.getStateChange() == ItemEvent.SELECTED) { }
               }
            });
          }
     
          public void paintIcon(Component c, Graphics g, int x, int y) { }
     
          public int getIconWidth() { return w; }
          public int getIconHeight() { return w; }
      }

    then
      myCheckBox CB = new myCheckBox();
      button.setIcon(CB);

    Any tryings of creating own button class with own content manager fails as well. Any ideas to solve this?


  2. #2
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to implement JCheckBox inside JButton?

    Unbelivable easy solution! Thanks.

    JCheckBox jcb = new JCheckBox();
    button.add(jcb);

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    32
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to implement JCheckBox inside JButton?

    Quote Originally Posted by s1w View Post
    Unbelivable easy solution! Thanks.

    JCheckBox jcb = new JCheckBox();
    button.add(jcb);
    Great!!! I have tried it. Cannot believe it is possible

    java memory
    Last edited by namhm; December 4th, 2011 at 06:49 PM.

Similar Threads

  1. how to implement timed out event in java?!
    By migongotar in forum Java Theory & Questions
    Replies: 3
    Last Post: December 15th, 2018, 07:41 AM
  2. how to implement method to add new Student and display it
    By exose in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 10th, 2011, 11:39 AM
  3. Beginner programmer... no idea how to implement this properly
    By rkrajnov in forum Java Theory & Questions
    Replies: 1
    Last Post: January 31st, 2011, 08:02 AM
  4. want to implement security key feature in the software
    By javasabin in forum Member Introductions
    Replies: 2
    Last Post: January 8th, 2011, 01:52 AM
  5. Implement dragging a circle
    By jolly in forum AWT / Java Swing
    Replies: 0
    Last Post: August 19th, 2009, 02:48 PM