Jump to content

Java Gui JFrame & GridLayout

tp95112

Hello, I am trying to create a gui that looks like this in Java. I cant get the JLabels and JFieldText to align to the right or left of the window

39bf3a29bb.JPG

 

but what I am getting is 

233e2deae7.JPG

Here is my code so far,

import java.awt.*;import java.awt.event.*;import javax.swing.*;public class test extends JFrame{	private JTextField eiinput, teleinput , eninput, yowinput;	private JLabel eilabel, telelabel, empnamelabel,yowlabel;	private GridLayout grid;	private JButton buttons[];	private String names[] ={ "Get Record", "Update", "Display Records"};	public test(){	super( "Testing JTextField and JPasswordField" );	Container container = getContentPane();	container.setLayout( new FlowLayout() );	//Input	eilabel = new JLabel( "Exployee Identification");	container.add( eilabel );		eiinput = new JTextField( "",10);	eiinput.setBounds(100,100,100,25);	container.add( eiinput );		telelabel = new JLabel( "Telephone Number");	container.add( telelabel);		teleinput = new JTextField( "",10);	container.add( teleinput );		empnamelabel = new JLabel( "Employee Name");	container.add( empnamelabel);		eninput = new JTextField( "",10);	container.add( eninput);		yowlabel = new JLabel( "Employee Name");	container.add( yowlabel);		yowinput = new JTextField( "",10);	container.add( yowinput);		//buttons	grid = new GridLayout( 2, 3);	container.setLayout( grid );	buttons = new JButton[ names.length ];	for ( int count = 0; count < names.length; count++ ) {		buttons[ count ] = new JButton( names[ count ] );		container.add( buttons[ count ] );		}			setSize(700,250);	setVisible( true );		}	public static void main( String args[] ){	test application = new test();	application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );		}		}

Link to comment
Share on other sites

Link to post
Share on other sites

Have a look at the way you're declaring your GridLayout. The GridLayout constructor takes in rows then columns. So all you have to do is just change the way you're declaring it. 

 

I count 6 rows and 2 columns, so all you have to do is change:

grid = new GridLayout(2, 3);

To...

grid = new GridLayout(6, 2);

For 6 rows and 2 columns. Here's a link to the documentation for GridLayout. https://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html. Also, make sure your class name is capitalized.

 

Hope that helps.

Link to comment
Share on other sites

Link to post
Share on other sites

 

Thank you that helped. Im getting the layout order wrong. Im thinking it will do the JLabel and JTextField as a flowlayout then the buttons will be done as a gridlayout. 

As for the "update" at the center top. I get this issue when using this code:

	title = new JLabel("Update",JLabel.CENTER);	container.add(title,BorderLayout.NORTH);	

57503c63e1.png

Link to comment
Share on other sites

Link to post
Share on other sites

No problem. Just one tip when doing Swing layouts in Java is you need to try and think of how the JPanels nest within one another. When you're adding the "Update" label, you're right about the BorderLayout flag, but it won't work with a grid layout. You can see on the bottom right of the image you provided that "Display Records" has moved to the bottom right-most "cell" in the GridLayout. This is because when you insert items in the grid they fill up from the top left-most cell.

 

So, to solve your problem. Try break the entire UI up into separate panels. It's just a different way of thinking about the problem. If you group common items into separate panels you can build the entire UI from the ground up. Each JPanel can then have it's own layout, and be inserted into other JPanels. Here's a YouTube video that could possibly explain how the JPanels could nest within one another. Full disclosure, not my video, but explains the concept.

 

https://www.youtube.com/watch?v=Q4Vb-Ws7MO8

Link to comment
Share on other sites

Link to post
Share on other sites

 

Thank you I will try that. I have one more question, I used a array to show the buttons in the window. How would I use a actionlistener on each individual button. Im going through notes and all them use get.source which I believe is for whenever any button is clicked. Would I have to somehow do if statements for the string name and then perform a action from there?

buttons[0].addActionListener( handler );buttons[1].addActionListener( handler );buttons[2].addActionListener( handler );
Link to comment
Share on other sites

Link to post
Share on other sites

That's exactly right. I wouldn't use an array personally because it would make it a whole lot more difficult to read and understand. I'd rather have named instances of the button objects. So it would be something like...

btnGetRecord.addActionListener(...);btnUpdateRecord.addActionListener(...);btnDisplayRecord.addActionListener(...);

Then you could just use the object instance in the class to check for equality. So...

public void actionPerformed(ActionEvent e) {	if(e.getSource() == btnGetRecord){		// do something	} else if (e.getSource() == btnUpdateRecord) {		// do something else	} else if (e.getSource() == btnDisplayRecord) {		//do display things	}}

There are however, other ways to write ActionListeners that don't require you using getSource(). I'd advocate for the use of anonymous inner classes personally, those adhere a little more to the single responsibility principle https://en.wikipedia.org/wiki/Single_responsibility_principle. Ultimately, you'd definitely not want a single class that handled all of the actions for everything in your UI, so you could use an anonymous inner class to handle a single buttons event.

 

But I assume you're doing this for a course of some kind, in which case, using getSource would be perfectly fine.

 

EDIT: If you wanted to continue to use an array, then you could just get the text of the button from the source object. So you use the grouped if statements and just check for equality on the text contained on the button as follows. You'd just have to cast it into a JButton to be able to get the text.

public void actionPerformed(ActionEvent e) {	if(((JButton) e.getSource()).getText().equals("button text")){		// do something	}}
Link to comment
Share on other sites

Link to post
Share on other sites

Sorry I havent had time to work on this course assignment. Is it possible for me to just message you privately? I have questions that are about read/writing to a textfile and also that bottom part of the gui. I am using JTextarea but I cannot get it to be under the gridlayout and a bigger size

	output = new JTextArea();	output.setPreferredSize( new Dimension( 200, 200 ) );	container.add(output);

I also have a record.java file that is this

package assignment2;public class Record {private int empId;private String telephone;private String name;private int years_of_Work;	public Record() {		empId = 0;		telephone = null;		name = null;		years_of_Work = 0;	}	public Record(int e, String t, String n, int y) {		empId = e;		telephone = t;		name = n;		years_of_Work = y;	}	public void setEmpId(int e) {		empId = e;	}	public void setName(String n) {		name = n;	}	public void setTelephone(String t) {		telephone = t;	}	public void setYears(int y) {		years_of_Work = y;	}	public int getEmpId() {		return empId;	}	public String getName() {		return name;	}	public String getTelephone() {		return telephone;	}	public int getYears() {		return years_of_Work;	}	public String toString() {		return name + " phone=" + telephone + " years of work=" + years_of_Work + "\n";	}}

I have never been good with classes that has returns/setters and getters. How would I implement this to my main code which in myclass posted on the first post

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×