Results 1 to 7 of 7
Thread: JCheckBox and JTextArea question
- 01-04-2012, 02:32 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 7
- Rep Power
- 0
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 02:34 PM.
- 01-04-2012, 03:31 PM #2
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.
- 01-04-2012, 04:30 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 7
- Rep Power
- 0
Re: JCheckBox and JTextArea question
Thanks for your answer. Did you mean Strings like
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 likeJava Code:String a = "This will be added when you check chckBx1"
when I uncheck the first checkbox? Thanks in advanceJava Code:String b="This will be added after text when you check checkbox2"
- 01-04-2012, 04:35 PM #4
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.
- 01-04-2012, 04:56 PM #5
Member
- Join Date
- Dec 2011
- Posts
- 7
- Rep Power
- 0
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?
- 01-04-2012, 04:59 PM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 01-04-2012, 04:59 PM #7
Re: JCheckBox and JTextArea question
Look at the text area class's API doc. It has methods for getting the contents and setting the contents.Is there a way to do this?
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
-
Swing JTextArea + JScrollPane question?
By niba10 in forum AWT / SwingReplies: 3Last Post: 04-19-2011, 12:07 AM -
Accessing a JTextArea after creation (Intro level question)
By jferrara in forum AWT / SwingReplies: 5Last Post: 08-22-2010, 09:04 PM -
JTable vs JTextArea scrolling text question
By adonos in forum AWT / SwingReplies: 2Last Post: 05-24-2010, 08:15 PM -
Can we add JCheckBox array into JTextArea?
By nancyhung in forum AWT / SwingReplies: 1Last Post: 02-17-2008, 12:07 AM -
using JCheckBox, JButton and JTextArea with JDBC
By warship in forum JDBCReplies: 1Last Post: 08-08-2007, 01:25 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks