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 3 of 3

Thread: Movable message with JTextfield

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Movable message with JTextfield

    Hi everybody, i have a problem on this code.
    The ideas is to make a moveable message and that string message obtained from JTextField

    _______________________________________________
    |JPanel("Input Word") | JTextField |
    |______________________________________________|
    | |
    | |
    | |
    | here is the moveable message |
    | |
    | |
    |______________________________________________|

    This is the part of my code, not finished yet.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
     
    public class TestListener extends JFrame {
     
    	private JTextField jtfMessage = new JTextField();
    	private JPanel p = new JPanel(new GridLayout(1, 2));
     
    	public TestListener() {
    		p.add(new JLabel("Word"));
    		p.add(jtfMessage);
    		//add(p, BorderLayout.CENTER);
    		add(new MovableMessagePanel("Welcome to Java"));
    	}
     
    	public static void main(String[] args) {
    		TestListener frame = new TestListener();
    		frame.setSize(300, 300);
    		frame.setTitle("Test move and Listener");
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
     
    	private class MovableMessagePanel extends JPanel {
    		private String message = "Welcome to java";
    		int x = 20;
    		int y = 20;
     
    		public MovableMessagePanel(String s) {
    			message = s;
    			addMouseMotionListener(new MouseMotionAdapter() {
    				public void mouseDragged(MouseEvent e) {
    					x = e.getX();
    					y = e.getY();
    					repaint();
    				}
    			});
    		}
     
    		protected void paintComponent(Graphics g) {
    			super.paintComponent(g);
    			g.drawString(message, x, y);
    		}
    	}
    }

    If i use BorderLayout.CENTER for jtextfield
    and BorderLayout.SOUTH for movablemessage

    will cause the moveablemessage disappear.
    Please help me thanks.


  2. #2
    Junior Member
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Movable message with JTextfield

    Problem solved

    import java.awt.*;
    import java.awt.event.*;
     
    import javax.swing.*;
     
     
    public class TestListener extends JFrame {
     
    	private JTextField jtfMessage = new JTextField();
    	private JPanel p = new JPanel(new GridLayout(1, 2));
    	private JButton jbtOk = new JButton("Ok");
     
    	public TestListener() {
    		p.add(jtfMessage);
    		p.add(jbtOk);
    		add(p, BorderLayout.SOUTH);
    		add(new MovableMessagePanel("Welcome to Java"), BorderLayout.CENTER);
    	}
     
    	public static void main(String[] args) {
    		TestListener frame = new TestListener();
    		frame.setSize(500, 300);
    		frame.setTitle("Test move and Listener");
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
     
    	private class MovableMessagePanel extends JPanel {
    		private String message = "Welcome to java";
    		int x = 20;
    		int y = 20;
     
    		public MovableMessagePanel(String s) {
    			message = s;
    			addMouseMotionListener(new MouseMotionAdapter() {
    				public void mouseDragged(MouseEvent e) {
    					x = e.getX();
    					y = e.getY();
    					repaint();
    				}
    			});
    			jbtOk.addActionListener(new ActionListener() {
    				public void actionPerformed(ActionEvent e) {
    					message = jtfMessage.getText();
    					repaint();
    				}		
    			});
     
    		}
     
    		protected void paintComponent(Graphics g) {
    			super.paintComponent(g);
    			g.drawString(message, x, y);
    		}
    	}
    }

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Movable message with JTextfield

    Mark this thread as SOLVED.

Similar Threads

  1. Movable message with JTextfield
    By whit3ang3l in forum AWT / Java Swing
    Replies: 0
    Last Post: December 27th, 2011, 03:49 AM
  2. what is -xlint message?
    By rush7610 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 21st, 2011, 10:36 PM
  3. Hello Message
    By shaik42 in forum Member Introductions
    Replies: 1
    Last Post: July 3rd, 2011, 10:02 AM
  4. Send XML message over UDP?
    By udpkillsme in forum Java Networking
    Replies: 1
    Last Post: June 22nd, 2011, 11:27 AM
  5. Hello message
    By ceejaysoft in forum Member Introductions
    Replies: 1
    Last Post: March 17th, 2011, 04:18 AM