I have my two JTextFields identified in a GridLayout, but when I try to reformat them the stay as large as their positioning allows. I have tried to specify the size of the field but it never changes on the final product. Could this be because the GridLayout or something else? I have attached a screen shot so you can see the final product of my program.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.text.*;

class ExampleJComboBoxSample extends JFrame
												implements ActionListener,
												ItemListener
{
private static final int FRAME_WIDTH		=1000;
private static final int FRAME_HEIGHT		=1000;

private JComboBox comboBox1, comboBox2;
private JTextField parentinput, emailinput;

public static void main (String[] args) 
{
	ExampleJComboBoxSample frame1 = new ExampleJComboBoxSample();
	frame1.setVisible(true);
}

public ExampleJComboBoxSample()
 {
Container contentPane1, contentPane2, contentPane3;

JPanel comboPanel, okPanel, headerPanel;

JLabel image1, image2, title1, parent, teacher, child, emaildr;

JButton	saveButton, cancelButton;

String[] teacherBoxItem = {"Choose Teacher...", "Mrs. Thomas", "Mr. Peter", "Mr. Key", "Mrs. Lucky" };
String[] studentBoxItem = {"Choose Student...", "Bobby Jones", "Hailey Johnson", "Sarah Lee", "Carolyn Birdie" };

// set the frame properties
setSize(FRAME_WIDTH,FRAME_HEIGHT);
setTitle ("ExampleJComboBoxSample");

contentPane1 = getContentPane();
contentPane1.setBackground(Color.BLUE);
contentPane1.setLayout(new GridLayout(4,2));

contentPane2 = getContentPane();
contentPane2.setBackground(Color.BLUE);
contentPane2.setLayout(new BorderLayout());

contentPane3 = getContentPane();
contentPane3.setBackground(Color.BLUE);
contentPane3.setLayout(new BorderLayout());

// create and place a header for the frame
headerPanel = new JPanel(new FlowLayout());
image1 = new JLabel(new ImageIcon("/Users/bndandridge/Zion Mascot Image/mascot 2.gif"));
image2 = new JLabel(new ImageIcon("/Users/bndandridge/Zion Mascot Image/mascot 2.gif"));
title1 = new JLabel();
image1.setSize(50,50);
title1.setText("ZION PARENT CONTACT DIRECTORY");
image2.setSize(50,50);
headerPanel.setBackground(Color.BLUE);
headerPanel.add(image1, BorderLayout.WEST);
headerPanel.add(title1, BorderLayout.CENTER);
headerPanel.add(image2, BorderLayout.EAST);


// create and place combo box text fields and labels
comboPanel = new JPanel(new GridLayout(4,2));
comboPanel.setBorder(BorderFactory.createTitledBorder("Parent Information"));
comboPanel.setBackground(Color.BLUE);

// Labels for the center panel to line up with the necessary fields of choice.
parent = new JLabel();
parent.setText("PARENTS NAME:");
teacher = new JLabel();
teacher.setText("TEACHERS NAME:");
child = new JLabel();
child.setText("STUDENTS NAME:");
emaildr = new JLabel();
emaildr.setText("EMAIL ADDRESS:");

//Grouping of the text fields, panels, and boxes.
comboPanel.add(parent);
parentinput =new JTextField(0);
parentinput.addActionListener(this);
comboPanel.add(parentinput);
comboPanel.add(teacher);
comboBox1 = new JComboBox (teacherBoxItem);
comboBox1.addItemListener(this);
comboPanel.add(comboBox1);
comboPanel.add(child);
comboBox2 = new JComboBox (studentBoxItem);
comboBox2.addItemListener(this);
comboPanel.add(comboBox2);
comboPanel.add(emaildr);
emailinput =new JTextField(0);
emailinput.addActionListener(this);
comboPanel.add(emailinput);

// create and place the buttons for the frame
okPanel =new JPanel(new FlowLayout());
saveButton = new JButton ("SAVE");
cancelButton = new JButton ("CANCEL");
saveButton.addActionListener(this);
okPanel.add(saveButton);
cancelButton.addActionListener(this);
okPanel.add(cancelButton);
okPanel.setBackground(Color.BLUE);

contentPane1.add(comboPanel, BorderLayout.CENTER);
contentPane2.add(okPanel, BorderLayout.SOUTH);
contentPane3.add(headerPanel, BorderLayout.NORTH);

// register 'Exit upon closing' as a default close operation
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent event)
{
try
{
String favorite, favorite2;
int	loc, loc2;

favorite = (String) comboBox1.getSelectedItem();
loc		= comboBox1.getSelectedIndex();

favorite2 = (String) comboBox2.getSelectedItem();
loc2		= comboBox2.getSelectedIndex();

JOptionPane.showMessageDialog(this, "Currently selected item'" + 
										favorite + " ' is at index position " + loc);

/*Have input from the parent to a prelabled and dated text file.
Having issues connecting the button action to save and write the new file to the drive.
*/

//Pull internal clock to date and time stamp the upcoming file output save.
Date today;
SimpleDateFormat simpleDF1;
today = new Date();
simpleDF1 = new SimpleDateFormat("EEEE MMMM dd, yyyy HH mm");

	File 						outFile			= new File("Parent Contact Directory.txt " + simpleDF1.format(today));
	FileOutputStream     outFileStream  = new FileOutputStream(outFile);
	PrintWriter          outStream		= new PrintWriter(outFileStream);
	
	outStream.println(parentinput);
	outStream.println(comboBox1);
	outStream.println(comboBox2);
	outStream.println(emailinput);
	
	outStream.close();
	}
	catch (IOException e){
	}
}

public void itemStateChanged(ItemEvent event) 
{
String state;
if (event.getStateChange() == ItemEvent.SELECTED) 
	{
		state = "is selected";
} 
	else 
	{
		state = "is deselected ";
}

JOptionPane.showMessageDialog(this, "JComboBox Item '" +
											event.getItem() + "' " + state);
}
}

Recommended Answers

All 10 Replies

GridLayout maintains equally sized grid cells along the horizontal and vertical. If you need to vary the size of the cells, you'll need to subdivide your layout into multiple panels with their own groups of controls or use a GridBagLayout.

GridBagLayout is a lot more flexible, but it's also much more complex and comes with a bit of a learning curve.

Member Avatar for hfx642

What I do is wrap my JTextFields within their OWN JPanel, with a FlowLayout.
That will shrink them down to an appropriate size.
Then, add this new "wrapper" JPanel to your Grid JPanel.
If you find your JTextFields to be too small,
change your JTextField definitions to have a different value than 0.

GridLayout maintains equally sized grid cells along the horizontal and vertical. If you need to vary the size of the cells, you'll need to subdivide your layout into multiple panels with their own groups of controls or use a GridBagLayout.

GridBagLayout is a lot more flexible, but it's also much more complex and comes with a bit of a learning curve.

I was doing some reading today as well and saw a reference to another layout GroupLayout? Both the GridBagLayout and the GroupLayout are layouts I have not learned yet. The only ones I've been able to play with are Flow, Grid, and Border. I read in our text a little about Absolute but we did not focus on that layout style. So I apologize now if some of my questions seem a little elementary, but if I make individual panels for each combination of Labels and fields wouldn't I still have the TextField issue?

What I do is wrap my JTextFields within their OWN JPanel, with a FlowLayout.
That will shrink them down to an appropriate size.
Then, add this new "wrapper" JPanel to your Grid JPanel.
If you find your JTextFields to be too small,
change your JTextField definitions to have a different value than 0.

I am not aware that you are able to do field wrapping, the only thing I have covered so far was actual lines of text that could be written to wrap. I am not sure how or where to begin with wrapping the text field.

I was doing some reading today as well and saw a reference to another layout GroupLayout?

I would not recommend trying to work with GroupLayout without a GUI designer. It's even more complex than GridBagLayout.

I would not recommend trying to work with GroupLayout without a GUI designer. It's even more complex than GridBagLayout.

Thanks for the information on the Layouts, I reached out to my professor and he told me that some minor imperfections of my program would be allowed as long as I had the basic concept together. I was really spinning my wheels on the sizing and resizing of my fields to the point I had one big Text box :$ . I am on the last part of the program and that is the file save. I have the creation down and the save being generated from the save button but it is pulling in what looks to be actual programming text? I have an attachment for record. I was trying to have it pull in the last input and chosen responses and save in a text file but that is not happening.

javax.swing.JTextField[,500,22,494x162,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=apple.laf.CUIAquaTextFieldBorder@4f80d6,flags=288,maximumSize=,minimumSize=,preferredSize=,caretColor=javax.swing.plaf.ColorUIResource[r=0,g=0,b=0],disabledTextColor=javax.swing.plaf.ColorUIResource[r=128,g=128,b=128],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=apple.laf.CUIAquaImageFactory$SystemColorProxy[r=0,g=0,b=0],selectionColor=apple.laf.CUIAquaImageFactory$SystemColorProxy[r=129,g=113,b=33],columns=0,columnWidth=0,command=,horizontalAlignment=LEADING]
javax.swing.JComboBox[,500,184,494x162,layout=apple.laf.CUIAquaComboBox$CUIAquaComboBoxLayoutManager,alignmentX=0.0,alignmentY=0.0,border=,flags=16777536,maximumSize=,minimumSize=,preferredSize=,isEditable=false,lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=Choose Teacher...]
javax.swing.JComboBox[,500,346,494x162,layout=apple.laf.CUIAquaComboBox$CUIAquaComboBoxLayoutManager,alignmentX=0.0,alignmentY=0.0,border=,flags=16777536,maximumSize=,minimumSize=,preferredSize=,isEditable=false,lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=Choose Student...]
javax.swing.JTextField[,500,508,494x162,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=apple.laf.CUIAquaTextFieldBorder@4f80d6,flags=288,maximumSize=,minimumSize=,preferredSize=,caretColor=javax.swing.plaf.ColorUIResource[r=0,g=0,b=0],disabledTextColor=javax.swing.plaf.ColorUIResource[r=128,g=128,b=128],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=apple.laf.CUIAquaImageFactory$SystemColorProxy[r=0,g=0,b=0],selectionColor=apple.laf.CUIAquaImageFactory$SystemColorProxy[r=129,g=113,b=33],columns=0,columnWidth=0,command=,horizontalAlignment=LEADING]

Those are the string representations of the components themselves.

You need to use the textfield.getText() or combobox.getSelectedItem() to access the values they contain.

your problem is setSize(FRAME_WIDTH, FRAME_HEIGHT);, then JFrames childs fills all empty space into Container,

Those are the string representations of the components themselves.

You need to use the textfield.getText() or combobox.getSelectedItem() to access the values they contain.

Thanks again, I am now able to save the content in a txt. file like I want to, but it was saving it when the save button and cancel button were pressed. I tried to identify the instance of the button action for save and cancel but it is saying

Example3JComboBoxSample.java:125: cannot find symbol
symbol  : variable saveButton
location: class Example3JComboBoxSample
if (clickedButton == saveButton) {
                     ^
1 error

This does not make sense to me I have the JButton Variable (saveButton) identified as well as the cancelButton?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.text.*;

class Example3JComboBoxSample extends JFrame
												implements ActionListener,
												ItemListener
{
private static final int FRAME_WIDTH		=1000;
private static final int FRAME_HEIGHT		=1000;

private JComboBox comboBox1, comboBox2;
private JTextField parentinput, emailinput;

public static void main (String[] args) 
{
	Example3JComboBoxSample frame1 = new Example3JComboBoxSample();
	frame1.setVisible(true);
}

public Example3JComboBoxSample()
 {
Container contentPane1, contentPane2, contentPane3;

JPanel comboPanel, okPanel, headerPanel;

JLabel image1, image2, title1, parent, teacher, child, emaildr;

JButton	saveButton, cancelButton;

String[] teacherBoxItem = {"Choose Teacher...", "Mrs. Thomas", "Mr. Peter", "Mr. Key", "Mrs. Lucky" };
String[] studentBoxItem = {"Choose Student...", "Bobby Jones", "Hailey Johnson", "Sarah Lee", "Carolyn Birdie" };

// set the frame properties
setSize(FRAME_WIDTH,FRAME_HEIGHT);
setTitle ("ExampleJComboBoxSample");

contentPane1 = getContentPane();
contentPane1.setBackground(Color.BLUE);
contentPane1.setLayout(new GridLayout(4,2));

contentPane2 = getContentPane();
contentPane2.setBackground(Color.BLUE);
contentPane2.setLayout(new BorderLayout());

contentPane3 = getContentPane();
contentPane3.setBackground(Color.BLUE);
contentPane3.setLayout(new BorderLayout());

// create and place a header for the frame
headerPanel = new JPanel(new FlowLayout());
image1 = new JLabel(new ImageIcon("/Users/bndandridge/Zion Mascot Image/mascot 2.gif"));
image2 = new JLabel(new ImageIcon("/Users/bndandridge/Zion Mascot Image/mascot 2.gif"));
title1 = new JLabel();
image1.setSize(50,50);
title1.setText("ZION PARENT CONTACT DIRECTORY");
image2.setSize(50,50);
headerPanel.setBackground(Color.BLUE);
headerPanel.add(image1, BorderLayout.WEST);
headerPanel.add(title1, BorderLayout.CENTER);
headerPanel.add(image2, BorderLayout.EAST);


// create and place combo box text fields and labels
comboPanel = new JPanel(new GridLayout(4,2));
comboPanel.setBorder(BorderFactory.createTitledBorder("Parent Information"));
comboPanel.setBackground(Color.BLUE);

// Labels for the center panel to line up with the necessary fields of choice.
parent = new JLabel();
parent.setText("PARENTS NAME:");
teacher = new JLabel();
teacher.setText("TEACHERS NAME:");
child = new JLabel();
child.setText("STUDENTS NAME:");
emaildr = new JLabel();
emaildr.setText("EMAIL ADDRESS:");

//Grouping of the text fields, panels, and boxes.
comboPanel.add(parent);
parentinput =new JTextField(0);
parentinput.addActionListener(this);
comboPanel.add(parentinput);
comboPanel.add(teacher);
comboBox1 = new JComboBox (teacherBoxItem);
comboBox1.addItemListener(this);
comboPanel.add(comboBox1);
comboPanel.add(child);
comboBox2 = new JComboBox (studentBoxItem);
comboBox2.addItemListener(this);
comboPanel.add(comboBox2);
comboPanel.add(emaildr);
emailinput =new JTextField(0);
emailinput.addActionListener(this);
comboPanel.add(emailinput);

// create and place the buttons for the frame
okPanel =new JPanel(new FlowLayout());
saveButton = new JButton ("SAVE");
cancelButton = new JButton ("CANCEL");
saveButton.addActionListener(this);
okPanel.add(saveButton);
cancelButton.addActionListener(this);
okPanel.add(cancelButton);
okPanel.setBackground(Color.BLUE);

contentPane1.add(comboPanel, BorderLayout.CENTER);
contentPane2.add(okPanel, BorderLayout.SOUTH);
contentPane3.add(headerPanel, BorderLayout.NORTH);

// register 'Exit upon closing' as a default close operation
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent event)
{
try
{
if (event.getSource() instanceof JButton) {
JButton clickedButton = (JButton) event.getSource();

if (clickedButton == saveButton) {


/*Have input from the parent to a prelabled and dated text file.
Having issues connecting the button action to save and write the new file to the drive.
*/

//Pull internal clock to date and time stamp the upcoming file output save.
Date today;
SimpleDateFormat simpleDF1;
today = new Date();
simpleDF1 = new SimpleDateFormat("EEEE MMMM dd, yyyy HH mm");

	File 						outFile			= new File("Parent Contact Directory.txt " + simpleDF1.format(today));
	FileOutputStream     outFileStream  = new FileOutputStream(outFile);
	PrintWriter          outStream		= new PrintWriter(outFileStream);
	
	outStream.println(parentinput.getText());
	outStream.println(comboBox1.getSelectedItem());
	outStream.println(comboBox2.getSelectedItem());
	outStream.println(emailinput.getText());
	
	outStream.close();
}
else {
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
	}
}
	catch (IOException e){
	}
}

public void itemStateChanged(ItemEvent event) 
{
String state;
if (event.getStateChange() == ItemEvent.SELECTED) 
	{
		state = "is selected";
} 
	else 
	{
		state = "is deselected ";
}

JOptionPane.showMessageDialog(this, "JComboBox Item '" +
											event.getItem() + "' " + state);
}
}

Thanks again, I am now able to save the content in a txt. file like I want to, but it was saving it when the save button and cancel button were pressed. I tried to identify the instance of the button action for save and cancel but it is saying

Example3JComboBoxSample.java:125: cannot find symbol
symbol  : variable saveButton
location: class Example3JComboBoxSample
if (clickedButton == saveButton) {
                     ^
1 error

This does not make sense to me I have the JButton Variable (saveButton) identified as well as the cancelButton?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.text.*;

class Example3JComboBoxSample extends JFrame
												implements ActionListener,
												ItemListener
{
private static final int FRAME_WIDTH		=1000;
private static final int FRAME_HEIGHT		=1000;

private JComboBox comboBox1, comboBox2;
private JTextField parentinput, emailinput;

public static void main (String[] args) 
{
	Example3JComboBoxSample frame1 = new Example3JComboBoxSample();
	frame1.setVisible(true);
}

public Example3JComboBoxSample()
 {
Container contentPane1, contentPane2, contentPane3;

JPanel comboPanel, okPanel, headerPanel;

JLabel image1, image2, title1, parent, teacher, child, emaildr;

JButton	saveButton, cancelButton;

String[] teacherBoxItem = {"Choose Teacher...", "Mrs. Thomas", "Mr. Peter", "Mr. Key", "Mrs. Lucky" };
String[] studentBoxItem = {"Choose Student...", "Bobby Jones", "Hailey Johnson", "Sarah Lee", "Carolyn Birdie" };

// set the frame properties
setSize(FRAME_WIDTH,FRAME_HEIGHT);
setTitle ("ExampleJComboBoxSample");

contentPane1 = getContentPane();
contentPane1.setBackground(Color.BLUE);
contentPane1.setLayout(new GridLayout(4,2));

contentPane2 = getContentPane();
contentPane2.setBackground(Color.BLUE);
contentPane2.setLayout(new BorderLayout());

contentPane3 = getContentPane();
contentPane3.setBackground(Color.BLUE);
contentPane3.setLayout(new BorderLayout());

// create and place a header for the frame
headerPanel = new JPanel(new FlowLayout());
image1 = new JLabel(new ImageIcon("/Users/bndandridge/Zion Mascot Image/mascot 2.gif"));
image2 = new JLabel(new ImageIcon("/Users/bndandridge/Zion Mascot Image/mascot 2.gif"));
title1 = new JLabel();
image1.setSize(50,50);
title1.setText("ZION PARENT CONTACT DIRECTORY");
image2.setSize(50,50);
headerPanel.setBackground(Color.BLUE);
headerPanel.add(image1, BorderLayout.WEST);
headerPanel.add(title1, BorderLayout.CENTER);
headerPanel.add(image2, BorderLayout.EAST);


// create and place combo box text fields and labels
comboPanel = new JPanel(new GridLayout(4,2));
comboPanel.setBorder(BorderFactory.createTitledBorder("Parent Information"));
comboPanel.setBackground(Color.BLUE);

// Labels for the center panel to line up with the necessary fields of choice.
parent = new JLabel();
parent.setText("PARENTS NAME:");
teacher = new JLabel();
teacher.setText("TEACHERS NAME:");
child = new JLabel();
child.setText("STUDENTS NAME:");
emaildr = new JLabel();
emaildr.setText("EMAIL ADDRESS:");

//Grouping of the text fields, panels, and boxes.
comboPanel.add(parent);
parentinput =new JTextField(0);
parentinput.addActionListener(this);
comboPanel.add(parentinput);
comboPanel.add(teacher);
comboBox1 = new JComboBox (teacherBoxItem);
comboBox1.addItemListener(this);
comboPanel.add(comboBox1);
comboPanel.add(child);
comboBox2 = new JComboBox (studentBoxItem);
comboBox2.addItemListener(this);
comboPanel.add(comboBox2);
comboPanel.add(emaildr);
emailinput =new JTextField(0);
emailinput.addActionListener(this);
comboPanel.add(emailinput);

// create and place the buttons for the frame
okPanel =new JPanel(new FlowLayout());
saveButton = new JButton ("SAVE");
cancelButton = new JButton ("CANCEL");
saveButton.addActionListener(this);
okPanel.add(saveButton);
cancelButton.addActionListener(this);
okPanel.add(cancelButton);
okPanel.setBackground(Color.BLUE);

contentPane1.add(comboPanel, BorderLayout.CENTER);
contentPane2.add(okPanel, BorderLayout.SOUTH);
contentPane3.add(headerPanel, BorderLayout.NORTH);

// register 'Exit upon closing' as a default close operation
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent event)
{
try
{
if (event.getSource() instanceof JButton) {
JButton clickedButton = (JButton) event.getSource();

if (clickedButton == saveButton) {


/*Have input from the parent to a prelabled and dated text file.
Having issues connecting the button action to save and write the new file to the drive.
*/

//Pull internal clock to date and time stamp the upcoming file output save.
Date today;
SimpleDateFormat simpleDF1;
today = new Date();
simpleDF1 = new SimpleDateFormat("EEEE MMMM dd, yyyy HH mm");

	File 						outFile			= new File("Parent Contact Directory.txt " + simpleDF1.format(today));
	FileOutputStream     outFileStream  = new FileOutputStream(outFile);
	PrintWriter          outStream		= new PrintWriter(outFileStream);
	
	outStream.println(parentinput.getText());
	outStream.println(comboBox1.getSelectedItem());
	outStream.println(comboBox2.getSelectedItem());
	outStream.println(emailinput.getText());
	
	outStream.close();
}
else {
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
	}
}
	catch (IOException e){
	}
}

public void itemStateChanged(ItemEvent event) 
{
String state;
if (event.getStateChange() == ItemEvent.SELECTED) 
	{
		state = "is selected";
} 
	else 
	{
		state = "is deselected ";
}

JOptionPane.showMessageDialog(this, "JComboBox Item '" +
											event.getItem() + "' " + state);
}
}

I was able to identify a simple fix for my save issue and have my file generated accordingly with a basic exit on command. I wanted to thank all of you for helping to get to this point and look forward to possibly conversing with you in the future. For my first big program I will continue to work on the look and feel to make it more user friendly thanks again.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.text.*;

class fileJComboBoxSample extends JFrame
												implements ActionListener,
												ItemListener
{
private static final int FRAME_WIDTH		=1000;
private static final int FRAME_HEIGHT		=1000;

private JComboBox comboBox1, comboBox2;
private JTextField parentinput, emailinput;

public static void main (String[] args) 
{
	fileJComboBoxSample frame1 = new fileJComboBoxSample();
	frame1.setVisible(true);
}

public fileJComboBoxSample()
 {
Container contentPane1, contentPane2, contentPane3;

JPanel comboPanel, okPanel, headerPanel;

JLabel image1, image2, title1, parent, teacher, child, emaildr;

JButton	saveButton, cancelButton;

String[] teacherBoxItem = {"Choose Teacher...", "Mrs. Thomas", "Mr. Peter", "Mr. Key", "Mrs. Lucky" };
String[] studentBoxItem = {"Choose Student...", "Bobby Jones", "Hailey Johnson", "Sarah Lee", "Carolyn Birdie" };

// set the frame properties
setSize(FRAME_WIDTH,FRAME_HEIGHT);
setTitle ("ExampleJComboBoxSample");

contentPane1 = getContentPane();
contentPane1.setBackground(Color.BLUE);
contentPane1.setLayout(new GridLayout(4,2));

contentPane2 = getContentPane();
contentPane2.setBackground(Color.BLUE);
contentPane2.setLayout(new BorderLayout());

contentPane3 = getContentPane();
contentPane3.setBackground(Color.BLUE);
contentPane3.setLayout(new BorderLayout());

// create and place a header for the frame
headerPanel = new JPanel(new FlowLayout());
image1 = new JLabel(new ImageIcon("/Users/bndandridge/Zion Mascot Image/mascot 2.gif"));
image2 = new JLabel(new ImageIcon("/Users/bndandridge/Zion Mascot Image/mascot 2.gif"));
title1 = new JLabel();
image1.setSize(50,50);
title1.setText("ZION PARENT CONTACT DIRECTORY");
image2.setSize(50,50);
headerPanel.setBackground(Color.BLUE);
headerPanel.add(image1, BorderLayout.WEST);
headerPanel.add(title1, BorderLayout.CENTER);
headerPanel.add(image2, BorderLayout.EAST);


// create and place combo box text fields and labels
comboPanel = new JPanel(new GridLayout(4,2));
comboPanel.setBorder(BorderFactory.createTitledBorder("Parent Information"));
comboPanel.setBackground(Color.BLUE);

// Labels for the center panel to line up with the necessary fields of choice.
parent = new JLabel();
parent.setText("PARENTS NAME:");
teacher = new JLabel();
teacher.setText("TEACHERS NAME:");
child = new JLabel();
child.setText("STUDENTS NAME:");
emaildr = new JLabel();
emaildr.setText("EMAIL ADDRESS:");

//Grouping of the text fields, panels, and boxes.
comboPanel.add(parent);
parentinput =new JTextField(0);
parentinput.addActionListener(this);
comboPanel.add(parentinput);
comboPanel.add(teacher);
comboBox1 = new JComboBox (teacherBoxItem);
comboBox1.addItemListener(this);
comboPanel.add(comboBox1);
comboPanel.add(child);
comboBox2 = new JComboBox (studentBoxItem);
comboBox2.addItemListener(this);
comboPanel.add(comboBox2);
comboPanel.add(emaildr);
emailinput =new JTextField(0);
emailinput.addActionListener(this);
comboPanel.add(emailinput);

// create and place the buttons for the frame
okPanel =new JPanel(new FlowLayout());
saveButton = new JButton ("SAVE");
cancelButton = new JButton ("CANCEL");
saveButton.addActionListener(this);
saveButton.setActionCommand("save");
okPanel.add(saveButton);
cancelButton.addActionListener(this);
cancelButton.setActionCommand("cancel");
okPanel.add(cancelButton);
okPanel.setBackground(Color.BLUE);

contentPane1.add(comboPanel, BorderLayout.CENTER);
contentPane2.add(okPanel, BorderLayout.SOUTH);
contentPane3.add(headerPanel, BorderLayout.NORTH);

// register 'Exit upon closing' as a default close operation
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent event)
{
try
{
if ("save".equals(event.getActionCommand()))
 {
/*Have input from the parent to a prelabled and dated text file.
Having issues connecting the button action to save and write the new file to the drive.
Pull internal clock to date and time stamp the upcoming file output save.
*/

Date today;
SimpleDateFormat simpleDF1;
today = new Date();
simpleDF1 = new SimpleDateFormat("EEEE MMMM dd, yyyy HH mm");

	File 						outFile			= new File("Parent Contact Directory.txt " + simpleDF1.format(today));
	FileOutputStream     outFileStream  = new FileOutputStream(outFile);
	PrintWriter          outStream		= new PrintWriter(outFileStream);
	
	outStream.println(parentinput.getText());
	outStream.println(comboBox1.getSelectedItem());
	outStream.println(comboBox2.getSelectedItem());
	outStream.println(emailinput.getText());
	
	outStream.close();
	
JOptionPane.showMessageDialog(this, "Your Contact Information Has Been Saved");
System.exit(0);
}
else {
System.exit(0);
}
	}

	catch (IOException e){
	}



	}

public void itemStateChanged(ItemEvent event) 
{
String state;
if (event.getStateChange() == ItemEvent.SELECTED) 
	{
		state = "is selected";
} 
	else 
	{
		state = "is deselected ";
}

JOptionPane.showMessageDialog(this, "JComboBox Item '" +
											event.getItem() + "' " + state);
}

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.