Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Having problems with GUI Interface

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Having problems with GUI Interface

    Hello again!

    In class we started this chapter on Graphics and I am struggling to understand it, it seems like it all hit me at once, I did the readings but it just seems like gibberish.

    My problem, is that in the GUI it keeps overriding the last data it put up.

    Here's my code:

    First the GUI

    //CameraGUI
     
    import javax.swing.*;
    import java.awt.*;
     
    public class CameraGUI extends JFrame
    {
    	JTextArea ta = new JTextArea(25,40);
     
    	JScrollPane sp = new JScrollPane(ta, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
    		ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
     
    	JPanel pane = new JPanel();
     
    	public CameraGUI() 
    	{
    		super("Camera Inventory Program");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setBounds(100,100,500,500);
    		pane.add(sp);
    		add(pane);
    		setVisible(true);
    	}
    }

    Then the Main program

    //Thomas Harrald
    //IT215 Checkpoint Inventory Program
    //Camera Inventory program
    import java.util.Arrays;
    import java.io.InputStreamReader;
    import java.io.BufferedReader;
    import java.io.IOException;
     
    // Stores and then gets info on a Camera
    public class CameraProgram4
    {
     
    	static CameraGUI mainGUI = new CameraGUI();
     
    	public static void main(String args[])
    	{
     
    		CameraClass[] cameras;
     
    		CameraClass nikonCamera= new CameraClass( 3100, "Nikon", "Electronics", 5, 10.00);
    		CameraClass sonyCamera = new CameraClass( 4500, "Sony", "Electronics", 10, 300.00);
    		CameraClass canonCamera= new CameraClass( 1550, "Canon", "Electronics", 3, 40.00);
    		DigitalCamera kodakCamera = new DigitalCamera( 3000, "Kodak", "Electronics", 20,5.00, 1080);
     
    		cameras = new CameraClass[ 4 ];
     
    		cameras[ 0 ] = nikonCamera;
    		cameras[ 1 ] = sonyCamera;
    		cameras[ 2 ] = canonCamera;
    		cameras[ 3 ] = kodakCamera;
     
     
    		mainGUI.ta.setText(cameras[0].toString());
    		mainGUI.ta.setText(cameras[1].toString());
     
     
    	}
    }

    If someone needs the other two files I can put them up.


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Having problems with GUI Interface

    I only put two of the Array elements in to troubleshoot. It only shows the last element. cameras[0] overrides cameras[1]. Is there a way I can just make it add the text instead of setting it?

    Just thinking out loud, can I append all array.toString() methods to make one inclusive string and set the text to that?
    Last edited by Harrald; August 19th, 2012 at 08:21 PM.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having problems with GUI Interface

    cameras[0] overrides cameras[1]
    Where are you trying to show these two items? The only place I see them used is with a text area. The setText() sets the text to be displayed. Is that the method you want to use? The text area class has other methods that might show data differently.
    Read the API doc for the text area to see:
    Java Platform SE 6
    If you don't understand my answer, don't ignore it, ask a question.

  4. The Following User Says Thank You to Norm For This Useful Post:

    Harrald (August 19th, 2012)

  5. #4
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Having problems with GUI Interface

    Thanks a lot Norm, I'll take a look at that.

  6. #5
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Having problems with GUI Interface

    Quote Originally Posted by Norm View Post
    Where are you trying to show these two items? The only place I see them used is with a text area. The setText() sets the text to be displayed. Is that the method you want to use? The text area class has other methods that might show data differently.
    Read the API doc for the text area to see:
    Java Platform SE 6
    Norm is there anything specific to look for GUI related? There is a LOT of data in the package lol.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having problems with GUI Interface

    Look at the methods for the class you are using: JTextArea
    Find the classname in the list on the left, click the link and the doc will show in the main frame.
    Scroll down to where the methods are listed.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Having problems with GUI Interface

    I'll take a look at better methods in the morning...

    I modified the toString() to include a \n, and appended all the arrays elements toString().

    It gets the job done... but I will try to find a better method. Heres the new code:

    //Thomas Harrald
    //IT215 Checkpoint Inventory Program
    //Camera Inventory program
    import java.util.Arrays;
    import java.io.InputStreamReader;
    import java.io.BufferedReader;
    import java.io.IOException;
     
    // Stores and then gets info on a Camera
    public class CameraProgram4
    {
     
    	static CameraGUI mainGUI = new CameraGUI();
     
    	public static void main(String args[])
    	{
     
    		CameraClass[] cameras;
     
    		CameraClass nikonCamera= new CameraClass( 3100, "Nikon", "Electronics", 5, 10.00);
    		CameraClass sonyCamera = new CameraClass( 4500, "Sony", "Electronics", 10, 300.00);
    		CameraClass canonCamera= new CameraClass( 1550, "Canon", "Electronics", 3, 40.00);
    		DigitalCamera kodakCamera = new DigitalCamera( 3000, "Kodak", "Electronics", 20,5.00, 1080);
     
    		cameras = new CameraClass[ 4 ];
     
    		cameras[ 0 ] = nikonCamera;
    		cameras[ 1 ] = sonyCamera;
    		cameras[ 2 ] = canonCamera;
    		cameras[ 3 ] = kodakCamera;
     
    		String guiDisplay ="";
     
    		for( int counter=0; counter <=3; counter++)
    		{
    			guiDisplay += cameras[counter].toString();
    		}
     
    		mainGUI.ta.setText(guiDisplay);
     
     
     
    	}
    }

    I did see a lot of cool methods I'd like to try out. Any suggestions?

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having problems with GUI Interface

    Does the code do what you want? Then it's good.


    Done for tonight. Back tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Interface
    By Samaras in forum Java Theory & Questions
    Replies: 2
    Last Post: August 4th, 2012, 05:27 PM
  2. Interface
    By kaasi in forum Member Introductions
    Replies: 4
    Last Post: April 21st, 2011, 10:45 AM
  3. User Interface Problems
    By pmg in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 4th, 2011, 07:32 AM
  4. interface
    By nasi in forum Object Oriented Programming
    Replies: 5
    Last Post: September 2nd, 2010, 10:36 PM
  5. Need Help on the Interface Function
    By yel_hiei in forum Object Oriented Programming
    Replies: 12
    Last Post: July 29th, 2010, 07:27 AM