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 2 of 2

Thread: Connecting Two Different ActionListeners

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Connecting Two Different ActionListeners

    I have one Actionlistener that can change my text to Italic... And I have one action listener that can change my text to bold.. But I just wrote up these IF ELSE statements and they do NOT work... How can I get the textarea to know that BOTH check boxes have been selected and not just one?


    		jcBold.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				if(jcBold.isSelected()){
    				Font font1 = new Font("Serif", Font.BOLD, 14);
    				jtaOutput.setFont(font1);
    				}else if (jcBold.isSelected() && jcItalic.isSelected()){
     
    				Font font2 = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
    				jtaOutput.setFont(font2);
    				}else {
    					Font font1 = new Font("Times Roman", Font.PLAIN, 12);
    					jtaOutput.setFont(font1);
     
    				}
    			}
    		});
    		panel_1.add(jcBold);
     
    		jcItalic = new JCheckBox("Italic");
    		jcItalic.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				if(jcItalic.isSelected()){
    				Font font1 = new Font("Serif", Font.ITALIC, 14);
    				jtaOutput.setFont(font1);
    				}else if (jcBold.isSelected() && jcItalic.isSelected()){
    				System.out.println("1");
    				Font font2 = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
    				jtaOutput.setFont(font2);
    				}else {
    					Font font1 = new Font("Times Roman", Font.PLAIN, 12);
    					jtaOutput.setFont(font1);
     
    				}
    			}
    		});


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Connecting Two Different ActionListeners

    Let both actionListeners call one method that determines the current selection of the checkboxes and sets the new font.
    int state = Font.PLAIN;
    if(jcItalic.isSelected()){
      state += Font.ITALIC;
    }
    if(jcBold.isSelected()){
      state += Font.BOLD;
    }
    Font font = new Font("Serif", state, size);

  3. The Following User Says Thank You to PhHein For This Useful Post:

    steme (April 26th, 2013)

Similar Threads

  1. Using while-loops in Actionlisteners - WindowBuilder
    By EatMyBible in forum Loops & Control Statements
    Replies: 6
    Last Post: February 15th, 2013, 10:25 AM
  2. How to use multiple actionlisteners
    By pottsiex5 in forum AWT / Java Swing
    Replies: 9
    Last Post: November 19th, 2011, 09:37 AM
  3. Connecting to VPN
    By raviteja84 in forum Java Networking
    Replies: 0
    Last Post: October 18th, 2011, 05:53 PM
  4. [SOLVED] Quick question regarding ActionListeners
    By Stockholm Syndrome in forum Java Theory & Questions
    Replies: 2
    Last Post: October 4th, 2011, 12:02 PM
  5. [SOLVED] NullPointerException on ActionListeners
    By cherryduck in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 7th, 2010, 04:17 PM