The Artima Developer Community
Sponsored Link

Java Answers Forum
Setting Locations

9 replies on 1 page. Most recent reply: Oct 24, 2005 11:53 PM by Kondwani Mkandawire

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 9 replies on 1 page
Serj Mavis

Posts: 8
Nickname: peb
Registered: Oct, 2005

Setting Locations Posted: Oct 23, 2005 4:04 AM
Reply to this message Reply
Advertisement
How can I set my JLabel button to display in the top left hand side of the screen, instead of using null to center it?

JLabel label = new JLabel("Add a flight");
label.setFont(new Font("Arial", Font.PLAIN, 14));
getContentPane().add(label);
setSize(800, 500);
setLocationRelativeTo(null);
setVisible(true);


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Setting Locations Posted: Oct 23, 2005 10:47 PM
Reply to this message Reply
I believe the coordinates for the top left are (0,0);

So I guess for the component you will say:

someComponent.setLocation(0, 0);

Serj Mavis

Posts: 8
Nickname: peb
Registered: Oct, 2005

Re: Setting Locations Posted: Oct 24, 2005 12:42 AM
Reply to this message Reply
I tried that but it would'nt seem to work. This is my first time i've used Swing, so if any one could help me get my menu bar and texts alligned, It would be appreciated. I want to make the toolbar alligned to the left, and have the "aff flight" jlabel just below that.

(sorry about the sloppy coding)..I hope its not too unreadable.

import java.awt.*;
import java.awt.Frame;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.undo.*;
import javax.swing.*;
import javax.swing.border.*;

public class TravelBookingSystemFrame extends JFrame {
JPanel contentPane;
BorderLayout borderLayout = new BorderLayout();
JToolBar toolBar = new JToolBar();
JButton button1 = new JButton();
JButton button2 = new JButton();
JButton button3 = new JButton();
JButton button4 = new JButton();
JButton button5 = new JButton();
JButton button6 = new JButton();
ImageIcon image1;
ImageIcon image2;
ImageIcon image3;
ImageIcon image4;
ImageIcon image5;
ImageIcon image6;

// define the menu related objects
JMenuBar jMenuBar = new JMenuBar();
JMenu jMenuFile = new JMenu();
JMenuItem jMenuItemOpen = new JMenuItem();
JMenuItem jMenuItemSave = new JMenuItem();
JMenuItem jMenuItemSaveAs= new JMenuItem();
JMenuItem jMenuItemCopy = new JMenuItem();
JMenuItem jMenuItemExit = new JMenuItem();
JMenu jMenuEdit = new JMenu();
JMenuItem jMenuItemFont = new JMenuItem();
JMenu jMenuView = new JMenu();
JMenu jMenuHelp = new JMenu();
JMenuItem jMenuItemAbout = new JMenuItem();
JFileChooser chooser = new JFileChooser();
JLabel statusLine = new JLabel();

JTextArea textArea;
JScrollPane textScroller;
String currentFile = "";

/**
* Constructor for objects of class TravelBookingSystemFrame
*
*/
public TravelBookingSystemFrame() {
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(borderLayout);
this.setSize(new Dimension(800, 500));
this.setTitle("Travel Booking System");




// set up the tool bar buttons
try {
image1 = new ImageIcon(TravelBookingSystemFrame.class.getResource("addFlight.gif"));
button1.setIcon(image1);
}
catch(Exception e) {
button1.setText("Add a Flight");
}

try {
image2 = new ImageIcon(TravelBookingSystemFrame.class.getResource("addMotel.gif"));
button2.setIcon(image2);
}
catch(Exception e) {
button2.setText("Add a Motel");
}

try {
image3 = new ImageIcon(TravelBookingSystemFrame.class.getResource("addCar.gif"));
button3.setIcon(image3);
}
catch(Exception e) {
button3.setText("Add a Car");
}

try {
image4 = new ImageIcon(TravelBookingSystemFrame.class.getResource("addDayTrip.gif"));
button4.setIcon(image4);
}
catch(Exception e) {
button4.setText("Add a DayTrip");
}


try {
image5 = new ImageIcon(TravelBookingSystemFrame.class.getResource("addResturant.gif"));
button5.setIcon(image5);
}
catch(Exception e) {
button5.setText("Add a Resturant");
}

try {
image6 = new ImageIcon(TravelBookingSystemFrame.class.getResource("epd.gif"));
button6.setIcon(image6);
}
catch(Exception e) {
button6.setText("Enter Personal Details");
}
// set the tool tips for the tool bar buttons
button1.setToolTipText("Adds a flight booking");
button2.setToolTipText("Adds a motel booking");
button3.setToolTipText("Adds a car booking");
button4.setToolTipText("Adds a day trip booking");;
button5.setToolTipText("Adds a resturant booking");
button6.setToolTipText("Enter your personal details");

// setup the tool bar
toolBar.add(button1);
toolBar.add(button2);
toolBar.add(button3);
toolBar.add(button4);
toolBar.add(button5);
toolBar.add(button6);
// now create the menu bar
// The file menu
jMenuFile.setText("File");
jMenuItemOpen.setText("Open");
jMenuItemSave.setText("Save");
jMenuItemSaveAs.setText("Save As...");
jMenuItemCopy.setText("copy");
jMenuItemExit.setText("Exit");
jMenuFile.add(jMenuItemOpen);
jMenuFile.add(jMenuItemSave);
jMenuFile.add(jMenuItemSaveAs);
jMenuFile.add(jMenuItemCopy);
jMenuFile.add(jMenuItemExit);
// The Edit Menu
jMenuEdit.setText("Edit");
jMenuItemFont.setText("Font");
jMenuEdit.add(jMenuItemFont);
// The View Menu
jMenuView.setText("View");
// The Help menu
jMenuHelp.setText("Help");
jMenuItemAbout.setText("About");
jMenuHelp.add(jMenuItemAbout);
// add menu object to menu bar
jMenuBar.add(jMenuFile);
jMenuBar.add(jMenuEdit);
jMenuBar.add(jMenuView);
jMenuBar.add(jMenuHelp);
// set the menu bar to jMenuBar
this.setJMenuBar(jMenuBar);
contentPane.add(toolBar, BorderLayout.NORTH);
contentPane.add(statusLine, BorderLayout.SOUTH);





//Sets Content Pane.
Insets insets=contentPane.getInsets();
//JLabel - Add a flight.
//New FlowLayout.
getContentPane().setLayout(new FlowLayout());
JLabel label = new JLabel("Add a Flight");
label.setFont(new Font("Helvetica", Font.PLAIN, 14));
getContentPane().add(label);
setLocationRelativeTo(null);
setVisible(true);
//Sets label location.
label.setBounds(20+ insets.left, 55+ insets.top,200, 20);//JLabel.
//Makes a new container.
Container contentPane=getContentPane();
contentPane.setLayout(null);

//JLabel - Flight Option
getContentPane().setLayout(new FlowLayout());
label = new JLabel("Flight Option");
label.setFont(new Font("Helvetica", Font.PLAIN, 12));
getContentPane().add(label);
setLocationRelativeTo(null);
setVisible(true);
//Sets label location.
label.setBounds(20+ insets.left, 85+ insets.top,200, 20);//JLabel.
//Makes a new container.
contentPane.setLayout(null);

//Makes the two radio buttons.
JRadioButton returnTrip=new JRadioButton("Return");
JRadioButton oneWay=new JRadioButton("One Way");
//Sets up the Radio Buttons.
ButtonGroup typebutton=new ButtonGroup();
typebutton.add(returnTrip);
typebutton.add(oneWay);
//Adds Radio Buttons.
contentPane.add(returnTrip);
contentPane.add(oneWay);
//Sets Radio buttons locations.
returnTrip.setBounds(120+ insets.left, 115+ insets.top,200, 20);//Radio button.
oneWay.setBounds(30+ insets.left, 115+ insets.top,200, 20);//Radio button.

}
}

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Setting Locations Posted: Oct 24, 2005 1:21 AM
Reply to this message Reply
Please re-post your code encampused in the appropriate
java tags
 \
 Your code \
as suggested
in the "Formatting your post" section. Code in Java
Style with appropriate Indentitions dependent on scope.

http://www.artima.com/forums/howtopost.html

I can't look through untidy code, its hard on the eyes
and takes up too much time.

Serj Mavis

Posts: 8
Nickname: peb
Registered: Oct, 2005

Re: Setting Locations Posted: Oct 24, 2005 1:43 AM
Reply to this message Reply
import java.awt.*;
import java.awt.Frame;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.undo.*;
import javax.swing.*;
import javax.swing.border.*;
 
public class TravelBookingSystemFrame extends JFrame {
JPanel contentPane;
BorderLayout borderLayout = new BorderLayout();
JToolBar toolBar = new JToolBar();
JButton button1 = new JButton();
JButton button2 = new JButton();
JButton button3 = new JButton();
JButton button4 = new JButton();
JButton button5 = new JButton();
JButton button6 = new JButton();
ImageIcon image1;
ImageIcon image2;
ImageIcon image3;
ImageIcon image4;
ImageIcon image5;
ImageIcon image6;
 
// define the menu related objects
JMenuBar jMenuBar = new JMenuBar();
JMenu jMenuFile = new JMenu();
JMenuItem jMenuItemOpen = new JMenuItem();
JMenuItem jMenuItemSave = new JMenuItem();
JMenuItem jMenuItemSaveAs= new JMenuItem();
JMenuItem jMenuItemCopy = new JMenuItem();
JMenuItem jMenuItemExit = new JMenuItem();
JMenu jMenuEdit = new JMenu();
JMenuItem jMenuItemFont = new JMenuItem();
JMenu jMenuView = new JMenu();
JMenu jMenuHelp = new JMenu();
JMenuItem jMenuItemAbout = new JMenuItem();
JFileChooser chooser = new JFileChooser();
JLabel statusLine = new JLabel();
 
JTextArea textArea;
JScrollPane textScroller;
String currentFile = "";
 
/**
* Constructor for objects of class TravelBookingSystemFrame
*
*/
public TravelBookingSystemFrame() {
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(borderLayout);
this.setSize(new Dimension(800, 500));
this.setTitle("Travel Booking System");
 
 
 
 
// set up the tool bar buttons
try {
image1 = new ImageIcon(TravelBookingSystemFrame.class.getResource("addFlight.gif"));
button1.setIcon(image1);
}
catch(Exception e) {
button1.setText("Add a Flight");
}
 
try {
image2 = new ImageIcon(TravelBookingSystemFrame.class.getResource("addMotel.gif"));
button2.setIcon(image2);
}
catch(Exception e) {
button2.setText("Add a Motel");
}
 
try {
image3 = new ImageIcon(TravelBookingSystemFrame.class.getResource("addCar.gif"));
button3.setIcon(image3);
}
catch(Exception e) {
button3.setText("Add a Car");
}
 
try {
image4 = new ImageIcon(TravelBookingSystemFrame.class.getResource("addDayTrip.gif"));
button4.setIcon(image4);
}
catch(Exception e) {
button4.setText("Add a DayTrip");
}
 
 
try {
image5 = new ImageIcon(TravelBookingSystemFrame.class.getResource("addResturant.gif"));
button5.setIcon(image5);
}
catch(Exception e) {
button5.setText("Add a Resturant");
}
 
try {
image6 = new ImageIcon(TravelBookingSystemFrame.class.getResource("epd.gif"));
button6.setIcon(image6);
}
catch(Exception e) {
button6.setText("Enter Personal Details");
}
// set the tool tips for the tool bar buttons
button1.setToolTipText("Adds a flight booking");
button2.setToolTipText("Adds a motel booking");
button3.setToolTipText("Adds a car booking");
button4.setToolTipText("Adds a day trip booking");;
button5.setToolTipText("Adds a resturant booking");
button6.setToolTipText("Enter your personal details");
 
// setup the tool bar
toolBar.add(button1);
toolBar.add(button2);
toolBar.add(button3);
toolBar.add(button4);
toolBar.add(button5);
toolBar.add(button6);
// now create the menu bar
// The file menu
jMenuFile.setText("File");
jMenuItemOpen.setText("Open");
jMenuItemSave.setText("Save");
jMenuItemSaveAs.setText("Save As...");
jMenuItemCopy.setText("copy");
jMenuItemExit.setText("Exit");
jMenuFile.add(jMenuItemOpen);
jMenuFile.add(jMenuItemSave);
jMenuFile.add(jMenuItemSaveAs);
jMenuFile.add(jMenuItemCopy);
jMenuFile.add(jMenuItemExit);
// The Edit Menu
jMenuEdit.setText("Edit");
jMenuItemFont.setText("Font");
jMenuEdit.add(jMenuItemFont);
// The View Menu
jMenuView.setText("View");
// The Help menu
jMenuHelp.setText("Help");
jMenuItemAbout.setText("About");
jMenuHelp.add(jMenuItemAbout);
// add menu object to menu bar
jMenuBar.add(jMenuFile);
jMenuBar.add(jMenuEdit);
jMenuBar.add(jMenuView);
jMenuBar.add(jMenuHelp);
// set the menu bar to jMenuBar
this.setJMenuBar(jMenuBar);
contentPane.add(toolBar, BorderLayout.NORTH);
contentPane.add(statusLine, BorderLayout.SOUTH);
 
 
 
 
 
//Sets Content Pane.
Insets insets=contentPane.getInsets();
//JLabel - Add a flight.
//New FlowLayout.
getContentPane().setLayout(new FlowLayout());
JLabel label = new JLabel("Add a Flight");
label.setFont(new Font("Helvetica", Font.PLAIN, 14));
getContentPane().add(label);
setLocationRelativeTo(null);
setVisible(true);
//Sets label location.
label.setBounds(20+ insets.left, 55+ insets.top,200, 20);//JLabel.
//Makes a new container.
Container contentPane=getContentPane();
contentPane.setLayout(null);
 
//JLabel - Flight Option
getContentPane().setLayout(new FlowLayout());
label = new JLabel("Flight Option");
label.setFont(new Font("Helvetica", Font.PLAIN, 12));
getContentPane().add(label);
setLocationRelativeTo(null);
setVisible(true);
//Sets label location.
label.setBounds(20+ insets.left, 85+ insets.top,200, 20);//JLabel.
//Makes a new container.
contentPane.setLayout(null);
 
//Makes the two radio buttons.
JRadioButton returnTrip=new JRadioButton("Return");
JRadioButton oneWay=new JRadioButton("One Way");
//Sets up the Radio Buttons.
ButtonGroup typebutton=new ButtonGroup();
typebutton.add(returnTrip);
typebutton.add(oneWay);
//Adds Radio Buttons.
contentPane.add(returnTrip);
contentPane.add(oneWay);
//Sets Radio buttons locations.
returnTrip.setBounds(120+ insets.left, 115+ insets.top,200, 20);//Radio button.
oneWay.setBounds(30+ insets.left, 115+ insets.top,200, 20);//Radio button.
 
}
}

Serj Mavis

Posts: 8
Nickname: peb
Registered: Oct, 2005

Re: Setting Locations Posted: Oct 24, 2005 2:11 AM
Reply to this message Reply
Just to make it a bit clearer, heres the basic design i was after.

http://i2.photobucket.com/albums/y29/koas/UI.jpg

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Setting Locations Posted: Oct 24, 2005 2:22 AM
Reply to this message Reply
Reposting the code means using the tags and copying the code from your IDE, not from the html site.

What you posted still isn't easyly readabele.

You are using a border layout.

Did you read the description of border layout?

If you want to set a position you must use NullLayout or AbsoluteLayout.

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Setting Locations Posted: Oct 24, 2005 6:03 AM
Reply to this message Reply
   public class SomePanel extends JPanel{
   	   private JRadioButton button1;
   	   private JRadioButton button2;
   	   
   	   private JTextField field1;
   	   private JTextField field2;
   	   
   	   public SomePanel(){
   	   	  setLayout(new java.awt.BorderLayout());
   	   	  int no_of_radio_buttons = 2; // atleast in this example
   	      JPanel northPanel = new JPanel();
   	      //  or use FlowLayout to place buttons from left to right
   	      northPanel.setLayout(new GridLayout(1, no_of_radio_buttons));
   	      
   	      button1 = new JRadioButton("CLICK 1");
   	      button2 = new JRadioButton("CLICK 2");
   	       
   	      northPanel.add(button1);
   	      northPanel.add(button2); 
   	      
   	      JPanel centralPanel = new JPanel();
   	      //  note: someLayoutManagerForYourFields is a fictional var
   	      centralPanel.setLayout(someLayoutManagerForYourFields);
   	      
   	      textField1 = new JTextField(12);
   	      textField2 = new JTextField(12);
   	      centralPanel.add(textField1);
   	      centralPanel.add(textField2);
   	      
   	      add(northPanel, java.awt.BorderLayout.NORTH);
   	      add(centralPanel, java.awt.BorderLayout.CENTER);
   	   }
   }
   
   //  In your frame/JDialog/whatever you have just simply do:
   //  getContentPane().add(new SomePanel());
   //      *******  all the Layout settings have been done within the SomePanel class
 
   
   

Serj Mavis

Posts: 8
Nickname: peb
Registered: Oct, 2005

Re: Setting Locations Posted: Oct 24, 2005 7:16 AM
Reply to this message Reply
Thaks a lot. Just one problem i'm having; I added the getContentPane().add(new SomePanel()); into my frame and call the class you created, but nothing different happens when I run my interface. Do I need to edit my code in the frame at all since im re-setting the layout settings?

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Setting Locations Posted: Oct 24, 2005 11:53 PM
Reply to this message Reply
> Thaks a lot. Just one problem i'm having; I added the
> getContentPane().add(new SomePanel()); into
> my frame and call the class you created, but nothing
> different happens when I run my interface. Do I need to
> edit my code in the frame at all since im re-setting the
> layout settings?


LOL! LOL! LOL! (lmao), that's actually pretty hilarious.
That used to happen to me so often when I first started
messing with Swing, I'd sit there for hours on end trying
to figure it out. If you don't have this line of code and
have not made your size settings, it means you have created
a frame but its just not visible. Make sure you have
included the following lines

If your class is extending Frame or Dialog add:
setSize(350, 450); // or whatever dimensions you want
setVisible(true);  //  This is the important one

If your Frame is created within some other class then:
frame.setSize(350, 450);
frame.setVisible(true);

Something to that effect.

Flat View: This topic has 9 replies on 1 page
Topic: JAva Application and JDBC Oracle 10g connection problem Previous Topic   Next Topic Topic: Eclipse won't  build/run because it's

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use