Change Focus

I can't figure out how to change the focus to the terminal window

Page 1 of 1

6 Replies - 2553 Views - Last Post: 03 December 2009 - 08:53 PM Rate Topic: -----

#1 mud  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-November 09

Change Focus

Posted 01 December 2009 - 05:33 PM

I can't figure out how to change the focus to the terminal window without clicking on the window. I am making a basic text-based rpg where you type comands in the terminal window, and it has images displayed to the side. Whenever I change the image the focus is changed automatically to the JFrame, and the user has to click on the terminal window to bring the focus back. It is unbelievably annoying. So I would need some way to change the focus back to the terminal window.

A change could be made here (one example of where I change the image)
	private void start()
	{
		boolean finished = false;
		int hole = 0;
		int skull = 0;

		drawImage.drawIcon("Jail1.jpg");
		printStart1();
	}




or here in the class where it displays the image
import javax.swing.*;

public class DisplayImage extends JFrame
{
	private JLabel label;
	
	public DisplayImage()
	{
	}
	
	public void drawIcon(String Filename)
	{
		if (label != null)
		{
			remove(label);
		}
		ImageIcon image = new ImageIcon(Filename);
		label = new JLabel(image);
		add(label);
		setSize(640,480);
		setVisible(true);
	}
}



This isn't required for the project but it would really help.

Is This A Good Question/Topic? 0
  • +

Replies To: Change Focus

#2 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: Change Focus

Posted 01 December 2009 - 05:43 PM

label.requestFocus();



In any swing component, the requestFocus method should do just that...request the focus. And almost all the time, it is given.

OH...misinterpreted the question...sorry. I don't know the answer, so I'll do some research. If this does become excessively annoying, you might want to use a TextArea for input/output instead.... I'd look into it... Another option for solely input, is the JOptionPane to ask questions from the user. Here is a link to that tutorial if you're interested: http://java.sun.com/...nts/dialog.html

This post has been edited by Dogstopper: 01 December 2009 - 05:46 PM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Change Focus

Posted 01 December 2009 - 05:53 PM

First this is a very bad coding practice
	public void drawIcon(String Filename)
	{
		if (label != null)
		{
			remove(label);
		}
		ImageIcon image = new ImageIcon(Filename);
		label = new JLabel(image);
		add(label);
		setSize(640,480);
		setVisible(true);
	}


You should create the label only once (even if empty)
set its size only once (even if empty)
set it visible only once (even if empty)
add it only once (even if empty)
and then just

	public void drawIcon(String Filename)
	{
		ImageIcon image = new ImageIcon(Filename);
		label.setIcon(image);
	}


removing and adding JComponent is not a good idea
now after you called drawIcon(filename);
you can
otherComponent.requestFocus();
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: Change Focus

Posted 01 December 2009 - 06:09 PM

But to switch between a GUI to a terminal? Why not just make it all GUI? There's no harm in it after all....

Back to the question...I looked around, and could not find anything to help you out in this case, so that leads me to think that maybe you cant do it.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Change Focus

Posted 01 December 2009 - 06:14 PM

View PostDogstopper, on 1 Dec, 2009 - 05:09 PM, said:

But to switch between a GUI to a terminal? Why not just make it all GUI? There's no harm in it after all....

Back to the question...I looked around, and could not find anything to help you out in this case, so that leads me to think that maybe you cant do it.

Ho !! the terminal windows is not a JTextField or something like that but the console from where the application was launched !!!

Dogstopper is right, I don't see an easy way to do that... even your JFrame might lost focus nothing say that the next window on the list would be your console, may be your browser or any other open window

Dogstopper is also right to suggest to make your stuff all GUI with a JTextArea/Field has "console"
Was This Post Helpful? 0
  • +
  • -

#6 mud  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-November 09

Re: Change Focus

Posted 03 December 2009 - 08:45 PM

OK I guess I might as well make the terminal window stuff into a GUI. It will help for when I try to change my application into an applet anyway. But if anyone finds anything that will help in the meantime, please tell.
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: Change Focus

Posted 03 December 2009 - 08:53 PM

View Postmud, on 3 Dec, 2009 - 09:45 PM, said:

OK I guess I might as well make the terminal window stuff into a GUI. It will help for when I try to change my application into an applet anyway. But if anyone finds anything that will help in the meantime, please tell.


Well, I wish you luck in this! Here is a great tutorial from sun:
http://java.sun.com/...ents/index.html

Just look at the text components tutorials.

Again, if you need help, let us know and we will be happy to help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1