Results 1 to 7 of 7

Thread: Draw TextField

  1. #1
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    10

    Default Draw TextField

    Is it possible to draw a JTextField on Graphics g? If yes could you please tell me how, and if not then could you give me suggestions what could I do?

    Second question.. How can I add a keylistener to this:
    Java Code:
    package org.swc.loginreg;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.TextField;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    import org.swc.App.Main;
    import org.swc.Misc.Misc;
    
    
    
    public class drawLoginPanel{
    	
    	public static TextField usernameField;
    	
    	public static void drawTehPanel(Graphics g){
    		Main main = new Main();
    		g.setColor(Color.white);
    		g.fillRoundRect(242, 110, 300, 250, 20, 20);
    		g.setColor(Color.GRAY);
    		g.setFont(new Font("Calibri", Font.BOLD, 24));
    		g.drawString("Login", 362, 140);
    		g.setFont(new Font("Calibri", Font.PLAIN, 12));
    		g.drawString("Username: ", 262, 180);
    		g.setColor(Color.BLACK);
    		g.drawRect(328, 169, 200, 14);
    		g.drawString(main.username, 330, 181);
    	}
    }
    and I use this class on :
    Java Code:
    package org.swc.App;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.UIManager;
    
    import org.swc.Misc.Misc;
    import org.swc.loginreg.*;
    
    public class Main implements ActionListener{
    
    	public String frameTitle = "Street Wars Client";
    	public JFrame frame;
    	public boolean loggedIn = false;
    	public String username = "N/A";
    	public JPanel gamePanel;
    	
    	public static void main(String args[]) {
    		Main main = new Main();
    		try {
    			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    		} catch (Exception e) {
    
    		}
    		main.initUI();
    	}
    	
    	public void initUI() {
    		frame = new JFrame(frameTitle);
    		frame.setLayout(new BorderLayout());
    		frame.setResizable(false);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		gamePanel = new JPanel () {
    			private static final long serialVersionUID = 1L;
    
    			public void paintComponent(Graphics g) {
    				g.setColor(Color.BLACK);
    				g.fillRect(0, 0, 765, 503);
    				if(loggedIn == false){
    					drawLoginPanel.drawTehPanel(g);
    				}
    		    }
    		};
    
    		JMenuBar menuBar = new JMenuBar();
    
    		JMenu fileMenu = new JMenu("File");
    
    		String[] fileButtons = new String[] { "Exit" };
    
    		for (String name : fileButtons) {
    			JMenuItem menuItem = new JMenuItem(name);
    			if (name.equalsIgnoreCase("-"))
    				fileMenu.addSeparator();
    			else {
    				menuItem.addActionListener(this);
    				fileMenu.add(menuItem);
    			}
    		}
    		
    		menuBar.add(fileMenu);
    		
    		gamePanel.setLayout(new BorderLayout());
    
    		gamePanel.setPreferredSize(new Dimension(765, 503));
    		
    		frame.getContentPane().add(gamePanel, BorderLayout.CENTER);
    		frame.getContentPane().add(menuBar, BorderLayout.NORTH);
    		frame.pack();
    		frame.setVisible(true);
    	}
    	
    	@Override
    	public void actionPerformed(ActionEvent evt) {
    		String cmd = evt.getActionCommand();
    		//misc.debug(cmd);
    		if (cmd != null){
    			if(cmd == "Exit"){
    				int i1 = 0;
    	            if((i1 = JOptionPane.showConfirmDialog(frame, "Are you sure that you want to exit Street Wars?", "Exit", i1)) == 0)
    	                System.exit(0);
    	            return;
    			}
    		}else{
    			Misc.debug("CMD is null !");
    		}
    	}
    	
    }
    and if you need the misc class:
    Java Code:
    package org.swc.Misc;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.URL;
    
    public class Misc {
    	
    	private static boolean debug = true;
    	
    	public static void sM(String message){
    		System.out.println(message);
    	}
    	
    	public static void debug(final String message){
    		if(debug == true)
    			System.err.println("DEBUG :: " + message);
    	}
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    31

    Default

    1) What do you mean draw a JTextField? You can draw an image of a JTextField using the Graphics object, but that's all it is, an image, not a functioning JTextField. What are you trying to do with it? What's its purpose?

    2) Can you give more detail on your key listener problem? You have asked a broad question and posted a lot of code. It would help us out a lot by telling us more as well as what you've tried, how it doesn't work, etc...

    Luck.

  3. #3
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    10

    Default

    Quote Originally Posted by Fubarable View Post
    1) What do you mean draw a JTextField? You can draw an image of a JTextField using the Graphics object, but that's all it is, an image, not a functioning JTextField. What are you trying to do with it? What's its purpose?
    I am going to try and make my first multiplayer game, and right now I am trying to add a login system, but I will make a normal panel instead, so that's ok now.
    2) Can you give more detail on your key listener problem? You have asked a broad question and posted a lot of code. It would help us out a lot by telling us more as well as what you've tried, how it doesn't work, etc...

    Luck.
    Java Code:
    gamePanel.addKeyListener(this);
    I did for my gamePanel.
    And then for the KeyListener I did this:
    Java Code:
        @Override
        public void keyPressed(KeyEvent e) {
            Misc.debug("Key: " + e.getKeyCode());
            
        }
    
        @Override
        public void keyReleased(KeyEvent arg0) {
            // TODO Auto-generated method stub
            
        }
    
        @Override
        public void keyTyped(KeyEvent arg0) {
            // TODO Auto-generated method stub
            
        }
    But when I run my app, and when I click something nothing happens.

    EDIT: SOLVED, I've changed gamePanel.addKeyListener(this); to frame.addKeyListener(this);
    Thanks for your help !
    Last edited by PhQ; 07-17-2010 at 12:49 AM.

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

    Default

    I see what you are trying to do with drawing a text field and I just wanted to mention that you should know that your code commits a cardinal Swing sin in that you're trying to put program logic in the paint portion of the code. As you've already suggested you're much better to create a JPanel and display it in a JDialog or JOptionPane.

    Luck.

  5. #5
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    10

    Default

    Quote Originally Posted by Fubarable View Post
    I see what you are trying to do with drawing a text field and I just wanted to mention that you should know that your code commits a cardinal Swing sin in that you're trying to put program logic in the paint portion of the code. As you've already suggested you're much better to create a JPanel and display it in a JDialog or JOptionPane.

    Luck.
    Can I draw on the JPanel and add the swing stuff on it? Because I kind of like my little box :)

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

    Default

    Quote Originally Posted by PhQ View Post
    Can I draw on the JPanel and add the swing stuff on it? Because I kind of like my little box :)
    What do you mean by "draw"? You can add components to a JPanel, sure, and doing so is one of the foundation techniques of Swing, one of the reasons why gaining a thorough knowledge of the layout managers is key. And you can "draw" on a JPanel within its paintComponent override, but you're not going to want to draw components on a JPanel, you'll want to add them. Also, what do you mean by "little box"?

  7. #7
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    10

    Default

    Quote Originally Posted by Fubarable View Post
    What do you mean by "draw"? You can add components to a JPanel, sure, and doing so is one of the foundation techniques of Swing, one of the reasons why gaining a thorough knowledge of the layout managers is key. And you can "draw" on a JPanel within its paintComponent override, but you're not going to want to draw components on a JPanel, you'll want to add them. Also, what do you mean by "little box"?
    I have made a nice box with the graphics stuff (you know : public void paint(Graphics g) <- that)
    and thanks for your help.

Similar Threads

  1. how to get integer value from textfield
    By sandysm in forum Advanced Java
    Replies: 4
    Last Post: 04-16-2010, 01:11 PM
  2. TextField Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 02:21 PM
  3. JSP - getting value from a textfield
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 08:58 PM

Posting Permissions

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