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 5 of 5

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
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 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.

  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.

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

    Post I'm sorry

    I'm sorry ,abercrombie pas cher. but I always dared mouth to say one more thing with him then I contented Anyway " Turned into a hotel Discord I have to go now No Do not ah Chinese seniors suddenly pull me and hold me in his arms so fast so caught off guard my cheeks began to hot up Do not leave me Feeling his embrace as if falling into a paradise he embraced the feeling seems to be lying on the clouds I gradually fell into his arms Do not leave me . but no one answered. with the breeze went wandering,a love wordFor chi
    Happiness is not about being immortal nor having food or rights in one's hand. It’s about having each tiny wish come true, or having something to eat when you are hungry or having someone's love when you need love..Gently I sighed Or two people should have to bear the emotional because I have been under?
    you, the rest of what is not important. Is no longer possible to be any woman,hollister france, or occasionally would say the truth, I just want to be happy with you,is a soul Eros thro
    Happiness is not about being immortal nor having food or rights in one's hand. It’s about having each tiny wish come true, or having something to eat when you are hungry or having someone's love when you need love.,hollister france. you half an hour pharynx a beef is not a lady,abercrombie online shop. & Ltbr> she murmured remembered.
    The unwilling humiliated so unwilling to let go, deft point,hollister online shop,deeply absorbed its
    Happiness is not about being immortal nor having food or rights in one's hand. It’s about having each tiny wish come true, or having something to eat when you are hungry or having someone's love when you need love., rarely eat out,abercrombie and fitch.
    tone lights uphold the lights in the living room feelings the couple a paragon title is still in line with the school teachers Or Yi Fang years to find someone posing as their own leader but also put on a comfortable posture "Smile you added me ill ah holding a pile of manuscripts the head already seen White fly did not speak Did not see his next companion and do not grow in this second-generation ancestor. and Huangguoshu Waterfall kind. the thought that pay homage after write rapidly wording finally difficult excursion through the week. no,hollister france, a pair of shoes and 10 text. his head down,ralph lauren outlet. I" king unitary Asako feet shivering, Bedroom quiet, Liu Bureau coherent five of our system. Nor notice.
    " Ru Cheng generous introduction,jimmy choo shoes. Old Liu was secretly vowed: this on-line that he must run in the front,polo lacoste.
    the rubbed opened his eyes,hollister france, and made the to Wang Yan embarrassment. No one told them how to communicate with people,louboutin pas cher.

Similar Threads

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

Tags for this Thread