Click to See Complete Forum and Search --> : centering a GUI


The One
02-16-2006, 03:01 PM
wen i create a GUI it always starts in the top left how do i make it start in the centre of the screen??

Khalid Ali
02-16-2006, 09:04 PM
you can get the screen size and then from there calculate the middle point and position ur GUI window there... I have a utility function already written, u can use it, u just need to play with the numbers so that u can bring it to center.Here are things to rememebr
Dividing screen size with 2 will give you the center, however if u set that then your window will start from center ....here is the code


/**
* Sets the location of window to open in the screen
*/
private void setGuiObjectsLocation() {

double d_locW = .7364; //the caller id should be this many % from left
double d_locH = .7035; //the caller id should be this many % from top

Dimension dimension = tk.getScreenSize();

int height = dimension.height;
int width = dimension.width;

height = (int) (height * d_locH);
width = (int) (width * d_locW);
this.setLocation(width, height);//sets the location almost right bottom
//where this refers to the current frame.
}

The One
02-19-2006, 06:24 AM
ya where abouts to i put that following code. cos it said illegal start of expression???

Khalid Ali
02-19-2006, 10:06 AM
put it in your GUI class that creates the Frame

The One
02-19-2006, 10:44 AM
i did like u said in the following but it still says illegal start of expression?? below is the code:

public static void main (String [] args) {

int Button_width = 65;
int Button_height = 25;

JButton Namebutton = new JButton("Name");
Namebutton.setPreferredSize (new Dimension (Button_width, Button_height));

JButton Salarybutton = new JButton("Salary Sheet");
Salarybutton.setPreferredSize (new Dimension (Button_width, Button_height));

//Add the date to the program
Date toDay = new Date();
JLabel DateLabel = new JLabel ("Date: " + toDay);


final JFrame frame = new JFrame ("Salary Sheet");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

private void setGuiObjectsLocation() {
double d_locW = .7364;
double d_locH = .7035;

Dimension dimension = tk.getScreenSize();

int height = dimension.height;
int width = dimension.width;

height = (int) (height * d_locH);
width = (int) (width * d_locW);
this.setLocation (width, height);
}



frame.setLayout (new GridLayout (3,1));

frame.add (Namebutton);
frame.add (Salarybutton);
frame.add (DateLabel);

frame.pack ();
frame.show ();
}
}

Khalid Ali
02-19-2006, 11:30 AM
illegalstartof expression generally means you are missing or have an extra bracket somewhere. Pay good attention at ur code and see if every curly bracket or parenthesis that is opened is closed appropriately.

The One
02-19-2006, 11:50 AM
i did as u said and everything is alright. before i added that Screen positioner the program works but after it doesnt.
it keeps sayin illegal start of expression pointing at private void........

wats wrong?
where does the curly bracket at the end of the screen positioner go. does it go straight after it?
HELP

Khalid Ali
02-19-2006, 04:25 PM
i did as u said and everything is alright. before i added that Screen positioner the program works but after it doesnt.
it keeps sayin illegal start of expression pointing at private void........

wats wrong?
.......
HELP

Obviously you did not do everything right otherwise you would not have this problem...post the full code and I will take a look at it.

The One
02-20-2006, 07:32 AM
public static void main (String [] args) {

int Button_width = 65;
int Button_height = 25;

JButton Namebutton = new JButton("Name");
Namebutton.setPreferredSize (new Dimension (Button_width, Button_height));

JButton Salarybutton = new JButton("Salary Sheet");
Salarybutton.setPreferredSize (new Dimension (Button_width, Button_height));

//Add the date to the program
Date toDay = new Date();
JLabel DateLabel = new JLabel ("Date: " + toDay);


final JFrame frame = new JFrame ("Salary Sheet");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

private void setGuiObjectsLocation() {
double d_locW = .7364;
double d_locH = .7035;

Dimension dimension = tk.getScreenSize();

int height = dimension.height;
int width = dimension.width;

height = (int) (height * d_locH);
width = (int) (width * d_locW);
this.setLocation (width, height);
}



frame.setLayout (new GridLayout (3,1));

frame.add (Namebutton);
frame.add (Salarybutton);
frame.add (DateLabel);

frame.pack ();
frame.show ();
}
}

Khalid Ali
02-20-2006, 09:01 AM
I said complete code, you have just posted here main methods code. And if this is it then its wrong completely cuse you need a class as well that may have a main method in it.

The One
02-20-2006, 10:01 AM
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

//Salary Sheet GUI
public class main {
public static void Salary () {

int Button_width = 65;
int Button_height = 25;




//Total Gross Salary
JLabel BasicSalaryLabel = new JLabel ("Basic Salary:");
JLabel LunchAllowLabel = new JLabel ("Lunch Allowance:");
JLabel HousingAllowLabel = new JLabel ("Housing Allowance:");
JLabel TravelAllowLabel = new JLabel ("Travel Allowance:");
JLabel BonusLabel = new JLabel ("Bonus:");
JLabel OvertimeLabel = new JLabel ("Overtime:");
JLabel GrossSalaryLabel = new JLabel ("GROSS SALARY:");
final JTextField BasicSalarytextfield = new JTextField(5);
final JTextField LunchAllowtextfield = new JTextField (5);
final JTextField HousingAllowtextfield = new JTextField (5);
final JTextField TravelAllowtextfield = new JTextField (5);
final JTextField Bonustextfield = new JTextField (5);
final JTextField Overtimetextfield = new JTextField (5);
final JTextField GrossSalarytextfield = new JTextField (5);

//Total Deductions
JLabel AdvanceLabel = new JLabel ("Advance:");
JLabel LoanLabel = new JLabel ("Loan:");
JLabel PAYELabel = new JLabel ("PAYE:");
JLabel NAPSALabel = new JLabel ("NAPSA:");
JLabel TDeductionsLabel = new JLabel ("TOTAL DEDUCTIONS:");
final JTextField Advancetextfield = new JTextField(5);
final JTextField Loantextfield = new JTextField (5);
final JTextField PAYEtextfield = new JTextField (5);
final JTextField NAPSAtextfield = new JTextField (5);
final JTextField TDeductionstextfield = new JTextField (5);

//Final Net Salary
JLabel NetSalaryLabel = new JLabel ("NET SALARY:");
final JTextField NetSalarytextfield = new JTextField (15);

//Buttons
JButton Calcbutton = new JButton("Calculate");
Calcbutton.setPreferredSize (new Dimension (Button_width, Button_height));

JButton Savebutton = new JButton("Save");
Savebutton.setPreferredSize (new Dimension (Button_width, Button_height));

JButton Backbutton = new JButton ("Main Menu");
Backbutton.setPreferredSize (new Dimension (Button_width, Button_height));


int[][] info =
{
{1},
{2},
{3}
};

for( int i = 0; i < info.length; i++ ) {
for( int j = 0; j < info[i].length; j++ ) {





JLabel infoLabel = new JLabel ("" + info[i][j]);
final JTextField infotextfield = new JTextField(5);


//Back button class
class BackButtonListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
SalaryMain();
}
}
ActionListener Back = new BackButtonListener ();
Backbutton.addActionListener (Back);


//Calculate Class
class CalcButtonListener implements ActionListener {

public void actionPerformed (ActionEvent event) {


String BasicSalary1 = BasicSalarytextfield.getText();
String LunchAllow1 = LunchAllowtextfield.getText();
String HousingAllow1 = HousingAllowtextfield.getText();
String TravelAllow1 = TravelAllowtextfield.getText();
String Bonus1 = Bonustextfield.getText();
String Overtime1 = Overtimetextfield.getText();
double BasicSalary2 = Double.parseDouble (BasicSalary1);
double LunchAllow2 = Double.parseDouble (LunchAllow1);
double HousingAllow2 = Double.parseDouble (HousingAllow1);
double TravelAllow2 = Double.parseDouble (TravelAllow1);
double Bonus2 = Double.parseDouble (Bonus1);
double Overtime2 = Double.parseDouble (Overtime1);

double Total1 = (BasicSalary2 + LunchAllow2 + HousingAllow2 + TravelAllow2 + Bonus2 + Overtime2);;
GrossSalarytextfield.setText (new Double(Total1).toString());//store value in Gross Salary text field

double PAYEt = ((Total1) * 30/100);
PAYEtextfield.setText (new Double(PAYEt).toString());//store value in PAYE text field

double NAPSAt = ((Total1) * 5/100);
NAPSAtextfield.setText (new Double(NAPSAt).toString());//store value in NAPSA text field

String Advance1 = Advancetextfield.getText();
String Loan1 = Loantextfield.getText();
String NAPSA1 = NAPSAtextfield.getText();
double Advance2 = Double.parseDouble (Advance1);
double Loan2 = Double.parseDouble (Loan1);
double NAPSA2 = Double.parseDouble (NAPSA1);

double Total2 = (Advance2 + Loan2 + PAYEt + NAPSA2);
TDeductionstextfield.setText (new Double(Total2).toString());//store value in Total Deductions text field

double Totalf = (Total1 - Total2);
NetSalarytextfield.setText (new Double(Totalf).toString());//store value in Net Salary text field

}
};
ActionListener Calc = new CalcButtonListener ();
Calcbutton.addActionListener (Calc);


//Save Class
class SaveButtonListener implements ActionListener {

public void actionPerformed (ActionEvent event) {

try {

FileWriter writer = new FileWriter ("Salary.txt");
PrintWriter out = new PrintWriter (writer);

String AddBasicSalary = BasicSalarytextfield.getText();
String AddLunchAllow = LunchAllowtextfield.getText();
String AddHousingAllow = HousingAllowtextfield.getText();
String AddTravelAllow = TravelAllowtextfield.getText();
String AddBonus = Bonustextfield.getText();
String AddOvertime = Overtimetextfield.getText();
String AddGrossSalary = GrossSalarytextfield.getText();
out.println (AddBasicSalary);
out.println (AddLunchAllow);
out.println (AddHousingAllow);
out.println (AddTravelAllow);
out.println (AddBonus);
out.println (AddOvertime);
out.println (AddGrossSalary);

String AddAdvance = Advancetextfield.getText();
String AddLoan = Loantextfield.getText();
String AddPAYE = PAYEtextfield.getText();
String AddNAPSA = NAPSAtextfield.getText();
String AddTDeductions = TDeductionstextfield.getText();
out.println (AddAdvance);
out.println (AddLoan);
out.println (AddPAYE);
out.println (AddNAPSA);
out.println (AddTDeductions);

String AddNetSalary = NetSalarytextfield.getText();
out.println (AddNetSalary);

out.close();

}
catch (IOException exception) {

System.out.println ("can't write to file");
}
}
};
ActionListener listener = new SaveButtonListener ();
Savebutton.addActionListener (listener);

final JFrame Salary = new JFrame ("Salary Sheet");
Salary.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

Salary.setLayout (new GridLayout (18,2));

//information about employee
Salary.add (infoLabel);
Salary.add (infotextfield);


//Gross salary
Salary.add (BasicSalaryLabel);
Salary.add (BasicSalarytextfield);
Salary.add (LunchAllowLabel);
Salary.add (LunchAllowtextfield);
Salary.add (HousingAllowLabel);
Salary.add (HousingAllowtextfield);
Salary.add (TravelAllowLabel);
Salary.add (TravelAllowtextfield);
Salary.add (BonusLabel);
Salary.add (Bonustextfield);
Salary.add (OvertimeLabel);
Salary.add (Overtimetextfield);

//Total deductions
Salary.add (AdvanceLabel);
Salary.add (Advancetextfield);
Salary.add (LoanLabel);
Salary.add (Loantextfield);
Salary.add (PAYELabel);
Salary.add (PAYEtextfield);
Salary.add (NAPSALabel);
Salary.add (NAPSAtextfield);

//Calculated values
Salary.add (GrossSalaryLabel);
Salary.add (GrossSalarytextfield);
Salary.add (TDeductionsLabel);
Salary.add (TDeductionstextfield);
Salary.add (NetSalaryLabel);
Salary.add (NetSalarytextfield);

//Buttons
Salary.add (Savebutton);
Salary.add (Calcbutton);
Salary.add (Backbutton);

Salary.pack ();
Salary.show ();

}
}
}

//Main Menu GUI
public static void SalaryMain () {

int Button_width = 65;
int Button_height = 25;

JButton Namebutton = new JButton("Name");
Namebutton.setPreferredSize (new Dimension (Button_width, Button_height));

JButton Salarybutton = new JButton("Salary Sheet");
Salarybutton.setPreferredSize (new Dimension (Button_width, Button_height));

//Adds the date to the program
Date toDay = new Date();
JLabel DateLabel = new JLabel ("DATE: " + toDay);


final JFrame frame = new JFrame ("Salary Sheet");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

frame.setLayout (new GridLayout (3,1));






private void setGuiObjectsLocation() {
double d_locW = .7364;
double d_locH = .7035;

Dimension dimension = tk.getScreenSize();

int height = dimension.height;
int width = dimension.width;

height = (int) (height * d_locH);
width = (int) (width * d_locW);
this.setLocation (width, height);
}








frame.add (Namebutton);
frame.add (Salarybutton);
frame.add (DateLabel);

frame.pack ();
frame.show ();



//Salary button listener
class SalaryButtonListener implements ActionListener {

public void actionPerformed (ActionEvent event) {

Salary ();
frame.setVisible (false);

}
}

ActionListener Salary = new SalaryButtonListener ();
Salarybutton.addActionListener (Salary);
}


//Main class
public static void main (String [] args) {
SalaryMain();
}
}

Khalid Ali
02-20-2006, 11:30 AM
thats not very good code, wonder where did u get help to write it.
Anyways...cut the method I posted above for you from where its at and paste it after the following

//Main class
public static void main (String [] args) {
SalaryMain();
}

private void setGuiObjectsLocation() {
double d_locW = .7364;
double d_locH = .7035;

Dimension dimension = tk.getScreenSize();

int height = dimension.height;
int width = dimension.width;

height = (int) (height * d_locH);
width = (int) (width * d_locW);
this.setLocation (width, height);
}

}

The One
02-20-2006, 11:44 AM
wat u mean its not very good code. Any tips.
and i did as u said but it says cannot find symbol tk and cannot find method seLocation
wats wrong?

Khalid Ali
02-20-2006, 12:17 PM
ok thats now is my fault...:-)
missed a line of code there
add the following line in the beginning of the setlocation method

Toolkit tk = Toolkit.getDefaultToolkit();

once you do that, you should know that this. keyword in this method presumes that the class extends JFrame, which in your case is not true, therefore you will have to replace "this" with the frames reference....

The One
02-20-2006, 01:20 PM
i did the code below

//Main class
public static void main (String [] args) {
SalaryMain();
}

private void setGuiObjectsLocation() {
Toolkit SalaryMain = Toolkit.getDefaultToolkit();

double d_locW = .7364;
double d_locH = .7035;

Dimension dimension = SalaryMain.getScreenSize();

int height = dimension.height;
int width = dimension.width;

height = (int) (height * d_locH);
width = (int) (width * d_locW);
SalaryMain.setLocation (width, height);
}
}


i replaced 'this' with SalaryMain as that is what the class is called.
says something bout variable.

whats wrong.

Khalid Ali
02-20-2006, 04:50 PM
no u don't replace it with the class name, rather it should be the frames name

take a look at this line
final JFrame frame = new JFrame ("Salary Sheet");
make this jframe variable a global one which means add the following line at the top of the class after the first curly brace
JFrame frame = null;

then replace this line
final JFrame frame = new JFrame ("Salary Sheet");
with the following
frame = new JFrame ("Salary Sheet");

now replace the "this" keyword in the method with frame

The One
02-21-2006, 08:37 AM
i tried it but nothining: the bit is below:


//Main Menu GUI
public static void SalaryMain () {


JFrame frame = null;


int Button_width = 65;
int Button_height = 25;

JButton Namebutton = new JButton("Name");
Namebutton.setPreferredSize (new Dimension (Button_width, Button_height));

JButton Salarybutton = new JButton("Salary Sheet");
Salarybutton.setPreferredSize (new Dimension (Button_width, Button_height));

//Adds the date to the program
Date toDay = new Date();
JLabel DateLabel = new JLabel ("DATE: " + toDay);


frame = new JFrame ("Salary Sheet");



frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

frame.setLayout (new GridLayout (3,1));

frame.add (Namebutton);
frame.add (Salarybutton);
frame.add (DateLabel);

frame.pack ();
frame.show ();



//Salary button listener
class SalaryButtonListener implements ActionListener {

public void actionPerformed (ActionEvent event) {

Salary ();
frame.setVisible (false);

}
}

ActionListener Salary = new SalaryButtonListener ();
Salarybutton.addActionListener (Salary);
}



//Main class
public static void main (String [] args) {
SalaryMain();
}

private void setGuiObjectsLocation() {
Toolkit tk = Toolkit.getDefaultToolkit();

double d_locW = .7364;
double d_locH = .7035;

Dimension dimension = tk.getScreenSize();

int height = dimension.height;
int width = dimension.width;

height = (int) (height * d_locH);
width = (int) (width * d_locW);
frame.setLocation (width, height);
}
}



whats wrong iv done all u said???

Khalid Ali
02-21-2006, 09:20 AM
u now have to call the method to set the relocation...don't u think?

update the code as such that it look like this

frame.add (Namebutton);
frame.add (Salarybutton);
frame.add (DateLabel);
setGuiObjectsLocation();
frame.pack ();
frame.show ();

The One
02-21-2006, 11:03 AM
sorry for bothering you. but i did everything as u said. but changed where that setlocation code goes to the actuall salarymain class. thats the only way i get it to compile withput any errors but now its no showing anything on the screen???????

code:




//Main Menu GUI
public static void SalaryMain () {
}

JFrame frame = null;
int Button_width = 65;
int Button_height = 25;

private void setGuiObjectsLocation() {
Toolkit tk = Toolkit.getDefaultToolkit();

double d_locW = .7364;
double d_locH = .7035;

Dimension dimension = tk.getScreenSize();

int height = dimension.height;
int width = dimension.width;

height = (int) (height * d_locH);
width = (int) (width * d_locW);
frame.setLocation (width, height);



JButton Namebutton = new JButton("Name");
Namebutton.setPreferredSize (new Dimension (Button_width, Button_height));

JButton Salarybutton = new JButton("Salary Sheet");
Salarybutton.setPreferredSize (new Dimension (Button_width, Button_height));

//Adds the date to the program
Date toDay = new Date();
JLabel DateLabel = new JLabel ("DATE: " + toDay);


frame = new JFrame ("Salary Sheet");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

frame.setLayout (new GridLayout (3,1));

frame.add (Namebutton);
frame.add (Salarybutton);
frame.add (DateLabel);
setGuiObjectsLocation();
frame.pack ();
frame.show ();



//Salary button listener
class SalaryButtonListener implements ActionListener {

public void actionPerformed (ActionEvent event) {

Salary ();


}
}

ActionListener Salary = new SalaryButtonListener ();
Salarybutton.addActionListener (Salary);
}



//Main class
public static void main (String [] args) {
SalaryMain();
}
}

Khalid Ali
02-21-2006, 12:10 PM
Holly .......
take a look

JFrame frame = null;
int Button_width = 65;
int Button_height = 25;

private void setGuiObjectsLocation() {
Toolkit tk = Toolkit.getDefaultToolkit();

double d_locW = .7364;
double d_locH = .7035;

Dimension dimension = tk.getScreenSize();

int height = dimension.height;
int width = dimension.width;

height = (int) (height * d_locH);
width = (int) (width * d_locW);
frame.setLocation (width, height);



its a function and its missing its end bracket.... pay some attention my friend...

now post the whole class here again and I will fix the it once for all

The One
02-21-2006, 12:33 PM
ok i have attached it

Khalid Ali
02-21-2006, 01:20 PM
its extremely hard to read code u have here, anyways....see if this works for u (I hope it does)

The One
02-21-2006, 01:33 PM
Thanks alot man. i would owe u but dont know much java!

Khalid Ali
02-21-2006, 02:14 PM
finally....:-).....
Have fun learning Java..its a good programming language