Results 1 to 8 of 8
  1. #1
    ogsirus is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default Radio Buttons and Jtextfield

    hi everyone i dont know if this is the right catergory but anyway

    want im trying to do is create a Jframe shown in the attachment:

    Basically i want the user to select for localhost or to input a specific ip

    i want to use RadioButtons. but with the specific ip there will be a Textfield

    there are also two other buttons one is to enter the system and one to exit.

    im struggling with trying to get a JRadioButton linking with a JTextField. any idea

    Thanks!
    Attached Thumbnails Attached Thumbnails Radio Buttons and Jtextfield-jframe.png  

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

    Default

    What does "JRadioButton linking with a JTextField" mean?

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

    Default

    Moving to the Swing forum which to better align the post topic with the appropriate forum.

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

    Default

    Agree with R-etc. You need to specify what you mean by "link". If you mean physically link, then you'll want to study the Sun Java tutorials on Laying out Components in a Container. If you mean logically linking, then look at the tutorial on using buttons and ActionListeners.

    Much luck!

  5. #5
    ogsirus is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    I want the second radiobutton to be able to understand what is written in the textfield. i dont know if that makes thinks clear

    the first Radiobuton would allow the user to select the local host

    the second button i have to know what is inputted in the textfield.

    im reading up about listeners etc now see if i could make any more progress

  6. #6
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by ogsirus View Post
    I want the second radiobutton to be able to understand what is written in the textfield. i dont know if that makes thinks clear
    I'm not sure you really want this. What you may instead want is that the portion of the program that displays this dialog looks at the JTextField text if the second JRadioButton is selected. There's a big difference here. To do that, you'll need to have the code for this dialog allow outside classes to query it for the state of its JRadioButtons and JTextFields.

  7. #7
    ogsirus is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default code so far

    ok. still unsure but here is the coding so far. maybe somebody can shed a but of light on it :) ta

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.io.IOException;
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    
    
    public class login extends JFrame
    {
        // Variables declaration
        private JLabel heading;
        private JLabel nameLabel;
        private JLabel passLabel;
        private JMenuItem radioGroup;
        private JRadioButton host;
        private JRadioButton ip;
        private JTextField spec1, spec2, spec3, spec4, nameField;
        private JPasswordField passField;
        private JButton enter;
        private JButton exit;
        private JPanel contentPane;
        private String specific, userName;
        private InetAddress localHost;
        private InetAddress connect;
        int loaddata = 1;
        int filenumber = 1;
        
        
        loginRead readFile;
        loginWrite writeFile;
        BufferedReader readStrings;
    
        // End of variables declaration
    
    
        public login()
        {
            super();
            initializeComponent();
            this.setVisible(true);
        }
        private void initializeComponent()
        {
            heading = new JLabel();
            nameLabel = new JLabel();
            passLabel = new JLabel();
            host = new JRadioButton("Localhost",  true);
            ip = new JRadioButton("Specify IP",  false);
            ButtonGroup group = new ButtonGroup();
            radioGroup = new JMenuItem();
            spec1= new JTextField(3);
            spec2= new JTextField(3);
            spec3= new JTextField(3);
            spec4= new JTextField(3);
            nameField = new JTextField(20);
            passField = new JPasswordField(20);
            specific = new String();
            userName = new String();
            enter = new JButton();
            exit = new JButton();
            contentPane = (JPanel)this.getContentPane();
            
            group.add(host);
            group.add(ip);
            
            heading.setText("Login Page Please choose from Localhost or Specific an IP");
            nameLabel.setText("User Name");
            passLabel.setText("Password");
            
            if (loaddata ==1){
                readFile = new loginRead(filenumber);
                userName = readFile.getuserName();
                nameLabel.setText(userName);
                          
            }
                
            
            enter.setText("Enter");
            enter.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
              
              
                {
                    userName = nameLabel.getText();
                   
                    if(host.isSelected())
                    {
                        try
                        {
                        InetAddress localHost = InetAddress.getLocalHost();
                        
                        byte[] ip = localHost.getAddress();
                        
                        int i = 4;
                        String ipAddress = "";
                        for (byte b : ip)
                        {
                            ipAddress += (b & 0xFF);
                            if (--i > 0)
                            {
                                Mainmenu run = new Mainmenu();
                            }
                        }
                    }catch (UnknownHostException es )
                    {
                        System.out.println("not connected to local host");
                    }
                }
    
                    if(ip.isSelected())
                    {
     
                        String specific = spec1.getText() +"." + spec2.getText() + "." + spec3.getText() + "." + spec4.getText();
                        
                        
                    }
                }
    
                                 
               
    
            });
            
            exit.setText("Exit");
            exit.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                    System.exit(0);
                }
    
            });
            
            contentPane.setLayout(null);
            addComponent(contentPane, heading, 69,12,305,34);
            addComponent(contentPane, nameLabel, 80,60,71,18);
            addComponent(contentPane, passLabel, 80,92,71,18);
            addComponent(contentPane, host, 64,128,100,22);
            addComponent(contentPane, ip, 65,174,100,24);
            addComponent(contentPane, spec1, 176,177,43,21);
            addComponent(contentPane, spec2, 221,177,43,21);
            addComponent(contentPane, spec3, 266,177,43,21);
            addComponent(contentPane, spec4, 311,177,43,21);
            addComponent(contentPane, nameField, 170,59,125,22);
            addComponent(contentPane, passField, 170,91,125,22);
            addComponent(contentPane, enter, 57,204,83,28);
            addComponent(contentPane, exit, 236,203,83,28);
            
            this.setTitle("Login into System");
            this.setLocation(new Point(0, 0));
            this.setSize(new Dimension(388, 287));
        }
    
        
        private void addComponent(Container container,Component c,int x,int y,int width,int height)
        {
            c.setBounds(x,y,width,height);
            container.add(c);
        }
    
    
    }
    Moderator Edit: Code tags added to aid readability
    Last edited by Fubarable; 05-26-2010 at 10:40 PM. Reason: Moderator Edit: Code tags added to aid readability

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

    Default

    I hope you don't mind that I added code tags to your post above. This should help your code retain its formatting and be readable by others in the forum. To do this yourself in future posts, please click on the link in my signature. Also, you really don't want to use null layout and absolute positioning as this is very hard to maintain. Have a look in the Sun Swing tutorials on how to use layout managers. You'll be glad you did.

    Much luck!

Similar Threads

  1. Select one of X radio buttons
    By Adrien in forum AWT / Swing
    Replies: 1
    Last Post: 02-25-2010, 06:30 PM
  2. How can i setString when i use radio buttons?
    By Tahorn in forum AWT / Swing
    Replies: 4
    Last Post: 02-16-2010, 02:03 PM
  3. Displaying Radio Buttons in JSP
    By kishan in forum Advanced Java
    Replies: 4
    Last Post: 10-23-2009, 03:44 PM
  4. How to use Radio Buttons
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 02:26 PM
  5. property for radio buttons
    By swapnanair in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 12-03-2007, 08:39 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
  •