Cant get my dialog work

I have tried diffrent solutions since yesterday but I cant make this work.

What I want is to have
JRadioButton
JRadioButton
 
Jlabel:     JTextField
Jlabe:      JTextFiled

SaveButton    CancelButton

Now I have managed to get the dialog up and showing.......but I cant get the correct look. How do I managed that?


package p3;

import java.awt.Container;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JRadioButton;

public class AddDialog extends JDialog
{    

	private JRadioButton button1;    
	private JRadioButton button2;     

	public AddDialog(Frame owner){              
		
		super(owner, true);        
		
		Container contentPane = getContentPane();        
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));         
		
		button1 = new JRadioButton("Elite Runner"); 
		button2 = new JRadioButton("Motion Runner");       
		button1.setSelected(true); 		
		contentPane.add(button1);  
		contentPane.add(button2); 		       
		        
		ButtonGroup bGroup = new ButtonGroup();       
		bGroup.add(button1);        
		bGroup.add(button2);  
		
		JButton btnSave = new JButton("Save"); 
		JButton btnCancel = new JButton("Cancel"); 
		
		btnSave.addActionListener(new ActionListener() 
		{  
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				setVisible(false);                
				dispose();
			}       
		});  		
		
		btnCancel.addActionListener(new ActionListener() 
		{  
			public void actionPerformed(ActionEvent e) {				             
				setVisible(false);
				dispose();
			}       
		});   
		
		
		contentPane.add(btnSave);  
		contentPane.add(btnCancel);  
		pack();   
	}     

	

}

Open in new window

MickeysAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
use two panels

one panel on the bottom for the save and cancel buttons
and another panel for the rest

then add those 2 panels to your dialog (using a BorderLayout)
0
MickeysAuthor Commented:
ok. I manged to do it with the buttons. But the labels and textfield I dont know how to get

aaaa:    aaaaa
bbbb:   bbbbbb

you are saying borderlayout but how?


package p3;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Frame;
import java.awt.event.*;
import javax.swing.*;


public class AddDialog extends JDialog
{    

	private static final long serialVersionUID = 1L;
	private JRadioButton eliteRadioButton;    
	private JRadioButton motionRadioButton;   
	
	private JPanel panel1 = new JPanel();
	private JPanel panel2 = new JPanel();
	
	private String[] shirtflag = { "Yes", "No"}; 
    private String eliteString = "Elite"; 
    private String motionString = "Motion"; 
     
    private JTextField name = new JTextField(); 
    private JTextField time = new JTextField(); 
    private JTextField startnr = new JTextField(); 
    private JTextField licensnr = new JTextField(); 
    private JTextField club = new JTextField(); 
     
    private JLabel nameLabel = new JLabel("Namn: "); 
    private JLabel timeLabel = new JLabel("Time: "); 
    private JLabel startnrLabel = new JLabel("Start nr: "); 
    private JLabel licensnrLabel = new JLabel("Licens nr: "); 
    private JLabel clubLabel = new JLabel("Club: "); 
    private JLabel tshirtLabel = new JLabel("Tshirt: "); 
    
    private JComboBox tshirt = new JComboBox(shirtflag); 

	
	public AddDialog(Frame owner){              
		
		super(owner, true);        
		
		Container contentPane = getContentPane();        
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));         
		
		eliteRadioButton = new JRadioButton("Elite Runner"); 
		motionRadioButton = new JRadioButton("Motion Runner");       
		eliteRadioButton.setSelected(true); 		
		contentPane.add(eliteRadioButton);  
		contentPane.add(motionRadioButton); 		       
		        
		ButtonGroup bGroup = new ButtonGroup();       
		bGroup.add(eliteRadioButton);        
		bGroup.add(motionRadioButton);  
		
		JButton btnSave = new JButton("Save"); 
		JButton btnCancel = new JButton("Cancel"); 
		
		panel2.add(btnSave);
		panel2.add(btnCancel);
		
		btnSave.addActionListener(new ActionListener() 
		{  
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				setVisible(false);                
				dispose();
			}       
		});  		
		
		btnCancel.addActionListener(new ActionListener() 
		{  
			public void actionPerformed(ActionEvent e) {				             
				setVisible(false);
				dispose();
			}       
		});   
		
		
		contentPane.add(panel2);  		
		pack();   
	}     

	

}

Open in new window

0
objectsCommented:
add the rest to a 2nd panel

                panel1.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));        
                // add your form to panel1

                contentPane.setLayout(new BorderLayout());
                contentPane.add(BorderLayout.CENTER, panel1);
                contentPane.add(BorderLayout.SOUTH, panel2);
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

MickeysAuthor Commented:
Borderlayout cant be shared exception
package p3;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Frame;
import java.awt.event.*;
import javax.swing.*;


public class AddDialog extends JDialog
{    

	private static final long serialVersionUID = 1L;
	private JRadioButton eliteRadioButton;    
	private JRadioButton motionRadioButton;   
	
	private JPanel panel1 = new JPanel();
	private JPanel panel2 = new JPanel();
	
	private String[] shirtflag = { "Yes", "No"}; 
    private String eliteString = "Elite"; 
    private String motionString = "Motion"; 
     
    private JTextField name = new JTextField(); 
    private JTextField time = new JTextField(); 
    private JTextField startnr = new JTextField(); 
    private JTextField licensnr = new JTextField(); 
    private JTextField club = new JTextField(); 
     
    private JLabel nameLabel = new JLabel("Namn: "); 
    private JLabel timeLabel = new JLabel("Time: "); 
    private JLabel startnrLabel = new JLabel("Start nr: "); 
    private JLabel licensnrLabel = new JLabel("Licens nr: "); 
    private JLabel clubLabel = new JLabel("Club: "); 
    private JLabel tshirtLabel = new JLabel("Tshirt: "); 
    
    private JComboBox tshirt = new JComboBox(shirtflag); 

	
	public AddDialog(Frame owner){              
		
		super(owner, true);  
		
		
		Container contentPane = getContentPane();        
		//contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));         
		
		panel1.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));         
		
		
		eliteRadioButton = new JRadioButton("Elite Runner"); 
		motionRadioButton = new JRadioButton("Motion Runner");       
		eliteRadioButton.setSelected(true); 		
		panel1.add(eliteRadioButton);  
		panel1.add(motionRadioButton); 	
		
		        
		ButtonGroup bGroup = new ButtonGroup();       
		bGroup.add(eliteRadioButton);        
		bGroup.add(motionRadioButton);  
		
		
		
		JButton btnSave = new JButton("Save"); 
		JButton btnCancel = new JButton("Cancel"); 
		
		panel2.add(btnSave);
		panel2.add(btnCancel);
		
		btnSave.addActionListener(new ActionListener() 
		{  
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				setVisible(false);                
				dispose();
			}       
		});  		
		
		btnCancel.addActionListener(new ActionListener() 
		{  
			public void actionPerformed(ActionEvent e) {				             
				setVisible(false);
				dispose();
			}       
		});   
		
        

        contentPane.setLayout(new BorderLayout());
        contentPane.add(BorderLayout.CENTER, panel1);
        contentPane.add(BorderLayout.SOUTH, panel2);
		 		
		pack();   
	}     

	

}

Open in new window

0
objectsCommented:
>                 panel1.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));        

sorry that should have been

                panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));        
0
MickeysAuthor Commented:
What about this?

And also How can I set a title to the dialog box?
package p3;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;


public class AddDialog extends JDialog
{    

	private static final long serialVersionUID = 1L;
	private JRadioButton eliteRadioButton;    
	private JRadioButton motionRadioButton;   
	
	private JPanel panel1 = new JPanel();
	private JPanel panel2 = new JPanel();
	private JPanel panel3 = new JPanel();
	
	private String[] shirtflag = { "Yes", "No"}; 
     
    private JTextField name = new JTextField(); 
    private JTextField time = new JTextField(); 
    private JTextField startnr = new JTextField(); 
    private JTextField licensnr = new JTextField(); 
    private JTextField club = new JTextField(); 
     
    private JLabel nameLabel = new JLabel("Namn: "); 
    private JLabel timeLabel = new JLabel("Time: "); 
    private JLabel startnrLabel = new JLabel("Start nr: "); 
    private JLabel licensnrLabel = new JLabel("Licens nr: "); 
    private JLabel clubLabel = new JLabel("Club: "); 
    private JLabel tshirtLabel = new JLabel("Tshirt: "); 
    
    private JComboBox tshirt = new JComboBox(shirtflag); 

	
	public AddDialog(Frame owner){              
		
		super(owner, true);
		
		Container contentPane = getContentPane(); 		         
		
		//RadioButtons
		eliteRadioButton = new JRadioButton("Elite Runner"); 
		motionRadioButton = new JRadioButton("Motion Runner");       
		eliteRadioButton.setSelected(true); 		
		panel3.add(eliteRadioButton);
		panel3.add(motionRadioButton);
		
		ButtonGroup bGroup = new ButtonGroup();       
		bGroup.add(eliteRadioButton);        
		bGroup.add(motionRadioButton);  			
		
		motionRadioButton.addActionListener(new ActionListener() 
		{  
			public void actionPerformed(ActionEvent e) {
				name.setVisible(true); 
			    time.setVisible(true); 
			    startnr.setVisible(true);
			    licensnr.setEnabled(false);
			    club.setEnabled(false); 
			    nameLabel.setVisible(true); 
			    timeLabel.setVisible(true); 
			    startnrLabel.setVisible(true); 
			    licensnrLabel.setVisible(true); 
			    clubLabel.setVisible(true); 
			    tshirtLabel.setVisible(true); 
			    tshirt.setEnabled(true); 
			}       
		}); 
		
		eliteRadioButton.addActionListener(new ActionListener() 
		{  
			public void actionPerformed(ActionEvent e) {
				name.setVisible(true); 
			    time.setVisible(true); 
			    startnr.setVisible(true);
			    licensnr.setVisible(true);
			    club.setVisible(true); 
			    nameLabel.setVisible(true); 
			    timeLabel.setVisible(true); 
			    startnrLabel.setVisible(true); 
			    licensnrLabel.setVisible(true); 
			    clubLabel.setVisible(true); 
			    tshirtLabel.setVisible(true); 
			    tshirt.setEnabled(false); 
			}       
		}); 
		
		//TextLabels + Fields
		panel1.setLayout(new GridLayout(0,2));  
		panel1.add(nameLabel);
		panel1.add(name);
		panel1.add(timeLabel);
		panel1.add(time);
		panel1.add(startnrLabel);
		panel1.add(startnr);
		panel1.add(licensnrLabel);
		panel1.add(licensnr);
		panel1.add(clubLabel);
		panel1.add(club);
		tshirt.setEnabled(false);
		panel1.add(tshirtLabel);
		panel1.add(tshirt);
				
		//Buttons
		JButton btnSave = new JButton("Save"); 
		JButton btnCancel = new JButton("Cancel"); 
		
		panel2.add(btnSave);
		panel2.add(btnCancel);
		
		btnSave.addActionListener(new ActionListener() 
		{  
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				setVisible(false);                
				dispose();
			}       
		});  		
		
		btnCancel.addActionListener(new ActionListener() 
		{  
			public void actionPerformed(ActionEvent e) {				             
				setVisible(false);
				dispose();
			}       
		});   
		
        

        contentPane.setLayout(new BorderLayout());   
        contentPane.add(BorderLayout.NORTH, panel3);
        contentPane.add(BorderLayout.CENTER, panel1);
        contentPane.add(BorderLayout.SOUTH, panel2);
		 		
		pack();   
	}     

	
//	panel1.add(tshirtLabel);
//	tshirt.setEnabled(false);
//	tshirt.setVisible(false);
//	panel1.add(tshirt);
	
	
}

Open in new window

0
ksivananthCommented:
check this e.g., with Border and GridBag layouts,

public class TestGridbaglayout{
      
      private JPanel buildComponent(){
            JPanel pnlMain = new JPanel( new GridBagLayout() ) ;
            
            //30%
            pnlMain.add( new JButton( "Button 1" ), new GridBagConstraints( 0, 0, 1, 1, 0.3, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            //70%
            pnlMain.add( new JButton( "Button 2" ), new GridBagConstraints( 1, 0, 1, 1, 1.0, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            //None - only component display area
            pnlMain.add( new JButton( "Button 3" ), new GridBagConstraints( 2, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            
            //50%
            pnlMain.add( new JButton( "Button 1" ), new GridBagConstraints( 0, 1, 1, 1, 1.0, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            //50%
            pnlMain.add( new JButton( "Button 2" ), new GridBagConstraints( 1, 1, 1, 1, 1.0, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            //None - only component display area
            pnlMain.add( new JButton( "Button 3" ), new GridBagConstraints( 2, 1, 1, 1, 0.0, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            
            
            //~35%
            pnlMain.add( new JButton( "Button 1" ), new GridBagConstraints( 0, 2, 1, 1, 1.0, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            //~30%
            pnlMain.add( new JButton( "Button 2" ), new GridBagConstraints( 1, 2, 1, 1, 0.3, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            //~35%
            pnlMain.add( new JButton( "Button 3" ), new GridBagConstraints( 2, 2, 1, 1, 1.0, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            
            //~35%
            pnlMain.add( new JButton( "Button 1" ), new GridBagConstraints( 0, 3, 1, 1, 1.0, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            //~30%
            pnlMain.add( new JButton( "Button 2" ), new GridBagConstraints( 1, 3, 2, 1, 0.3, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );

            
            //~35%
            pnlMain.add( new JButton( "Button 1" ), new GridBagConstraints( 0, 4, 2, 1, 1.0, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            //~30%
            pnlMain.add( new JButton( "Button 2" ), new GridBagConstraints( 2, 4, 1, 1, 0.3, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            
            //~35%
            pnlMain.add( new JButton( "Button 1" ), new GridBagConstraints( 0, 5, 2, 1, 1.0, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            //~30%
            pnlMain.add( new JButton( "Button 2" ), new GridBagConstraints( 2, 5, 1, 1, 0.3, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ) );

            return pnlMain ;
      }
      
      /**
       * @param args
       */
      public static void main( String[] args ){
            TestGridbaglayout test = new TestGridbaglayout() ;
        JFrame frame = new JFrame("TestGridbaglayout");
       
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        });
 
        frame.getContentPane().add( test.buildComponent(), BorderLayout.CENTER);
        frame.setSize( 400, 400 ) ;
//        frame.pack();
        frame.setVisible(true);
      }
      
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.