Java2 homework help.
Quick comment...Java was not meant for GUI! If you want GUI, then use VB. Now that I have that out of my system...
Homework goal...
To create a GUI java app that will save the info in the text fields to a file to a location that will be designated in the app (see attached screen shot) And yes I know the “Diisplay tab is spelled wrong. The instructor messed it up in his instructions for the assignment and I just wanted to mess with him. ;-) How do I fix this?
2 questions
1. I put my JTextFields and JLabels in a JPanel to keep them lined up, then put the JPanels in my JFrame. I'm trying to line up all of the JPanels to the right, but when I do that, they all end up lining up in one row from left to right. To get them to stack one over the other, I have to center them all and that just doesn't look right.
2. But before the first one, I have to get this thing to write to a file (“prop.prop). The ActionListener shown below is an example that the teacher gave us, but I'm not totally sure why it's not working.
Thanks in advance,
Jonboy
PS
Nothing is in the "Diisplay" tab b/c that is for a later assignment. He just wanted us to put it in to show that we knew how to put in tabs.
package Week3;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import java.io.*;
//import java.io.IOException;
//import java.io.InputStreamReader;
import java.util.Properties;
public class ComboBox {
JButton btSave = null;
JFrame frame = null;
JButton save = null;
JTextField txtUserName = new JTextField(20);
JTextField txtPW = new JTextField(20);
JTextField txtServer = new JTextField(20);
JTextField txtDatabase = new JTextField(20);
public static void main(String[] args) {
ComboBox monster = new ComboBox();
monster.build();
}//end main
public void build(){
JFrame frame = new JFrame("Database Connection");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
//import tab as object & create tabs
JTabbedPane tb = new JTabbedPane();
JPanel tbDisp = new JPanel();
JPanel tbConf = new JPanel();
//create labels and text fields for Configure tab
JPanel fpUsrName = new JPanel();
JPanel fpPassword = new JPanel();
JPanel fpServer = new JPanel();
JPanel fpDatabase = new JPanel();
JLabel flUsrName = new JLabel("Username:");
JLabel flPassword = new JLabel("Password:");
JLabel flServer = new JLabel("Server:");
JLabel flDatabase = new JLabel("Database:");
fpUsrName.add(flUsrName);
fpUsrName.add(txtUserName);
fpPassword.add(flPassword);
fpPassword.add(txtPW);
fpServer.add(flServer);
fpServer.add(txtServer);
fpDatabase.add(flDatabase);
fpDatabase.add(txtDatabase);
//create Configure tab
tb.add(tbConf,"Configuration");
//tbDisp.setLayout(new BoxLayout(tbDisp, BoxLayout.Y_AXIS));
tbConf.add(fpUsrName);
tbConf.add(fpPassword);
tbConf.add(fpServer);
tbConf.add(fpDatabase);
btSave = new JButton("Save");
btSave.addActionListener(new btSave());
tbConf.add(new JButton("Save"));
//create "Diisplay" tab
tb.add(tbDisp,"Diisplay");
//more to be added later
//create frame size, add tabs, and make visable
contentPane.add(tb,BorderLayout.CENTER);
//frame.getContentPane().add(BorderLayout.EAST, tbConf);
frame.setSize(350, 260);
frame.setVisible(true);
}
class btSave implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
Properties propertyFile = new Properties();
InputStream input = null;
OutputStream output = null;
try{
input = new FileInputStream("prop.prop");
propertyFile.load(input);
System.out.println(propertyFile.getProperty("Name"));
System.out.println(propertyFile.getProperty("Password"));
input.close();
}catch(IOException ex){
ex.printStackTrace();
}
try{
output = new FileOutputStream("prop.prop");
propertyFile.setProperty("Name",null);
propertyFile.setProperty("Password",null);
propertyFile.store(output,null);
output.close();
}catch(IOException ex){
ex.printStackTrace();
}
}
}//end Button1 subclass
}//end ComboBox()