•
•
•
•
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
![]() |
•
•
Join Date: Nov 2004
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
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.
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()•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,646
Reputation:
Rep Power: 18
Solved Threads: 191
•
•
Join Date: Nov 2004
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
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).
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,646
Reputation:
Rep Power: 18
Solved Threads: 191
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
- Creating first GUI Java Program (Java)
- Write File to disk (Java)
- Read/write to same file > once, Help (C++)
- Write to a file (C)
Other Threads in the Java Forum
- Previous Thread: How to call Client.java from HTML?
- Next Thread: Problem with JBuilder



Linear Mode