Hi, Im creating a GUI however i'm experiencing some difficulties. I want to have a text field and a button both placed inside a radiobutton. I've tried inputting ONLY the button, but the button is not displayed when i run the codes. Would be good if light could be shed, thanks.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class Layout {

    public static void main(String[] args) {
    	
	
    //for query
    JRadioButton query1, query2, query3;
    query1 = new JRadioButton("for uploading of file");
    //query1.setActionCommand("bach1");
    query2 = new JRadioButton("dropdownlist for database");
    //query2.setActionCommand("bach2");
    query3 = new JRadioButton("entering of sequence");
    //query3.setActionCommand("Shostakovich");
    
    JButton UploadQuery = new JButton("Upload");
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(UploadQuery);

    
    
    //a group
    final ButtonGroup group1 = new ButtonGroup();
    group1.add(query1);
    group1.add(query2);
    group1.add(query3);
    	
    	
    //for target
    JRadioButton target1, target2, target3;
    target1 = new JRadioButton("for uploading of file");
    //choice1.setActionCommand("bach1");
    target2 = new JRadioButton("dropdownlist for database");
    //choice2.setActionCommand("bach2");
    target3 = new JRadioButton("entering of sequence");
    //choice3.setActionCommand("Shostakovich");
    
    //a group that ensures we vote for only one
    final ButtonGroup group2 = new ButtonGroup();
    group2.add(target1);
    group2.add(target2);
    group2.add(target3);    	
    	 	

    //throw everything together
    JFrame frame = new JFrame("Biomolecular Structure Comparison");
    frame.setPreferredSize(new Dimension(500, 500));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    

    Container QUERY = frame.getContentPane();
    QUERY.setLayout(new GridLayout(10,20) );
    QUERY.add(new JLabel("Query Structure:"));
    QUERY.add(query1,buttonPanel);
    QUERY.add(query2);
    QUERY.add(query3);

    Container TARGET = frame.getContentPane();
    TARGET.setLayout(new GridLayout(10,50) );
    TARGET.add(new JLabel("Target Structure:"));
    TARGET.add(target1);
    TARGET.add(target2);
    TARGET.add(target3);
    
    frame.pack();
    frame.setVisible(true);	
 	
    }//end of main
    
}//end of class

Hi,

do you mean something like this:

query1 = new JRadioButton("for uploading of file");
		query1.setLayout(new FlowLayout());
		query1.add(new JTextField("textfield"));
		query1.add(new JButton("Button"));

Thanks for the reply. now i have an alignment problem for all the radio buttons. cannot seem to arrange the radio buttons above each other.

what i expect to get is smth like that


This is one panel this is the second panel
Query Structure: Target Structure:
[radiobutton1] [radio1]
[radiobutton2] [radio2]
[radiobutton3] [radio3]

Now Im only able to see the things that i've added in the second panel but not the first. Would be good if some help can be given. Thanks!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class Layout {

    public static void main(String[] args) {
    	
    	//create frame
    	JFrame frame = new JFrame("Biomolecular Structure Comparison");
    	frame.setSize(600,600);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setLocation(300,100);
		GridLayout grid = new GridLayout(1,1);
		FlowLayout flow = new FlowLayout();
			
    	//add radio buttons
    	JRadioButton query1, query2, query3, target1, target2, target3;
    	
    	query1 = new JRadioButton("for uploading of file query");
		query1.add(new JTextField("textfield"));
		query1.add(new JButton("Button"));
		//query1.setLayout(grid);
    	
    	query2 = new JRadioButton("dropdownlist for database");
    	//query2.setLayout(grid);
    	
    	query3 = new JRadioButton("entering of sequence");
    	//query3.setLayout(grid);
    	
    	target1 = new JRadioButton("for uploading of file target");
    	target1.add(new JTextField("textfield"));
		target1.add(new JButton("Button"));
		//target1.setLayout(grid);
		
    	target2 = new JRadioButton("dropdownlist for database");
    	//target2.setLayout(grid);
    	
    	target3 = new JRadioButton("entering of sequence");
    	//target3.setLayout(grid);
    		
    	//query group button
    	final ButtonGroup group1 = new ButtonGroup();
    	group1.add(query1);
    	group1.add(query2);
    	group1.add(query3);
    
    	//target group button
    	final ButtonGroup group2 = new ButtonGroup();
    	group2.add(target1);
    	group2.add(target2);
    	group2.add(target3);    
    	
    	//panel
    	JPanel panel1 = new JPanel();
    	panel1.setLayout(grid);
    	panel1.add(new JLabel("Query Structure:"));	
    	panel1.add(query1);
    	panel1.add(query2);
    	panel1.add(query3);
   	
    	
    	JPanel panel2 = new JPanel();
    	panel2.setLayout(grid);
    	panel2.add(new JLabel("Target Structure:"));
    	panel2.add(target1);
    	panel2.add(target2);
    	panel2.add(target3);
    	
 
    	    	//content pane
    	//Container contentPane = getContentPane();
		    	//layout
    	//FlowLayout layout = new FlowLayout();
    	
    	//contentPane.setLayout(layout);
    	frame.getContentPane().add(panel1);
    	frame.getContentPane().add(panel2);
    	
    	
    	
    	//show frame
    	frame.setVisible(true);
	
   
 	
    }//end of main
    
}//end of class

Thanks for the reply. now i have an alignment problem for all the radio buttons. cannot seem to arrange the radio buttons above each other.

what i expect to get is smth like that


This is one panel this is the second panel
Query Structure: Target Structure:
[radiobutton1] [radio1]
[radiobutton2] [radio2]
[radiobutton3] [radio3]

Now Im only able to see the things that i've added in the second panel but not the first. Would be good if some help can be given. Thanks!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class Layout {

    public static void main(String[] args) {
    	
    	//create frame
    	JFrame frame = new JFrame("Biomolecular Structure Comparison");
    	frame.setSize(600,600);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setLocation(300,100);
		GridLayout grid = new GridLayout(1,1);
		FlowLayout flow = new FlowLayout();
			
    	//add radio buttons
    	JRadioButton query1, query2, query3, target1, target2, target3;
    	
    	query1 = new JRadioButton("for uploading of file query");
		query1.add(new JTextField("textfield"));
		query1.add(new JButton("Button"));
		//query1.setLayout(grid);
    	
    	query2 = new JRadioButton("dropdownlist for database");
    	//query2.setLayout(grid);
    	
    	query3 = new JRadioButton("entering of sequence");
    	//query3.setLayout(grid);
    	
    	target1 = new JRadioButton("for uploading of file target");
    	target1.add(new JTextField("textfield"));
		target1.add(new JButton("Button"));
		//target1.setLayout(grid);
		
    	target2 = new JRadioButton("dropdownlist for database");
    	//target2.setLayout(grid);
    	
    	target3 = new JRadioButton("entering of sequence");
    	//target3.setLayout(grid);
    		
    	//query group button
    	final ButtonGroup group1 = new ButtonGroup();
    	group1.add(query1);
    	group1.add(query2);
    	group1.add(query3);
    
    	//target group button
    	final ButtonGroup group2 = new ButtonGroup();
    	group2.add(target1);
    	group2.add(target2);
    	group2.add(target3);    
    	
    	//panel
    	JPanel panel1 = new JPanel();
    	panel1.setLayout(grid);
    	panel1.add(new JLabel("Query Structure:"));	
    	panel1.add(query1);
    	panel1.add(query2);
    	panel1.add(query3);
   	
    	
    	JPanel panel2 = new JPanel();
    	panel2.setLayout(grid);
    	panel2.add(new JLabel("Target Structure:"));
    	panel2.add(target1);
    	panel2.add(target2);
    	panel2.add(target3);
    	
 
    	    	//content pane
    	//Container contentPane = getContentPane();
		    	//layout
    	//FlowLayout layout = new FlowLayout();
    	
    	//contentPane.setLayout(layout);
    	frame.getContentPane().add(panel1);
    	frame.getContentPane().add(panel2);
    	
    	
    	
    	//show frame
    	frame.setVisible(true);
	
   
 	
    }//end of main
    
}//end of class

Currently, i am able to align the categories one on the left and the other on the right as i'm using the Border to segregate them, however the border stretches from the top all the way down to the end(bottom). is there a way to just let it end just below the last radio button?

below are my codes

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class Layout {

    public static void main(String[] args) {
    	
    	//create frame
    	JFrame frame = new JFrame("Biomolecular Structure Comparison");
    	frame.setSize(800,600);
    	frame.setLayout(new GridLayout(1,2) );
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setLocation(300,100);
		GridLayout grid = new GridLayout(1,1);
		FlowLayout flow = new FlowLayout();
			
    	//add radio buttons
    	JRadioButton query1, query2, query3, target1, target2, target3;
    	
    	query1 = new JRadioButton();
    	query1.setLayout(new FlowLayout());
		query1.add(new JTextField("Upload Your Raw PDB File Here"));
		query1.add(new JButton("Upload"));

    	query2 = new JRadioButton("dropdownlist for database");

    	query3 = new JRadioButton("entering of sequence");

    	target1 = new JRadioButton();
    	target1.setLayout(new FlowLayout());
    	target1.add(new JTextField("Upload Your Raw PDB File Here"));
		target1.add(new JButton("Upload"));

    	target2 = new JRadioButton("dropdownlist for database");

    	target3 = new JRadioButton("entering of sequence");
	
    	//query group button
    	final ButtonGroup group1 = new ButtonGroup();
    	group1.add(query1);
    	group1.add(query2);
    	group1.add(query3);
    
    	//target group button
    	final ButtonGroup group2 = new ButtonGroup();
    	group2.add(target1);
    	group2.add(target2);
    	group2.add(target3);    
   	
    JPanel querypane = new JPanel();
    //contentpane.setLayout(new GridLayout(2,2) );
    querypane.setSize(10,10);
    querypane.setBorder(BorderFactory.createTitledBorder("Query Structure:"));
    querypane.add(query1);
    querypane.add(query2);
    querypane.add(query3);

	JPanel targetpane = new JPanel();
    //	targetpane.setLayout(new GridLayout(1,1) );
	targetpane.setBorder(BorderFactory.createTitledBorder("Target Structure:"));
    targetpane.add(target1);
    targetpane.add(target2);
    targetpane.add(target3);
 
	frame.getContentPane().add(querypane, BorderLayout.CENTER);
	frame.getContentPane().add(targetpane, BorderLayout.CENTER);
	
    	//show frame
    	frame.setVisible(true);
 	
    }//end of main
    
}//end of class

I am not very good at using Layouts but here's what I would do:
Create 2 JPanels representing EAST and WEST in a BorderLayout. These would represent "Query structure" and "Target structure". Use Boxlayout to layout them vertically.
Create 2 JPanels for the radiobuttons used with a textfield and a button (query1, target1). Use BoxLayout to layout them horizonatlly.

Something like this:

/* Set border layout on the mainframe */
frame.setLayout(new BorderLayout());

JPanel target1, query1;
query1 = new JPanel();
/* use box layout horizontally */
query1.setLayout(new BoxLayout(query1, BoxLayout.LINE_AXIS));
/* Add components */
query1.add(new JRadioButton("for uploading to query: "));
query1.add(new JTextField("textfield"));
query1.add(new JButton("Button"));
/* Align left */
query1.setAlignmentX(Component.LEFT_ALIGNMENT);	

// the same goes for target1

/* create west panel */
JPanel panel1 = new JPanel();
/* use box layout vertically */
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
panel1.add(new JLabel("Query Structure:"));
/* add queries... */
// same for panel2

// finally add to frame and pack
frame.getContentPane().add(panel1, BorderLayout.WEST);
frame.getContentPane().add(panel2, BorderLayout.EAST);
frame.pack();

I hope your following :)
I think you can use flowlayout instead of borderlayout on the frame if you like.

Thank you so much, I've managed align them. Another thing is that, how do i disable the radio buttons if they're not selected?

here is my latest codes, did some changes to it.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Layout extends JFrame {
	
   	Layout() {
   
   		//add radio buttons
    	JRadioButton query1, query2, query3, target1, target2, target3;
    	
    	query1 = new JRadioButton();
    	//query1.setLayout(new BoxLayout(query1, BoxLayout.LINE_AXIS));
    	query1.setLayout(new FlowLayout());
		query1.add(new JTextField(15));
		query1.add(new JButton("Upload"));
		//query1.setAlignmentX(Component.LEFT_ALIGNMENT);

    	query2 = new JRadioButton();
    	//query2.setLayout(new BoxLayout(query2, BoxLayout.LINE_AXIS));
    	query2.setLayout(new FlowLayout());
    	query2.add(new JLabel("Select PDB File:"));
    	query2.add(new JComboBox());
    	//query2.setAlignmentX(Component.LEFT_ALIGNMENT);

    	query3 = new JRadioButton();
    	//query3.setLayout(new BoxLayout(query3, BoxLayout.LINE_AXIS));
    	query3.setLayout(new FlowLayout());
    	query3.add(new JLabel("    Enter Sequence :"));
		query3.add(new JTextField(15));
		//query3.setAlignmentX(Component.LEFT_ALIGNMENT);
		
    	target1 = new JRadioButton();
    	//target1.setLayout(new BoxLayout(target1, BoxLayout.LINE_AXIS));
    	target1.setLayout(new FlowLayout());
    	target1.add(new JTextField(15));
		target1.add(new JButton("Upload"));
		//target1.setAlignmentX(Component.LEFT_ALIGNMENT);

    	target2 = new JRadioButton();
    	//target2.setLayout(new BoxLayout(target2, BoxLayout.LINE_AXIS));
    	target2.setLayout(new FlowLayout());
    	target2.add(new JLabel("Select PDB File :"));
    	target2.add(new JComboBox());
        //target2.setAlignmentX(Component.LEFT_ALIGNMENT);

    	target3 = new JRadioButton();
    	//target3.setLayout(new BoxLayout(target3, BoxLayout.LINE_AXIS));
		target3.setLayout(new FlowLayout());
    	target3.add(new JLabel("    Enter Sequence :"));		
		target3.add(new JTextField(15));
		//target3.setAlignmentX(Component.LEFT_ALIGNMENT);
		
    	//query group button
    	final ButtonGroup group1 = new ButtonGroup();
    	group1.add(query1);
    	group1.add(query2);
    	group1.add(query3);
    
    	//target group button
    	final ButtonGroup group2 = new ButtonGroup();
    	group2.add(target1);
    	group2.add(target2);
    	group2.add(target3);    
   	
    	JPanel querypane = new JPanel();
    	querypane.setLayout(new BoxLayout(querypane, BoxLayout.Y_AXIS));
    	//querypane.setLayout(new GridLayout(3,3) );
    	querypane.setSize(10,10);
    	querypane.setBorder(BorderFactory.createTitledBorder("Query Structure:"));
    	querypane.add(query1);
    	querypane.add(query2);
    	querypane.add(query3);

		JPanel targetpane = new JPanel();
		targetpane.setLayout(new BoxLayout(targetpane, BoxLayout.Y_AXIS));
    	//targetpane.setLayout(new GridLayout(3,3) );
		targetpane.setBorder(BorderFactory.createTitledBorder("Target Structure:"));
    	targetpane.add(target1);
    	targetpane.add(target2);
    	targetpane.add(target3);
 
		Container contentPane = getContentPane();
		contentPane.add(querypane);
		contentPane.add(targetpane);
   
   		}//end of Layout()
   

    public static void main(String[] args) {
    	
    	//create frame
    	Layout frame = new Layout() ;
    	frame.setTitle("Biomolecular Structure Comparison");
    	frame.setSize(850,650);
    	frame.setLayout(new FlowLayout());
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setLocation(300,100);
				    
		//show frame
    	frame.setVisible(true); 
    		   
		    }//end of main
		    	
    }//end of class

hmmmm, what I meant was to disable components of that radiobutton if it is not selected (like greying the part out). for eg.

o upload file
o enter....
o blah....

if i were to click on the upload file radio button the 2nd and the 3rd radio button component will be grey-ed out.

aha ok. You can add listeners to the radiobuttons. So if the user clicks one radiobutton you can grey the others out.
You might want to have some initial configuration of how the buttons are enabled and from there listen to any activity on the radiobuttons and take the proper action.

myRadioButton.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    if(myRadioButton.isSelected()) {
      theOtherRadioButton.setEnabled(false);
    } else {
      theOtherRadioButton.setEnabled(true);
    }
  }
});

When adding ActionListeners you can either do it like above, with an inner anonymous class or create a class that implements ActionListener and inject that one.

So Sorry, I'm actually not very familiar in using listeners and event handling, do you mind giving me an example on how to add it into my codes?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.