Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    30
    Thanks
    1
    Thanked 0 Times in 0 Posts

    GUI Scrollbar Problem

    Following the examples in my book, the code I have should place a scrollbar on the right side of the bottom most text area. Am I overlooking something?

    Code:
    	CS1337.502
    	DATE HANDED IN
    	This program displays labeled textboxes for name, address, and phone number. When the save button is pressed, 
    	the data from the boes is written to a file named CS1337Asg6Out.txt. 
    */
    
    import java.util.Scanner;
    import java.io.*;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JFrame;
    import java.awt.GridLayout;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class contactBook extends JFrame
    {
    	private JButton saveButton = new JButton("Save");
    	private JList nameList = new JList();
    	
    	public contactBook()
    	{
    		JPanel p1 = new JPanel(new GridLayout(4, 2, 4, 4));
    		p1.add(new JLabel("Name"));
    		p1.add(new JTextField(15));
    		p1.add(new JLabel("Address"));
    		p1.add(new JTextField(20));
    		p1.add(new JLabel("Phone Number"));
    		p1.add(new JTextField(12));
    		p1.add(new JLabel());
    		p1.add(saveButton);
    		
    		saveButton.addActionListener(new SaveListener());
    		
    		JPanel p2 = new JPanel(new BorderLayout());
    		p2.add(new JLabel("Names"), BorderLayout.NORTH);
    		p2.add(new JScrollPane(nameList), BorderLayout.WEST);
    		nameList.setBorder(new javax.swing.border.LineBorder(Color.black, 2));
    		
    		setLayout(new BorderLayout(2,2));
    		add(p1, BorderLayout.NORTH);
    		add(p2, BorderLayout.SOUTH);
    	}
    	
    	public static void main(String[] args)
    	{
    		contactBook frame = new contactBook();
    		frame.setTitle("Contact Book");
    		frame.setSize(400,300);
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
    	
    	class SaveListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			System.out.print("hi");
    		}
    	}
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    It probably does. There's nothing in the List right now to show, so the scrollbars are not necessary and the default policy is to only show them as needed.
    If you want to add them in for all the time, add the two arguments to the JScrollPane constructor: JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS.

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    30
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I would need a String array to store and display the names as they are added by the user, correct?

    How do I clear all the text boxes? I want this to be one of the actions that take place when the save button is pressed.
    Last edited by trantommyd; 04-26-2012 at 12:26 AM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    No, you're in a graphical environment, so I wouldn't recommend the use of a scanner.
    The easiest thing to do is implement ActionListener onto contactBook. This way it can access any of the members. The textfields will need to be promoted to object members to retrieve them, but all you need to do is pull the fields, and insert into list. Now list is scalar in a sense, so it will only hold one object to display at a time, and not multiple fields in a single entry. I still haven't installed javac since I put in a new harddisk, but that would be as so:
    PHP Code:
    class contactBook extends JFrame implements ActionListener
    {
        
    //...
        
    public void actionPerformed(ActionEvent e)
        {
            if (
    e.getSource() == this.saveButton)
            {
                
    // This is where I'm saying a list is scalar:
                
    String sContact this.txtName " " this.txtAddress " " this.txtPhoneNumber;
                ((
    DefaultListModel)nameList.getModel()).addElement(sContact);
            }
        }

    Then simply set this as the actionListener.
    If you want to show each in separate columns, you'll want to use a JTable instead of a JList. You can also create a Contact class and override the toString to show in the JList as you like, or rewrite the ListCellRenderer to change the display. Hmm, as interesting as that is, its possible to make it look like a JTable within the renderer, but I've never tried that before.

  • #5
    New Coder
    Join Date
    Mar 2012
    Posts
    30
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I edited my last question while you were answering what was previously there. I figured it out and went the route of making private specific instances and then used getText().

    how do I clear the JTextFields? I wish to include this as one of the actions that happen when the save button is pressed.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Simply call the .setText and set it to "".

  • #7
    New Coder
    Join Date
    Mar 2012
    Posts
    30
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Where can I put output.close() so I won't get any errors? Right now I have it inside the try block, but that causes the file content to be erased and then written over that. (So all of the data isn't in the output file, just the latest entries.)

    Code:
    import java.util.Scanner;
    import java.io.*;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JFrame;
    import java.awt.GridLayout;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class contactBook extends JFrame
    {
    	private JButton saveButton = new JButton("Save");
    	private DefaultListModel model = new DefaultListModel();
    	private JList nameList = new JList(model);
    	private JTextField nameTF = new JTextField(15);	
    	private JTextField addressTF = new JTextField(20);
    	private JTextField numberTF = new JTextField(12);
    	private int counter = 0;
    	private java.io.File file = new java.io.File("CS1337Asg6Out.txt");
    	
    	public contactBook()
    	{
    		JPanel p1 = new JPanel(new GridLayout(4, 2, 4, 4));
    		p1.add(new JLabel("Name"));
    		p1.add(nameTF);
    		p1.add(new JLabel("Address"));
    		p1.add(addressTF);
    		p1.add(new JLabel("Phone Number"));
    		p1.add(numberTF);
    		p1.add(new JLabel());
    		p1.add(saveButton);
    		
    		saveButton.addActionListener(new SaveListener());
    		
    		JPanel p2 = new JPanel(new BorderLayout());
    		p2.add(new JLabel("Names"), BorderLayout.NORTH);
    		p2.add(new JScrollPane(nameList), BorderLayout.WEST);
    		nameList.setBorder(new javax.swing.border.LineBorder(Color.black, 2));
    		
    		setLayout(new BorderLayout(2,2));
    		add(p1, BorderLayout.NORTH);
    		add(p2, BorderLayout.SOUTH);
    	}
    	
    	public static void main(String[] args) 
    	{
    		contactBook frame = new contactBook();
    		frame.setTitle("Contact Book");
    		frame.setSize(400,300);
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
    	
    	class SaveListener implements ActionListener 
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			try
    			{
    				java.io.PrintWriter output = new java.io.PrintWriter(file);
    				String name = nameTF.getText();
    				output.print(name);
    				output.print(", ");
    				String address = addressTF.getText();
    				output.print(address);
    				output.print(", ");
    				String number = numberTF.getText();
    				output.print(number);
    				output.print("' ");
    				output.println();
    				output.close();
    				model.add(counter, name);
    				counter++;
    				nameTF.setText("");
    				addressTF.setText("");
    				numberTF.setText("");
    			}
    			catch(Exception a)
    			{
    				System.out.print("Error: " + a.getMessage());
    			}
    		}
    	}
    }

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    The try block won't cause the overwrite. Its the print that's doing that. Close shouldn't throw an error in the PrintWriter; only the constructor is mandated to do that.
    Move all file handling into a try block. Use only a member to declare the path to it; File() may throw an error as well. Switch it to the FileWriter class as this makes appending easier:
    PHP Code:
    BufferedWriter writer null;
    try
    {
        
    File myFile = new File(FILEPATH);
        
    // here you can force a new file creation if myFile.exists is false.
        
    FiileWriter fp = new FileWriter(myFiletrue);
        
    writer = new BufferedWriter(fp);
        
    writer.write("Appended data");
        
    writer.newline();
    }
    catch (
    Exception ex)
    {
        
    // split up as you see fit.
    }
    finally
    {
        try
        {
            
    writer.close();
        }
        catch (
    Exception ex)
        {
        }

    Try that.

  • #9
    New Coder
    Join Date
    Mar 2012
    Posts
    30
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'm gonna try playing with it to see if I can get anywhere. This material seems new to me.

    Edit.
    I got it to work implementing your suggestions. Again, I sincerely thank you for your time and help.

    Code:
    import java.util.Scanner;
    import java.io.*;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JFrame;
    import java.awt.GridLayout;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class contactBook extends JFrame
    {
    	private JButton saveButton = new JButton("Save");
    	private DefaultListModel model = new DefaultListModel();
    	private JList nameList = new JList(model);
    	private JTextField nameTF = new JTextField(15);	
    	private JTextField addressTF = new JTextField(20);
    	private JTextField numberTF = new JTextField(12);
    	private int counter = 0;
    	
    	public contactBook()
    	{
    		JPanel p1 = new JPanel(new GridLayout(4, 2, 4, 4));
    		p1.add(new JLabel("Name"));
    		p1.add(nameTF);
    		p1.add(new JLabel("Address"));
    		p1.add(addressTF);
    		p1.add(new JLabel("Phone Number"));
    		p1.add(numberTF);
    		p1.add(new JLabel());
    		p1.add(saveButton);
    		
    		saveButton.addActionListener(new SaveListener());
    		
    		JPanel p2 = new JPanel(new BorderLayout());
    		p2.add(new JLabel("Names"), BorderLayout.NORTH);
    		p2.add(new JScrollPane(nameList), BorderLayout.WEST);
    		nameList.setBorder(new javax.swing.border.LineBorder(Color.black, 2));
    		
    		setLayout(new BorderLayout(2,2));
    		add(p1, BorderLayout.NORTH);
    		add(p2, BorderLayout.SOUTH);
    	}
    	
    	public static void main(String[] args) 
    	{
    		contactBook frame = new contactBook();
    		frame.setTitle("Contact Book");
    		frame.setSize(400,300);
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
    	
    	class SaveListener implements ActionListener 
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			try
    			{
    				FileWriter fstream = new FileWriter("CS1337Asg6Out.txt", true);
    				BufferedWriter output = new BufferedWriter(fstream);
    				String name = nameTF.getText();
    				output.write(name);
    				output.write(", ");
    				String address = addressTF.getText();
    				output.write(address);
    				output.write(", ");
    				String number = numberTF.getText();
    				output.write(number);
    				output.write(", ");
    				output.close();	
    				model.add(counter, name);
    				counter++;
    				nameTF.setText("");
    				addressTF.setText("");
    				numberTF.setText("");
    			}
    			catch(Exception a)
    			{
    				System.out.print("Error: " + a.getMessage());
    			}
    		}
    	}
    }
    Last edited by trantommyd; 04-26-2012 at 05:49 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •