Results 1 to 4 of 4
  1. #1
    DaveMc is offline Member
    Join Date
    Dec 2012
    Location
    Kerry, Ireland
    Posts
    8
    Rep Power
    0

    Default JCheckBox Question

    Hey guys I'm coding a project and in it I have 2 JCheckBoxes and a JTextField. The idea is the Font in the TextField will change to bold or italic depending on which CheckBox is Selected. When I run it, however, the program crashes when I select the ItalicBox CheckBox and the textField text becomes a much smaller Font than before. The Code below is a snippet from the project. I am using NetBeans and running a Mac OS X. If you could look over the code maybe its something I've done stupidly. Any help as usual would be appreciated

    Java Code:
    public class JCheckBoxPanel extends JPanel {
    
        private JCheckBox boldBox;
        private JCheckBox italicBox;
        private JTextField textField;
    
        public JCheckBoxPanel() {
            setLayout(new BorderLayout());
    
            // Create Components
            boldBox = new JCheckBox("Bold");
            italicBox = new JCheckBox("Italic");
            textField = new JTextField("This is a  sentence");
    
            // Create a handler object for the item listeners
            HandlerClass handler = new HandlerClass();
    
            boldBox.addItemListener(handler);
            italicBox.addItemListener(handler);
    
            this.add(textField, BorderLayout.NORTH);
            this.add(boldBox, BorderLayout.EAST);
            this.add(italicBox, BorderLayout.WEST);
    
        }
    
        private class HandlerClass implements ItemListener {
    
            public void itemStateChanged(ItemEvent e) {
    
                Font font = null;
    
                if (boldBox.isSelected() && italicBox.isSelected()) {
                    font = new Font("Serif", 12, Font.BOLD + Font.ITALIC);
                    textField.setFont(font);
                } else if (boldBox.isSelected()) {
                    font = new Font("Serif", 12, Font.BOLD);
                    textField.setFont(font);
                } else if (italicBox.isSelected()) {
                    font = new Font("Serif", 12, Font.ITALIC);
                    textField.setFont(font);
                } else {
                    font = new Font("Serif", 12, Font.PLAIN);
                    textField.setFont(font);
                }
    
            }
        }
    }
    Last edited by DaveMc; 07-21-2013 at 01:00 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: JCheckBox Question

    Quote Originally Posted by DaveMc View Post
    ... the program crashes ...
    When seeking help with any error, always provide the error stack trace. Without seeing that, we could be guessing at the problem and wasting our time and yours.

    Read the API for java.awt.Font. Carefully. What are the parameters to the (String, int, int) constructor you use in your code?

    Also, since you haven't stated any problem with the checkboxes, this thread might have been better titled "Font problem"

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,513
    Blog Entries
    7
    Rep Power
    20

    Default Re: JCheckBox Question

    Look at the constructors for the Font class and see which one determines the size and which one determines the style of the Font.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    DaveMc is offline Member
    Join Date
    Dec 2012
    Location
    Kerry, Ireland
    Posts
    8
    Rep Power
    0

    Default Re: JCheckBox Question

    Looks like i messed up the constructor arguments. Thats embarrassing!! Thanks Darryl & JosAH.
    Last edited by DaveMc; 07-21-2013 at 01:39 PM.

Similar Threads

  1. Help with JCheckBox
    By cyentw in forum New To Java
    Replies: 2
    Last Post: 02-06-2013, 05:19 AM
  2. JCheckBox and JTextArea question
    By manalinik in forum New To Java
    Replies: 6
    Last Post: 01-04-2012, 04:59 PM
  3. JCheckBox help
    By lawrenceejgray in forum AWT / Swing
    Replies: 3
    Last Post: 11-12-2011, 04:38 PM
  4. JCheckBox
    By bczm8703 in forum JDBC
    Replies: 1
    Last Post: 10-23-2011, 05:30 PM
  5. How can I add JCheckBox to each row?
    By batya in forum AWT / Swing
    Replies: 1
    Last Post: 11-04-2009, 09:25 PM

Posting Permissions

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