Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    mlibot is offline Member
    Join Date
    Sep 2009
    Posts
    44
    Rep Power
    0

    Default uses of radiobutton...

    hi all, im new in java... i have a problem about radiobutton...
    i have a 2 radiobutton 2 textbox and 2 buttons.. and i want to do is..

    when i choose radiobutton1 the textbox2 and button2 was being disabled. when i choose radiobutton2 the textbox1 and button1 was being disabled also but the the textbox2 and button2 did not return to enabled..

    what should i do..

    thanks in advance for your reply.. hope you help me..

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Improve your listeners.

  3. #3
    mlibot is offline Member
    Join Date
    Sep 2009
    Posts
    44
    Rep Power
    0

    Default

    how to improve..? i don't know what to do...
    i want more details..

    thanks.

  4. #4
    mlibot is offline Member
    Join Date
    Sep 2009
    Posts
    44
    Rep Power
    0

    Default

    this is my code... I'm planning to use if statement but i do not know how to do..

    help me please..

    radioforSFile.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    textField1.setEnabled(false);
    browseDir.setEnabled(false);
    radioforDir.setEnabled(true);
    }
    });
    radioforDir.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    textField.setEnabled(false);
    browseSFile.setEnabled(false);
    radioforSFile.setEnabled(true);
    }
    });

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You never setEnabled(true) for textboxes or textFields in your code.

  6. #6
    mlibot is offline Member
    Join Date
    Sep 2009
    Posts
    44
    Rep Power
    0

    Default

    Ok... thanks.. but it is still not working for what i want to happen...

    when i choose radioforSFile/radiobutton1 the textbox2/textField1 and button2/browseDir will be disabled and that was good. but if i choose radiorforDir/radiobutton2 the textbox1/textField and button1/browseSFile will be disabled but the textbox2/textField1 and button2/browseDir will not going to enabled it still disabled.. what should i do..

    == for clarification ==
    textField1-name of my textbox2
    textField-name of my textbox1
    button1-name of my browseSFile
    button2-name of my browseDir

  7. #7
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    800
    Rep Power
    6

    Default

    post ur complete code.How can we know all ur variables and everything with 2 lines of ur code?
    Ramya:cool:

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    When you use setEnabled(false) on the "1" items use setEnabled(true) on the "2" items.

    Do both. Do you understand? Is there anybody home?

  9. #9
    mlibot is offline Member
    Join Date
    Sep 2009
    Posts
    44
    Rep Power
    0

    Default

    Java Code:
    import java.awt.Container;
    import java.awt.FileDialog;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.lang.Boolean;
    
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JRadioButton;
    import javax.swing.JCheckBox;
    
    public class FileBrowse extends JFrame {
    
        JButton browseSFile = new JButton("Browse1...");
        JButton browseDir = new JButton("Browse2...");
    	JButton browseEn = new JButton("Browse3...");
        JTextField textField = new JTextField(20);
    	JTextField textField1 = new JTextField(20);
    	JTextField textField2 = new JTextField(20);
    	JCheckBox checkforEn = new JCheckBox("English File");
    	JRadioButton radioforSFile = new JRadioButton("Single Files");
    	JRadioButton radioforDir = new JRadioButton("Directories");
    	
        public FileBrowse() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setResizable(false);
    
            browseSFile.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    onBrowseSFile();
                }
            });
            browseDir.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    onBrowseDir();
                }
            });
    		 browseEn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    onBrowseEn();
                }
            });
    		[COLOR="Red"]radioforSFile.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				textField1.setEnabled(false);
    				browseDir.setEnabled(false);
    			}
    		});
    		radioforDir.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				textField.setEnabled(false);
    				browseSFile.setEnabled(false);
    			}[/COLOR]
    		});
    
            Container container = getContentPane();
            container.setLayout(new GridLayout(3, 3));
    		
    		container.add(checkforEn);
    		container.add(textField2);
    		container.add(browseEn);
    		container.add(radioforSFile);
    		container.add(textField);
            container.add(browseSFile);
    		container.add(radioforDir);
    		container.add(textField1);
            container.add(browseDir);
            
            pack();
        }
    	
    
        protected void onBrowseSFile() {
            JFileChooser fileChooser = new JFileChooser();
            int result = fileChooser.showDialog(this, "Open/Save");
            if (result == JFileChooser.APPROVE_OPTION) {
                File file = fileChooser.getSelectedFile();
                textField.setText(file.toString());
            }
        }
    
        protected void onBrowseDir() {
            FileDialog fileDialog = new FileDialog(this, "Open/Save");
            fileDialog.setVisible(true);
            if (fileDialog.getFile() != null) {
                String directory = fileDialog.getDirectory();
                if (!directory.endsWith(File.separator)) {
                    directory += File.separator;
                }
                String file = fileDialog.getFile();
                textField1.setText(directory + file);
            }
        }
    	
    	protected void onBrowseEn() {
    		FileDialog fileDialog1 = new FileDialog(this, "Open/Save");
    		fileDialog1.setVisible(true);
    		if (fileDialog1.getFile() != null) {
    			String directory = fileDialog1.getDirectory();
    			if (!directory.endsWith(File.separator)) {
    				directory += File.separator;
    			}
    			String file = fileDialog1.getFile();
    			textField2.setText(directory + file);
    		}
    	
    	}
        public static void main(String[] args) {
            new FileBrowse().setVisible(true);
        }
    }

    that's my code... the red text is my code for radiobutton

    thanks.

    Mod: I added code tags to aid in readability.
    Last edited by Fubarable; 09-17-2009 at 02:12 AM.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    http://java.sun.com/docs/books/tutor...mlistener.html

    Java Code:
          ButtonGroup btnGroup = new ButtonGroup();
          btnGroup.add(radioforSFile);
          btnGroup.add(radioforDir);
          radioforSFile.addItemListener(new ItemListener() 
          {
             public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                   textField1.setEnabled(false);
                   browseDir.setEnabled(false);
                   textField.setEnabled(true);
                   browseSFile.setEnabled(true);
                }
             }
          });
          radioforDir.addItemListener(new ItemListener() 
          {
             public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                   textField.setEnabled(false);
                   browseSFile.setEnabled(false);
                   textField1.setEnabled(true);
                   browseDir.setEnabled(true);
                }
             }
          });
    Last edited by Fubarable; 09-17-2009 at 02:32 AM.

  11. #11
    mlibot is offline Member
    Join Date
    Sep 2009
    Posts
    44
    Rep Power
    0

    Default

    thanks. I'll try it to apply... but i hope you are all open for my other problems..

    thanks for your help.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  13. #13
    mlibot is offline Member
    Join Date
    Sep 2009
    Posts
    44
    Rep Power
    0

    Default

    when i used your codes i got this error.

    FileBrowse.java:52: cannot find symbol
    symbol : class ItemListener
    location: class FileBrowse
    radioforSFile.addItemListener(new ItemListener()

    FileBrowse.java:52: cannot find symbol
    symbol : class ItemListener
    location: class FileBrowse
    radioforDir.addItemListener(new ItemListener()

    2 errors

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  15. #15
    mlibot is offline Member
    Join Date
    Sep 2009
    Posts
    44
    Rep Power
    0

    Default

    I already imports this components:

    Java Code:
    import java.awt.Container;
    import java.awt.FileDialog;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JRadioButton;
    import javax.swing.JCheckBox;
    import javax.swing.ButtonGroup;

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The error messages tell you what imports you're missing. With a little effort you can figure this out on your own. Keep at it.

  17. #17
    mlibot is offline Member
    Join Date
    Sep 2009
    Posts
    44
    Rep Power
    0

    Default

    ok I'll try to fix it... but it takes a lot of time to fix this problem hope that you tell/help me what is the import missing.

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Again, the error messages are already telling you this. They're really not that cryptic, and I'm trying to get you to learn to use these messages to your advantage. Once you figure out the name of the class that's not imported, then you can look it up in the Java API to figure out how to import it.

    Java API

  19. #19
    mlibot is offline Member
    Join Date
    Sep 2009
    Posts
    44
    Rep Power
    0

    Default

    I got it now... thanks for your help... hope you help me again when i have some questions that I'm not know how to solve it...

    Thank You..

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Page 1 of 2 12 LastLast

Similar Threads

  1. RadioButton selections in an Exam application
    By riddhikarnik in forum AWT / Swing
    Replies: 6
    Last Post: 06-29-2009, 04:41 AM
  2. freeze radioButton without disable them
    By itaipee in forum AWT / Swing
    Replies: 5
    Last Post: 05-25-2009, 09:29 AM
  3. RadioButton
    By tiger100plus in forum New To Java
    Replies: 1
    Last Post: 01-05-2009, 08:04 PM
  4. radiobutton selection
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-17-2008, 02:58 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
  •