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

Thread: Problem with Controls because of buttons

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

    Default Problem with Controls because of buttons

    Hi,I got an application that contains buttons that when pressed,draw a figure.For example,if the "triangle" buttons is pressed then a triangle is drawn.I tried to add controls so that the figure that appears could be moved by keyboard keys but for some reason when a button is pressed tthe controls stop working.
    the buttons are in a panel when the controls are in another class that is a frame.
    My code:
    Menu.java
     
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
     
    public class Menu extends Frame{
    private static Picture pic;
     
    private static Button b1;
    private Triangle triangle;
     
    private static Button b2;
    private Serpinsky s;
     
    private static Button b3;
    private Smiley sm;
     
    private static Button b4;
    private Rectangles r;
     
    private static Button b5;
    private MovePingPong mpp;
    public Menu(String st,Triangle tri,Serpinsky s,Smiley sm,Rectangles r,MovePingPong mpp)
    {
    	super(st);
    	this.setSize(800, 600);
     
    	this.addWindowListener(new ExitListener());
    	ButtonListener al=new ButtonListener();
     
    	b1=new Button("Traingle");
    	b1.setBackground(Color.gray);
    	b1.addActionListener(al);
     
     
    	b2=new Button("Serpinsky");
    	b2.setBackground(Color.gray);
    	b2.addActionListener(al);
     
    	b3=new Button("Smiley");
    	b3.setBackground(Color.gray);
    	b3.addActionListener(al);
     
    	b4=new Button("Rectangles");
    	b4.setBackground(Color.gray);
    	b4.addActionListener(al);
     
    	b5=new Button("Ping Pong ball");
    	b5.setBackground(Color.gray);
    	b5.addActionListener(al);
     
    	this.mpp=mpp;
    	this.sm=sm;
    	this.s=s;
        this.triangle=tri;
        this.r=r;
     
        pic=new Picture(this.triangle,this.s,this.sm,this.r,this.mpp);
        pic.setBackground(Color.LIGHT_GRAY);
        pic.add(b1);
        pic.add(b2);
       pic.add(b3);
       pic.add(b4);
        pic.add(b5);
        this.add(pic);
        this.setFocusable(true);
        this.setVisible(true);
     
    }
     
    public class ButtonListener implements ActionListener,KeyListener{
    	public ButtonListener(){
    		addKeyListener(this);
    	}
    	public void actionPerformed(ActionEvent e){
    		Button b=(Button)e.getSource();
    		Graphics g=pic.getGraphics();
     
    		if(b.equals(Menu.b1))
    			pic.paintFrameTraing(Menu.this.triangle, g);
    		if(b.equals(Menu.b2))
    			pic.paintFrameSerpinsky(Menu.this.s, g);
    		if(b.equals(Menu.b3))
    			pic.paintFrameSmiley(sm, g);
    		if(b.equals(Menu.b4))
    			pic.paintFrameRectangles(r, g);
    		if(b.equals(Menu.b5))
    				pic.paintFramePingPong(mpp, g);
    	}
     
    	public void keyPressed(KeyEvent e) {
    		if(e.getKeyCode()==KeyEvent.VK_RIGHT){
    			triangle.Move(10, 0);
    			repaint();
    		  }
    	}
     
    	public void keyReleased(KeyEvent e) {
    	}
    	public void keyTyped(KeyEvent e) {	
    	}
    }
     
    public class ExitListener extends WindowAdapter{
    	@Override
    	public void windowClosing(WindowEvent e){
    		System.exit(0);
    	}
    }
     
    public static void main(String[] args)
    {
    	Triangle triangle=new Triangle(Color.black);
    	Serpinsky s=new Serpinsky();
    	Smiley sm=new Smiley();
    	Rectangles r=new Rectangles();
    	MovePingPong mpp=new MovePingPong(5);
    	new Menu("This is an Applicaion Biiaaaaacch!!",triangle,s,sm,r,mpp);
    }
     
    }
    Picture.java
     
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.Panel;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
     
    public class Picture extends Panel{
    	public Picture(Triangle tri,Serpinsky s,Smiley sm,Rectangles r,MovePingPong mpp)
    	{
    		super();
    		this.setLocation(0,85);
    		this.setLayout(new FlowLayout());
    		this.setSize(800,600);
    	}
     
    public void paintFrameTraing(Triangle tri,Graphics g)
    {
    	g.clearRect(0,0,1500,1000);
    	g.setColor(Color.black);
     
    	tri.DrawTriangle(g);
    }
     
    public void paintFrameSerpinsky(Serpinsky s,Graphics g){
    	g.clearRect(0, 0, 1500, 1000);
    	g.setColor(Color.black);
    	s.drawSerpinsky(g);
    }
    public void paintFrameSmiley(Smiley sm,Graphics g)
    {
    	g.clearRect(0, 0, 1500, 1000);
    	g.setColor(Color.black);
    	sm.drawSmiley(g);
    }
    public void paintFrameRectangles(Rectangles r,Graphics g){
    	g.clearRect(0, 0, 1500, 1000);
    	g.setColor(Color.black);
    	r.drawRectangles(g);
    }
    public void paintFramePingPong(MovePingPong mpp,Graphics g){
    	g.setColor(Color.black);
    	g.clearRect(0, 0, 1500, 1000);
    	mpp.drawPingPong(g);
    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 600 Times in 515 Posts

    Default Re: Problem with Controls because of buttons

    I assume your button has taken the focus. You can test this by pressing space. Does that press the button? It's a shortcut that lets the user interact with your gui using the keyboard, but it can get in the way if you want your keyboard to do other stuff.

    You might want to look into key bindings, or you could just set each button to be unfocusable. Check out the API for useful functions.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Problem with Controls because of buttons

    I thought it was about focus as well,do you know how to set as unfocusable?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 600 Times in 515 Posts

    Default Re: Problem with Controls because of buttons

    Quote Originally Posted by SagiIs View Post
    I thought it was about focus as well,do you know how to set as unfocusable?
    Did you look at the API?

    Java Platform SE 6
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Problem with Controls because of buttons

    I tried to set it as unfocusable but it didn't help,got any other ideas?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 600 Times in 515 Posts

    Default Re: Problem with Controls because of buttons

    Quote Originally Posted by SagiIs View Post
    I tried to set it as unfocusable but it didn't help,got any other ideas?
    If you want help, you'll have to provide an updated SSCCE (not your whole program) that demonstrates exactly what you've tried.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    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: Problem with Controls because of buttons

    Quote Originally Posted by SagiIs View Post
    I thought it was about focus as well,do you know how to set as unfocusable?
    Shift the focus to some other component.

Similar Threads

  1. Buttons and a Login problem
    By raja211991 in forum AWT / Java Swing
    Replies: 1
    Last Post: September 23rd, 2011, 02:41 PM
  2. Replies: 2
    Last Post: July 8th, 2011, 06:33 AM
  3. My buttons will not work
    By ITStudent02 in forum What's Wrong With My Code?
    Replies: 21
    Last Post: June 29th, 2011, 03:36 PM
  4. [SOLVED] Problem with swing-buttons
    By Minken in forum AWT / Java Swing
    Replies: 8
    Last Post: October 15th, 2010, 09:20 PM
  5. Need more buttons!
    By GotWankel? in forum AWT / Java Swing
    Replies: 0
    Last Post: October 5th, 2009, 01:08 AM