Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By camickr

Thread: How to implement JCheckBox inside JButton?

  1. #1
    s1w
    s1w is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    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:

    How to implement JCheckBox inside JButton?-jbuttonjcheckbox.jpg

    Java Code:
      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
    Java Code:
      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
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: How to implement JCheckBox inside JButton?

    You have a class that implements Icon but has an empty paintIcon(...) method. Doesn't make sense, that.

    Describe the user interaction with your custom composite component. What happens on button click? What determines whether the check box will be selected / unselected?

    Without knowing that, I can't make any design suggestion.

    db

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: How to implement JCheckBox inside JButton?

    Not sure I understand the usage of this component either, but maybe Component Border Java Tips Weblog will work for you.
    DarrylBurke likes this.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: How to implement JCheckBox inside JButton?

    Here's one interpretation, assuming the checked status is separately set externally.
    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class CheckButtonDemo {
    
      public static void main(String[] args) throws Exception {
        SwingUtilities.invokeLater(new Runnable() {
    
          @Override
          public void run() {
            new CheckButtonDemo().makeUI();
          }
        });
      }
    
      public void makeUI() {
        final CheckButton checkButton = new CheckButton("TSP ROUTE");
    
        JFrame frame = new JFrame();
        frame.setLayout(new FlowLayout());
        frame.add(checkButton);
        frame.pack();
    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    
        new Timer(750, new ActionListener() {
    
          @Override
          public void actionPerformed(ActionEvent e) {
            checkButton.setChecked(!checkButton.isChecked());
          }
        }).start();
      }
    }
    
    class CheckButton extends JButton {
    
      private JCheckBox checkBox = new JCheckBox();
    
      public CheckButton(String text) {
        super(text);
        setHorizontalTextPosition(LEFT);
        setIcon(new Icon() {
    
          private Icon checkIcon = UIManager.getIcon("CheckBox.icon");
    
          @Override
          public void paintIcon(Component c, Graphics g, int x, int y) {
            checkIcon.paintIcon(checkBox, g, x, y);
          }
    
          @Override
          public int getIconWidth() {
            return checkIcon.getIconWidth();
          }
    
          @Override
          public int getIconHeight() {
            return checkIcon.getIconHeight();
          }
        });
      }
    
      public void setChecked(boolean checked) {
        checkBox.setSelected(checked);
        repaint();
      }
    
      public boolean isChecked() {
        return checkBox.isSelected();
      }
    }
    db

  5. #5
    s1w
    s1w is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default Re: How to implement JCheckBox inside JButton?

    DarrylBruke, your answer source is greatly valuable for my experience. In addition, I had received solution unbelivable trivial from other forum...

    Java Code:
    JCheckBox jcb = new JCheckBox();
    button.add(jcb);
    Thank You guys, advanced handling Icon solution teached me something as well. Good day!

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: How to implement JCheckBox inside JButton?

    You still haven't answered this:
    Describe the user interaction with your custom composite component. What happens on button click? What determines whether the check box will be selected / unselected?
    db

  7. #7
    s1w
    s1w is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default Re: How to implement JCheckBox inside JButton?

    Its about using Travelling Salesman Algorithm etc; there is limited space on toolbar, and checkbox role is, when its unselected, route is not closing at final connection/in kruskal's or prim's version last connection is being made as addition, which in some cases of application needs is not compulsory
    sorry for my english if its not perfect

    edit:
    while ive learned that all components in java are containers, simple adding jchkbox to jbutton makes both accessible by user, and works great. Thanks again for Your inquiry
    Last edited by s1w; 09-11-2011 at 12:46 AM.

Similar Threads

  1. Jbutton inside a jtable wont fire action
    By giskard in forum AWT / Swing
    Replies: 5
    Last Post: 08-02-2011, 09:07 PM
  2. Replies: 9
    Last Post: 04-12-2011, 08:26 PM
  3. How can I add JCheckBox to each row?
    By batya in forum AWT / Swing
    Replies: 1
    Last Post: 11-04-2009, 10:25 PM
  4. How To Add A Jbutton Inside A Rectangle
    By SANDY_INDIA in forum AWT / Swing
    Replies: 9
    Last Post: 07-06-2008, 09:06 AM
  5. Replies: 1
    Last Post: 08-08-2007, 02:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •