Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to add a title

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to add a title

    Sorry I'm a bit new to using a forum sight. I basically want a JLabel to go in the very top left of the screen but do not want it close to the other components. Should i just make my insets for the JLabel very high numbers ?

    Here is my current code, I know that I have not touched Action and Item Handlers.


     
    package screens;
     
    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Font;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.GridLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
     
    import javax.swing.BorderFactory;
    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;
     
    public class LoginGUI extends JFrame implements ActionListener, ItemListener
    {
     
    	//Global Variables
    	private JTextField uName, pWord;
    	private JButton enter, cancel;
    	private JRadioButton sale, admin;
    	private JLabel title;
    	private Font font = new Font("Trebuchet MS", Font.BOLD, 20);;
    	private ButtonGroup options;
    	private GridBagLayout layout;
    	private GridBagConstraints gbc;
     
    	public LoginGUI()
    	{
     
    		setSize(800,400);
    	  	setTitle("Login");
    	  	setLocationRelativeTo(null);
    	  	setDefaultCloseOperation(EXIT_ON_CLOSE);
     
     
    	  	//setBorder(BorderFactory.createTitledBorder("Header"));
    	  	layout = new GridBagLayout();
    	  	setLayout(layout);
    	  	gbc = new GridBagConstraints();
     
     
    	  	//GUI Components
     
    	  	// User Type Line
    	  	addComponent(new JLabel("User Type:"), 0,0,2,1);
    	  	sale = new JRadioButton("Sale Staff", true);
    	  	admin = new JRadioButton("Admin", false);
     
    	  	options = new ButtonGroup(); //Grouping Buttons
    	  	options.add(sale);
    	  	options.add(admin);
     
    	  	sale.addItemListener(this);
    	  	admin.addItemListener(this);
     
    	  	addComponent(sale,2,0,1,1);
    	  	addComponent(admin,3,0,1,1);
     
     
    	  	//Username Line
    	  	addComponent(new JLabel("Username:"),0,1,2,1);
    	 	uName = new JTextField();
    	  	addComponent(uName,2,1,2,1);
     
    	  	//Password Line
    	  	addComponent(new JLabel("Password:"),0,2,2,1);
    	 	pWord = new JTextField();
    	  	addComponent(pWord,2,2,2,1);
     
    	  	//Buttons
    	  	enter = new JButton("Enter");
    	  	enter.addActionListener(this);
    	  	cancel = new JButton("Cancel");
    	  	cancel.addActionListener(this);
    	  	addComponent(enter,2,3,1,1);
    	  	addComponent(cancel,3,3,1,1);
     
    	  	this.setVisible(true); //Sets visibility when object is created
    	}
     
    	//Method to add a component to GridBagLayout
    	public void addComponent(Component c, int gx, int gy, int gw, int gh)
    			{
    				c.setFont(font);
    			   gbc.gridx = gx;
    			   gbc.gridy = gy;
    			   gbc.gridwidth = gw;
    			   gbc.gridheight = gh;
    			   gbc.weightx = 0;
    			   gbc.weighty = 0;  
    			   gbc.insets = new Insets(5,5,5,5);
    			   gbc.fill = GridBagConstraints.HORIZONTAL;
    			   add(c, gbc);
    			}
     
    	@Override
    	public void actionPerformed(ActionEvent arg0)
    	{
    		// TODO Auto-generated method stub
     
    	}
     
     
    	@Override
    	public void itemStateChanged(ItemEvent arg0)
    	{
    		// TODO Auto-generated method stub
     
    	}
     
     
    	public static void main(String[] args)
    	{
    		LoginGUI gui = new LoginGUI();
     
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to add a title

    The JFrame class has a method to set its title that the code uses. Can you explain what you mean by "title"?

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to add a title

    Done. I hope I am explaining myself properly.

  4. #4
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: How to add a title

    Quote Originally Posted by X00090307 View Post
    Done. I hope I am explaining myself properly.
    You already have a title called Login that you set using the JFrame method, setTitle("Login"). Do you want a JLabel to be in the top left corner but underneath the bar where the title is shown? If so, set the GridBagConstraints gridx and gridy to 0. If that's not what you meant, then can you please elaborate.

Similar Threads

  1. Replies: 5
    Last Post: February 15th, 2013, 05:01 PM
  2. Trying to add a button to my Title Screen...
    By Jnk1296 in forum AWT / Java Swing
    Replies: 5
    Last Post: January 25th, 2013, 11:51 AM
  3. Insert original title here: Hello there!
    By Leetment in forum Member Introductions
    Replies: 0
    Last Post: May 9th, 2012, 02:48 PM
  4. About Title border in jscrollpane
    By subhvi in forum AWT / Java Swing
    Replies: 2
    Last Post: December 15th, 2009, 12:45 AM

Tags for this Thread