[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Cant get my dialog work

Posted on 2010-04-09
7
Medium Priority
?
182 Views
Last Modified: 2012-05-09
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

0
Comment
Question by:Mickeys
  • 3
  • 3
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 30187197
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
 

Author Comment

by:Mickeys
ID: 30187698
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
 
LVL 92

Expert Comment

by:objects
ID: 30188668
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:Mickeys
ID: 30190564
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
 
LVL 92

Expert Comment

by:objects
ID: 30192479
>                 panel1.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));        

sorry that should have been

                panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));        
0
 

Author Comment

by:Mickeys
ID: 30192861
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
 
LVL 26

Accepted Solution

by:
ksivananth earned 2000 total points
ID: 30194746
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question