User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 391,777 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,447 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 5984 | Replies: 5
Reply
Join Date: Nov 2004
Posts: 5
Reputation: jonboy_us is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jonboy_us jonboy_us is offline Offline
Newbie Poster

line up JTextField GUI java & write to file

  #1  
Dec 13th, 2004
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()
Attached Images
File Type: jpg ComboBox.JPG (15.0 KB, 5 views)
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,646
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 191
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: line up JTextField GUI java & write to file

  #2  
Dec 14th, 2004
Create a JPanel with a GridLayout with 2 columns and as many rows as you have inputfields.
Put the JLabels in the left column, the JTextFields in the right column.
They are now lined up perfectly.

What isn't the write funtion doing? And what do you expect it to do?
Reply With Quote  
Join Date: Nov 2004
Posts: 5
Reputation: jonboy_us is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jonboy_us jonboy_us is offline Offline
Newbie Poster

Re: line up JTextField GUI java & write to file

  #3  
Dec 14th, 2004
Originally Posted by jwenting
Create a JPanel with a GridLayout with 2 columns and as many rows as you have inputfields.
Put the JLabels in the left column, the JTextFields in the right column.
They are now lined up perfectly.

What isn't the write funtion doing? And what do you expect it to do?

My bad If I didn't explain what I wanted befor more clearly...

I have to grab the user name and password from the fields to a file in the noted server (path) and database (file name).
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,646
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 191
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: line up JTextField GUI java & write to file

  #4  
Dec 14th, 2004
So? Fill the properties object properly and set the correct name and path for the file...
Nothing mysterious about that...
Reply With Quote  
Join Date: Nov 2004
Posts: 5
Reputation: jonboy_us is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jonboy_us jonboy_us is offline Offline
Newbie Poster

Re: line up JTextField GUI java & write to file

  #5  
Dec 14th, 2004
Originally Posted by jwenting
So? Fill the properties object properly and set the correct name and path for the file...
Nothing mysterious about that...
Yea, no mystory for you maybe, but I'm stuck. I don't know how to do that.
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,646
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 191
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: line up JTextField GUI java & write to file

  #6  
Dec 15th, 2004
that's what that nice courseware and books you got (or should have) are for...
It's such basic stuff...
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 4:54 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC