Results 1 to 7 of 7
  1. #1
    Join Date
    May 2011
    Location
    Seattle, WA
    Posts
    35
    Rep Power
    0

    Default Making a textbox

    Hello!
    I wrote a program that prints out a certain image on my screen and after many bugfixes got it to work ^_^ Now I want to add a textbox so that the user can enter the address of an image on their computer and have it printed out. I've been reading a lot of examples about textboxes but I just can't seem to get it to work :(

    I'm very new to the language so please don't get too technical XD

    Here is the code code I wrote for my first program. If any of you can give me some hints on how to add the textbox I'd be very grateful. Sorry about the overuse of notes, I tend to get carried away.

    Java Code:
    (Class Images)
    
    //Loading vital libraries 
    import javax.swing.JFrame;
    
    public class Images
    {	
    	public static void main(String[] args)
    	{
            //Object to run class Screen
    	Screen scr = new Screen();
    	//Closes window properly when exit or escape is pressed
    	scr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	//Setting screen size to 361 by 374 pixels
    	scr.setSize(361,374);
    	//Making the screen visible
    	scr.setVisible(true);
    	}
    }
    
    (Class Screen)
    
    //Loading vital libraries
    import java.awt.*;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    
    public class Screen extends JFrame
    {
    //Creating the private image variable "picture"
    private Image picture;
    
    public Screen()
        {
    	//Printing the window title
        super("Trololo");	
        }
    
    public void paint(Graphics g)
        {	    
    	//Loading the image to be printed into the "picture" variable
    	picture = new ImageIcon("C:\\Users\\Adam\\Pictures\\lolcat.png").getImage();
    	//Printing "picture" on the screen at 0, 16 
    	g.drawImage(picture,0,16,null);
    	}
    }
    Last edited by meesterpickles; 07-18-2011 at 11:26 PM.

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Make a textbox, make a button, make the button listener getText from the textbox. Check if getText is a valid file location, if yes print, if no report error.

    However you could just use a JFileChooser and save yourself the trouble.
    How to Use File Choosers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  3. #3
    Join Date
    May 2011
    Location
    Seattle, WA
    Posts
    35
    Rep Power
    0

    Default

    Quote Originally Posted by Dark View Post
    Make a textbox, make a button, make the button listener getText from the textbox. Check if getText is a valid file location, if yes print, if no report error.

    However you could just use a JFileChooser and save yourself the trouble.
    How to Use File Choosers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    I know that that is what I need to do, but I would like to know how.
    I'll check out that JFileChooser if all else fails though, thanks.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    What is a "textbox" as no such animal exists in Swing? There are several different text components available, and to learn more about them, and when to use each, I suggest you check out the Oracle Swing tutorials.

  5. #5
    Join Date
    May 2011
    Location
    Seattle, WA
    Posts
    35
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    What is a "textbox" as no such animal exists in Swing? There are several different text components available, and to learn more about them, and when to use each, I suggest you check out the Oracle Swing tutorials.
    XD C'mon you know what I mean! Anyways the official Java tutorials have been of little use to me, but I will make sure to check out the one on Swing.
    Thanks!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by meesterpickles View Post
    XD C'mon you know what I mean!
    No, honestly I don't. You could be referring to a JTextField, JTextArea, JTextPane, ... etc, and I find if I assume anything here, I will usually assume wrong. Your job, if you want a precise answer from this forum, is to put in the effort to give us precise information. That's not asking too much, you think?

    Anyways the official Java tutorials have been of little use to me, ...
    To make them useful to you, keep studying them. Like anything else, your ability to use and gain from them gets better with practice.

  7. #7
    Join Date
    May 2011
    Location
    Seattle, WA
    Posts
    35
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    No, honestly I don't. You could be referring to a JTextField, JTextArea, JTextPane, ... etc, and I find if I assume anything here, I will usually assume wrong. Your job, if you want a precise answer from this forum, is to put in the effort to give us precise information. That's not asking too much, you think?
    To make them useful to you, keep studying them. Like anything else, your ability to use and gain from them gets better with practice.
    I meant JTextField, sorry I guess It wasn't that rhetorical after all XP
    I'll be more patient with the tutorials, I apologize, I am but young in the ways of Java oh wise one.

    Thanks for all your help! I think I'm pointed in the right direction now.

Similar Threads

  1. How I can draw on textbox
    By iraqy2010 in forum CLDC and MIDP
    Replies: 0
    Last Post: 06-19-2011, 05:48 PM
  2. Textbox limit setting
    By rjagan in forum New To Java
    Replies: 2
    Last Post: 03-22-2011, 10:03 AM
  3. How to set value to a TextBox
    By kishan in forum JavaServer Faces (JSF)
    Replies: 3
    Last Post: 06-29-2010, 02:46 PM

Tags for this Thread

Posting Permissions

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