line up JTextField GUI java & write to file

Reply

Join Date: Nov 2004
Posts: 5
Reputation: jonboy_us is an unknown quantity at this point 
Solved Threads: 0
jonboy_us jonboy_us is offline Offline
Newbie Poster

line up JTextField GUI java & write to file

 
0
  #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.

  1. package Week3;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Container;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import javax.swing.BoxLayout;
  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JLabel;
  11. import javax.swing.JPanel;
  12. import javax.swing.JTabbedPane;
  13. import javax.swing.JTextField;
  14.  
  15. import java.io.*;
  16. //import java.io.IOException;
  17. //import java.io.InputStreamReader;
  18. import java.util.Properties;
  19.  
  20. public class ComboBox {
  21. JButton btSave = null;
  22. JFrame frame = null;
  23. JButton save = null;
  24. JTextField txtUserName = new JTextField(20);
  25. JTextField txtPW = new JTextField(20);
  26. JTextField txtServer = new JTextField(20);
  27. JTextField txtDatabase = new JTextField(20);
  28.  
  29. public static void main(String[] args) {
  30. ComboBox monster = new ComboBox();
  31. monster.build();
  32. }//end main
  33.  
  34. public void build(){
  35. JFrame frame = new JFrame("Database Connection");
  36. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  37. Container contentPane = frame.getContentPane();
  38.  
  39. //import tab as object & create tabs
  40. JTabbedPane tb = new JTabbedPane();
  41. JPanel tbDisp = new JPanel();
  42. JPanel tbConf = new JPanel();
  43.  
  44. //create labels and text fields for Configure tab
  45. JPanel fpUsrName = new JPanel();
  46. JPanel fpPassword = new JPanel();
  47. JPanel fpServer = new JPanel();
  48. JPanel fpDatabase = new JPanel();
  49. JLabel flUsrName = new JLabel("Username:");
  50. JLabel flPassword = new JLabel("Password:");
  51. JLabel flServer = new JLabel("Server:");
  52. JLabel flDatabase = new JLabel("Database:");
  53.  
  54. fpUsrName.add(flUsrName);
  55. fpUsrName.add(txtUserName);
  56. fpPassword.add(flPassword);
  57. fpPassword.add(txtPW);
  58. fpServer.add(flServer);
  59. fpServer.add(txtServer);
  60. fpDatabase.add(flDatabase);
  61. fpDatabase.add(txtDatabase);
  62.  
  63. //create Configure tab
  64. tb.add(tbConf,"Configuration");
  65. //tbDisp.setLayout(new BoxLayout(tbDisp, BoxLayout.Y_AXIS));
  66. tbConf.add(fpUsrName);
  67. tbConf.add(fpPassword);
  68. tbConf.add(fpServer);
  69. tbConf.add(fpDatabase);
  70. btSave = new JButton("Save");
  71. btSave.addActionListener(new btSave());
  72. tbConf.add(new JButton("Save"));
  73.  
  74. //create "Diisplay" tab
  75. tb.add(tbDisp,"Diisplay");
  76. //more to be added later
  77.  
  78. //create frame size, add tabs, and make visable
  79. contentPane.add(tb,BorderLayout.CENTER);
  80. //frame.getContentPane().add(BorderLayout.EAST, tbConf);
  81. frame.setSize(350, 260);
  82. frame.setVisible(true);
  83. }
  84.  
  85. class btSave implements ActionListener{
  86. public void actionPerformed(ActionEvent arg0) {
  87.  
  88. Properties propertyFile = new Properties();
  89. InputStream input = null;
  90. OutputStream output = null;
  91.  
  92. try{
  93. input = new FileInputStream("prop.prop");
  94. propertyFile.load(input);
  95. System.out.println(propertyFile.getProperty("Name"));
  96. System.out.println(propertyFile.getProperty("Password"));
  97. input.close();
  98. }catch(IOException ex){
  99. ex.printStackTrace();
  100. }
  101.  
  102. try{
  103. output = new FileOutputStream("prop.prop");
  104. propertyFile.setProperty("Name",null);
  105. propertyFile.setProperty("Password",null);
  106. propertyFile.store(output,null);
  107. output.close();
  108. }catch(IOException ex){
  109. ex.printStackTrace();
  110. }
  111. }
  112. }//end Button1 subclass
  113. }//end ComboBox()
Attached Thumbnails
ComboBox.JPG  
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: line up JTextField GUI java & write to file

 
0
  #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 Quick reply to this message  
Join Date: Nov 2004
Posts: 5
Reputation: jonboy_us is an unknown quantity at this point 
Solved Threads: 0
jonboy_us jonboy_us is offline Offline
Newbie Poster

Re: line up JTextField GUI java & write to file

 
0
  #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 Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: line up JTextField GUI java & write to file

 
0
  #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 Quick reply to this message  
Join Date: Nov 2004
Posts: 5
Reputation: jonboy_us is an unknown quantity at this point 
Solved Threads: 0
jonboy_us jonboy_us is offline Offline
Newbie Poster

Re: line up JTextField GUI java & write to file

 
0
  #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 Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: line up JTextField GUI java & write to file

 
0
  #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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC