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

Thread: JCheckBox and JTextArea question

  1. #1
    manalinik is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default JCheckBox and JTextArea question

    Hello mates, i just wonder if I can do this and write a little bit of code but I'm not able to do it so I'm here. I want to add (append) text to TextArea when checkbox is checked and removed this text when I uncheck it. But more than one checkbox will be there so if I checked one the text will be added, then when I checked another checkbox its text will be added after first ones in TextArea, then when I unchecked first one the first one's text will be removed, other one will be there. I hope I told it well.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class test {
    	public static void main(String[] args) {
    
    		JFrame table = new JFrame();
    		table.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		table.setLayout(new GridLayout(0, 2));
    
    		final String c = "Some text about something";
    
    		JPanel lTab = new JPanel();
    		lTab.setLayout(new GridLayout(2, 0));
    
    		final JTextArea ins = new JTextArea(5, 10);
    
    		JCheckBox lst = new JCheckBox("ChckBx1");
    		lst.addItemListener(new ItemListener() {
    
    			public void itemStateChanged(ItemEvent e) {
    				if (e.getStateChange() == ItemEvent.SELECTED) {
    					ins.append(c);
    
    				}
    
    				if (e.getStateChange() == ItemEvent.DESELECTED) {
    
    					if (ins.getText().contains(c)) {
    						// I think I need to write the code to here which will
    						// delete the text
    					}
    				}
    
    			}
    		});
    
    		JCheckBox nd = new JCheckBox("Another text ...");
    		// and another checkbox to add other text
    		
    
    		lTab.add(lst);
    		lTab.add(nd);
    
    		JPanel rTab = new JPanel();
    		rTab.setLayout(new FlowLayout());
    
    		rTab.add(ins);
    
    		table.add(lTab);
    		table.add(rTab);
    		table.pack();
    		table.setVisible(true);
    	}
    }
    Last edited by manalinik; 01-04-2012 at 03:34 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: JCheckBox and JTextArea question

    If the Strings are unique and there is no substrings, then finding and replacing the String with an empty String should work.
    If not, you need to remember where in the textarea the added String is located so you can remove it and update the locations of all Strings added after it.
    manalinik likes this.

  3. #3
    manalinik is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: JCheckBox and JTextArea question

    Thanks for your answer. Did you mean Strings like
    Java Code:
    String a = "This will be added when you check chckBx1"
    by unique String? I don't get it sorry, will you be more specific? If unique Strings are like above I wrote, then will I be able to erase it when I add a String like
    Java Code:
    String b="This will be added after text when you check checkbox2"
    when I uncheck the first checkbox? Thanks in advance

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: JCheckBox and JTextArea question

    If the text area contains:
    This is a String here in the text area
    and the string you add is "a String"
    Then the new contents of the text area would be:
    This is a String here in the text area a String

    The String "a String" is not unique now. It occurs in two places in the text area.
    If you try to remove it you could remove the wrong one.

  5. #5
    manalinik is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: JCheckBox and JTextArea question

    Ohh ok, i see, thanks.. All I want is this, for example think that there are 3 checkboxes about 3 people biographies. When I check 1st checkbox x person's bio will be added to textarea, then when I checked second ones and when I check third ones. Bio's are defined before, all I am asking is when I uncheck the first checkbox, I just want to delete the first one's --x person's bio for e.g.-- text but others will be there. Is there a way to do this? or any suggestions doing it any alternative ways?

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

    Default Re: JCheckBox and JTextArea question

    Not sure if I understand the question, but if you want to add/remove something from a component, then maybe using a JList would be a better approach. JList already support an API that allows you to add/remove items from the list.

    Check out: How to Use Lists (The Javaô Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) for an example of how to hire/fire people which you should be able to modify for your requirements.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: JCheckBox and JTextArea question

    Is there a way to do this?
    Look at the text area class's API doc. It has methods for getting the contents and setting the contents.
    Look at the API doc for the String class. It has methods for changing the contents of a String.

    One way: get a String, change the String, put the new String back.

Similar Threads

  1. Swing JTextArea + JScrollPane question?
    By niba10 in forum AWT / Swing
    Replies: 3
    Last Post: 04-19-2011, 01:07 AM
  2. Replies: 5
    Last Post: 08-22-2010, 10:04 PM
  3. JTable vs JTextArea scrolling text question
    By adonos in forum AWT / Swing
    Replies: 2
    Last Post: 05-24-2010, 09:15 PM
  4. Can we add JCheckBox array into JTextArea?
    By nancyhung in forum AWT / Swing
    Replies: 1
    Last Post: 02-17-2008, 01:07 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
  •