Opening up a new frame

Hi everyone, im new here.

I'm creating a friendly GUI project with buttons and detecting key press events. Currently, I'm having problems detecting keypress for a new frame(sub frame) that i've opened.

I've 2 AWTEventListener, 1 for main frame and 1 for sub frame.

Is there any ways that i can clear this problem?

Thanks in advance

[366 byte] By [Smile1412a] at [2007-11-27 3:48:42]
# 1

By "sub frame" do you mean an JInternalFrame or a JDialog or something else? Also it is not quite clear what exactly you want to do (subject -> opening up a frame, message -> detecting keypresses).

Generally, you do not need to mess with AWTEventListener, it's mainly for AWT internal use. Which listener is right for you depends on what you want to do...

Posting some code would certainly also help.

Mike

MikePa at 2007-7-12 8:52:35 > top of Java-index,Desktop,Core GUI APIs...
# 2
[url http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html]How to Use Key Bindings[/url]
camickra at 2007-7-12 8:52:35 > top of Java-index,Desktop,Core GUI APIs...
# 3
/*Attached below is the program codes. When you select Singapore and press enter. Another Frame (SubFrame0) will pop up, the keypress will then be focused on that frame alone. However, I'm not able to do that. Thanks in advance for your help.*/
Smile1412a at 2007-7-12 8:52:35 > top of Java-index,Desktop,Core GUI APIs...
# 4

//This is the First program code

//MainFrame

import javax.swing.SwingUtilities;

import javax.swing.JPanel;

import javax.swing.JFrame;

import java.awt.GridBagLayout;

import javax.swing.JButton;

import java.awt.AWTEvent;

import java.awt.GridBagConstraints;

import java.awt.Insets;

import java.awt.Toolkit;

import java.awt.event.AWTEventListener;

import java.awt.event.KeyEvent;

public class MainFrame extends JFrame {

private static final long serialVersionUID = 1;

private JPanel jContentPane = null;

public JButton[] MenuButton = new JButton[8];

{

for (int i = 0; i MenuButton = null;

}

private int current = 0;

private JPanel jContentPane() {

if (jContentPane == null) {

GridBagConstraints gridBagConstraints7 = new GridBagConstraints();

gridBagConstraints7.gridx = 1;

gridBagConstraints7.insets = new Insets(8, 8, 8, 8);

gridBagConstraints7.fill = GridBagConstraints.BOTH;

gridBagConstraints7.gridy = 3;

GridBagConstraints gridBagConstraints6 = new GridBagConstraints();

gridBagConstraints6.gridx = 1;

gridBagConstraints6.insets = new Insets(8, 8, 8, 8);

gridBagConstraints6.fill = GridBagConstraints.BOTH;

gridBagConstraints6.gridy = 2;

GridBagConstraints gridBagConstraints5 = new GridBagConstraints();

gridBagConstraints5.gridx = 1;

gridBagConstraints5.insets = new Insets(8, 8, 8, 8);

gridBagConstraints5.fill = GridBagConstraints.BOTH;

gridBagConstraints5.gridy = 1;

GridBagConstraints gridBagConstraints4 = new GridBagConstraints();

gridBagConstraints4.gridx = 1;

gridBagConstraints4.insets = new Insets(8, 8, 8, 8);

gridBagConstraints4.fill = GridBagConstraints.BOTH;

gridBagConstraints4.gridy = 0;

GridBagConstraints gridBagConstraints3 = new GridBagConstraints();

gridBagConstraints3.gridx = 0;

gridBagConstraints3.insets = new Insets(8, 8, 8, 8);

gridBagConstraints3.fill = GridBagConstraints.BOTH;

gridBagConstraints3.gridy = 3;

GridBagConstraints gridBagConstraints2 = new GridBagConstraints();

gridBagConstraints2.gridx = 0;

gridBagConstraints2.insets = new Insets(8, 8, 8, 8);

gridBagConstraints2.fill = GridBagConstraints.BOTH;

gridBagConstraints2.gridy = 2;

GridBagConstraints gridBagConstraints1 = new GridBagConstraints();

gridBagConstraints1.gridx = 0;

gridBagConstraints1.insets = new Insets(8, 8, 8, 8);

gridBagConstraints1.fill = GridBagConstraints.BOTH;

gridBagConstraints1.gridy = 1;

GridBagConstraints gridBagConstraints = new GridBagConstraints();

gridBagConstraints.gridx = 0;

gridBagConstraints.fill = GridBagConstraints.BOTH;

gridBagConstraints.insets = new Insets(8, 8, 8, 8);

gridBagConstraints.gridy = 0;

jContentPane = new JPanel();

jContentPane.setLayout(new GridBagLayout());

jContentPane.add(getJButton(), gridBagConstraints);

jContentPane.add(getJButton1(), gridBagConstraints1);

jContentPane.add(getJButton2(), gridBagConstraints2);

jContentPane.add(getJButton3(), gridBagConstraints3);

jContentPane.add(getJButton4(), gridBagConstraints4);

jContentPane.add(getJButton5(), gridBagConstraints5);

jContentPane.add(getJButton6(), gridBagConstraints6);

jContentPane.add(getJButton7(), gridBagConstraints7);

Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {

public void eventDispatched(AWTEvent e)

{

if (((KeyEvent)e).getID() == 401)

myAction(((KeyEvent) e).getKeyCode());

}

}, AWTEvent.KEY_EVENT_MASK);

}

return jContentPane;

}

private JButton getJButton() {

if (MenuButton[0] == null) {

MenuButton[0] = new JButton();

MenuButton[0].setText("Singapore");

}

return MenuButton[0];

}

private JButton getJButton1() {

if (MenuButton[1] == null) {

MenuButton[1] = new JButton();

MenuButton[1].setText("World");

}

return MenuButton[1];

}

private JButton getJButton2() {

if (MenuButton[2] == null) {

MenuButton[2] = new JButton();

MenuButton[2].setText("Business/Finance");

}

return MenuButton[2];

}

private JButton getJButton3() {

if (MenuButton[3] == null) {

MenuButton[3] = new JButton();

MenuButton[3].setText("Sports");

}

return MenuButton[3];

}

private JButton getJButton4() {

if (MenuButton[4] == null) {

MenuButton[4] = new JButton();

MenuButton[4].setText("Technology");

}

return MenuButton[4];

}

private JButton getJButton5() {

if (MenuButton[5] == null) {

MenuButton[5] = new JButton();

MenuButton[5].setText("Entertainment");

}

return MenuButton[5];

}

private JButton getJButton6() {

if (MenuButton[6] == null) {

MenuButton[6] = new JButton();

MenuButton[6].setText("Special Reports");

}

return MenuButton[6];

}

private JButton getJButton7() {

if (MenuButton[7] == null) {

MenuButton[7] = new JButton();

MenuButton[7].setText("Travel");

}

return MenuButton[7];

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

MainFrame thisClass = new MainFrame();

thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

thisClass.setVisible(true);

}

});

}

public MainFrame() {

super();

initialize();

}

private void initialize() {

this.setSize(1024, 768);

this.setContentPane(jContentPane());

this.setTitle("Daily News");

}

public void myAction(int currentSelect)

{

switch (currentSelect)

{

case KeyEvent.VK_DOWN:

current = current 7 ? 0:1);

MenuButton[current].requestFocus();

break;

case KeyEvent.VK_UP:

current = current 0 ? 1:0);

if (current == -1)

current = 7;

MenuButton[current].requestFocus();

break;

case KeyEvent.VK_LEFT:

case KeyEvent.VK_RIGHT:

{

if (current == 0 )

{

current = 4;

MenuButton[current].requestFocus();

break;

}

if (current == 1 )

{

current = 5;

MenuButton[current].requestFocus();

break;

}

if (current == 2 )

{

current = 6;

MenuButton[current].requestFocus();

break;

}

if (current == 3 )

{

current = 7;

MenuButton[current].requestFocus();

break;

}

if (current == 4 )

{

current = 0;

MenuButton[current].requestFocus();

break;

}

if (current == 5 )

{

current = 1;

MenuButton[current].requestFocus();

break;

}

if (current == 6 )

{

current = 2;

MenuButton[current].requestFocus();

break;

}

if (current == 7 )

{

current = 3;

MenuButton[current].requestFocus();

break;

}

}

case KeyEvent.VK_ENTER:

{

if (current == 0)

{

JFrame aa = new SubFrame0();

aa.isFocusOwner();

aa.requestFocus();

aa.setVisible(true);

aa.setLocation(80, 70);

aa.setSize(262,280);

}

}

}

}

}

Smile1412a at 2007-7-12 8:52:35 > top of Java-index,Desktop,Core GUI APIs...
# 5

//This is SubFrame0

import javax.swing.SwingUtilities;

import javax.swing.JPanel;

import javax.swing.JFrame;

import java.awt.GridBagLayout;

import javax.swing.JButton;

import java.awt.*;

import java.awt.event.*;

import java.awt.AWTEvent;

import java.awt.GridBagConstraints;

import java.awt.Insets;

import java.awt.Toolkit;

public class SubFrame0 extends JFrame {

private static final long serialVersionUID = 1;

private JPanel jContentPane = null;

public JButton[] jButton = new JButton[8];

{

for (int i = 0; i jButton = null;

}

public Boolean[] chkButton = new Boolean[4];

{

for (int i = 0; i {

chkButton = false;

}

}

private int currentY = 0;

private int X = 128, Y = 0, Z = 128;

private int X1,Y1,Z1 = 238;

private JButton getJButton0() {

if (jButton[0] == null) {

jButton[0] = new JButton();

jButton[0].setText("North News");

jButton[0].setBackground(new Color(X,Y,Z));

}

return jButton[0];

}

private JButton getJButton1() {

if (jButton[1] == null) {

jButton[1] = new JButton();

jButton[1].setText("East News");

jButton[1].setBackground(new Color(X,Y,Z));

}

return jButton[1];

}

private JButton getJButton2() {

if (jButton[2] == null) {

jButton[2] = new JButton();

jButton[2].setText("South News");

jButton[2].setBackground(new Color(X,Y,Z));

}

return jButton[2];

}

private JButton getJButton3() {

if (jButton[3] == null) {

jButton[3] = new JButton();

jButton[3].setText("West News");

jButton[3].setBackground(new Color(X,Y,Z));

}

return jButton[3];

}

private JButton getJButton4() {

if (jButton[4] == null) {

jButton[4] = new JButton();

jButton[4].setText("Confirm");

jButton[4].setBackground(new Color(X,Y,Z));

}

return jButton[4];

}

private JButton getJButton7() {

if (jButton[7] == null) {

jButton[7] = new JButton();

jButton[7].setText("Cancel");

jButton[7].setBackground(new Color(X,Y,Z));

}

return jButton[7];

}

private JButton getJButton5() {

if (jButton[5] == null) {

jButton[5] = new JButton();

jButton[5].setText("Select All");

jButton[5].setBackground(new Color(X,Y,Z));

}

return jButton[5];

}

private JButton getJButton6() {

if (jButton[6] == null) {

jButton[6] = new JButton();

jButton[6].setText("Clear All");

jButton[6].setBackground(new Color(X,Y,Z));

}

return jButton[6];

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

SubFrame0 thisClass = new SubFrame0();

thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

thisClass.setVisible(true);

}

});

}

public SubFrame0() {

super();

initialize();

}

private void initialize() {

this.setSize(462, 319);

this.setContentPane(getJContentPane());

this.setTitle("Singapore Daily News");

}

private JPanel getJContentPane() {

if (jContentPane == null) {

GridBagConstraints gridBagConstraints7 = new GridBagConstraints();

gridBagConstraints7.gridx = 1;

gridBagConstraints7.insets = new Insets(6, 6, 6, 6);

gridBagConstraints7.fill = GridBagConstraints.BOTH;

gridBagConstraints7.gridheight = 2;

gridBagConstraints7.gridy = 2;

GridBagConstraints gridBagConstraints6 = new GridBagConstraints();

gridBagConstraints6.gridx = 1;

gridBagConstraints6.insets = new Insets(6, 6, 6, 6);

gridBagConstraints6.gridheight = 2;

gridBagConstraints6.fill = GridBagConstraints.BOTH;

gridBagConstraints6.gridy = 0;

GridBagConstraints gridBagConstraints5 = new GridBagConstraints();

gridBagConstraints5.gridx = 1;

gridBagConstraints5.insets = new Insets(6, 6, 6, 6);

gridBagConstraints5.fill = GridBagConstraints.BOTH;

gridBagConstraints5.gridy = 4;

GridBagConstraints gridBagConstraints4 = new GridBagConstraints();

gridBagConstraints4.gridx = 0;

gridBagConstraints4.insets = new Insets(6, 6, 6, 6);

gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;

gridBagConstraints4.gridy = 4;

GridBagConstraints gridBagConstraints3 = new GridBagConstraints();

gridBagConstraints3.gridx = 0;

gridBagConstraints3.insets = new Insets(6, 6, 6, 6);

gridBagConstraints3.fill = GridBagConstraints.BOTH;

gridBagConstraints3.gridy = 3;

GridBagConstraints gridBagConstraints2 = new GridBagConstraints();

gridBagConstraints2.gridx = 0;

gridBagConstraints2.insets = new Insets(6, 6, 6, 6);

gridBagConstraints2.fill = GridBagConstraints.BOTH;

gridBagConstraints2.gridy = 2;

GridBagConstraints gridBagConstraints1 = new GridBagConstraints();

gridBagConstraints1.gridx = 0;

gridBagConstraints1.insets = new Insets(6, 6, 6, 6);

gridBagConstraints1.fill = GridBagConstraints.BOTH;

gridBagConstraints1.gridy = 1;

GridBagConstraints gridBagConstraints = new GridBagConstraints();

gridBagConstraints.gridx = 0;

gridBagConstraints.fill = GridBagConstraints.BOTH;

gridBagConstraints.insets = new Insets(6, 6, 6, 6);

gridBagConstraints.gridy = 0;

jContentPane = new JPanel();

jContentPane.setLayout(new GridBagLayout());

jContentPane.add(getJButton0(), gridBagConstraints);

jContentPane.add(getJButton1(), gridBagConstraints1);

jContentPane.add(getJButton2(), gridBagConstraints2);

jContentPane.add(getJButton3(), gridBagConstraints3);

jContentPane.add(getJButton4(), gridBagConstraints4);

jContentPane.add(getJButton5(), gridBagConstraints6);

jContentPane.add(getJButton6(), gridBagConstraints7);

jContentPane.add(getJButton7(), gridBagConstraints5);

Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {

public void eventDispatched(AWTEvent e)

{

if (((KeyEvent)e).getID() == 401)

myAction(((KeyEvent) e).getKeyCode());

}

}, AWTEvent.KEY_EVENT_MASK);

}

return jContentPane;

}

public void myAction(int currentSelect)

{

switch (currentSelect)

{

case KeyEvent.VK_DOWN:

currentY = currentY 7 ? 0:1);

jButton[currentY].requestFocus();

break;

case KeyEvent.VK_UP:

currentY = currentY 0 ? 1:0);

if (currentY == -1)

currentY = 7;

jButton[currentY].requestFocus();

break;

case KeyEvent.VK_LEFT:

case KeyEvent.VK_RIGHT:

{

if ((currentY == 0 ) || (currentY == 1 ))

{

currentY = 5;

jButton[currentY].requestFocus();

break;

}

if ((currentY == 2 ) || (currentY == 3 ))

{

currentY = 6;

jButton[currentY].requestFocus();

break;

}

if (currentY == 4 )

{

currentY = 7;

jButton[currentY].requestFocus();

break;

}

if (currentY == 5 )

{

currentY = 0;

jButton[currentY].requestFocus();

break;

}

if (currentY == 6 )

{

currentY = 2;

jButton[currentY].requestFocus();

break;

}

if (currentY == 7 )

{

currentY = 4;

jButton[currentY].requestFocus();

break;

}

}

case KeyEvent.VK_ENTER:

{

if (( currentY jButton[currentY].setBackground(new Color(X1,Y1,Z1));

chkButton[currentY] = true;

break;

}

if (( currentY jButton[currentY].setBackground(new Color(X,Y,Z));

chkButton[currentY] = false;

break;

}

if ( currentY == 4)

{

String Subscribe = new String("You have choosen to subscribe to");

for (int i = 0; i {

if (chkButton == true)

{

Subscribe = Subscribe + ", " + (jButton .getLabel());

}

}

Subscribe = Subscribe + ".";

System.out.println(Subscribe);

}

if ( currentY == 5)

{

for (int i=0;i {

jButton .setBackground(new Color(X1,Y1,Z1));

chkButton = true;

}

}

if ( currentY == 6)

{

for (int i=0;i {

jButton .setBackground(new Color(X,Y,Z));

chkButton = false;

}

}

if ( currentY == 7)

{

System.out.println("Frame closed");

}

}

}

}

}

Smile1412a at 2007-7-12 8:52:35 > top of Java-index,Desktop,Core GUI APIs...
# 6

Hi camickr and MikeP.

I tried using key binders for the main frame. I'm starting with button key press down. However, the focus doesn't seem to shift over to the next button, and the program will stay there.

But when i use System.out.println("Button focused on " +current);

without the request focus line.

It managed to show which button is suppose to have the focus.

Below is the edited program with key binders

import javax.swing.AbstractAction;

import javax.swing.Action;

import javax.swing.KeyStroke;

import javax.swing.SwingUtilities;

import javax.swing.JPanel;

import javax.swing.JFrame;

import java.awt.GridBagLayout;

import javax.swing.JButton;

import java.awt.GridBagConstraints;

import java.awt.Insets;

import java.awt.event.ActionEvent;

public class MainFrame extends JFrame{

private static final long serialVersionUID = 1;

private JPanel jContentPane = null;

public JButton[] MenuButton = new JButton[8];

{

for (int i = 0; i<8 ; i++)

MenuButton[i] = null;

}

private int current = 0;

private JPanel jContentPane() {

if (jContentPane == null) {

GridBagConstraints gridBagConstraints7 = new GridBagConstraints();

gridBagConstraints7.gridx = 1;

gridBagConstraints7.insets = new Insets(8, 8, 8, 8);

gridBagConstraints7.fill = GridBagConstraints.BOTH;

gridBagConstraints7.gridy = 3;

GridBagConstraints gridBagConstraints6 = new GridBagConstraints();

gridBagConstraints6.gridx = 1;

gridBagConstraints6.insets = new Insets(8, 8, 8, 8);

gridBagConstraints6.fill = GridBagConstraints.BOTH;

gridBagConstraints6.gridy = 2;

GridBagConstraints gridBagConstraints5 = new GridBagConstraints();

gridBagConstraints5.gridx = 1;

gridBagConstraints5.insets = new Insets(8, 8, 8, 8);

gridBagConstraints5.fill = GridBagConstraints.BOTH;

gridBagConstraints5.gridy = 1;

GridBagConstraints gridBagConstraints4 = new GridBagConstraints();

gridBagConstraints4.gridx = 1;

gridBagConstraints4.insets = new Insets(8, 8, 8, 8);

gridBagConstraints4.fill = GridBagConstraints.BOTH;

gridBagConstraints4.gridy = 0;

GridBagConstraints gridBagConstraints3 = new GridBagConstraints();

gridBagConstraints3.gridx = 0;

gridBagConstraints3.insets = new Insets(8, 8, 8, 8);

gridBagConstraints3.fill = GridBagConstraints.BOTH;

gridBagConstraints3.gridy = 3;

GridBagConstraints gridBagConstraints2 = new GridBagConstraints();

gridBagConstraints2.gridx = 0;

gridBagConstraints2.insets = new Insets(8, 8, 8, 8);

gridBagConstraints2.fill = GridBagConstraints.BOTH;

gridBagConstraints2.gridy = 2;

GridBagConstraints gridBagConstraints1 = new GridBagConstraints();

gridBagConstraints1.gridx = 0;

gridBagConstraints1.insets = new Insets(8, 8, 8, 8);

gridBagConstraints1.fill = GridBagConstraints.BOTH;

gridBagConstraints1.gridy = 1;

GridBagConstraints gridBagConstraints = new GridBagConstraints();

gridBagConstraints.gridx = 0;

gridBagConstraints.fill = GridBagConstraints.BOTH;

gridBagConstraints.insets = new Insets(8, 8, 8, 8);

gridBagConstraints.gridy = 0;

jContentPane = new JPanel();

jContentPane.setLayout(new GridBagLayout());

jContentPane.add(getJButton(), gridBagConstraints);

jContentPane.add(getJButton1(), gridBagConstraints1);

jContentPane.add(getJButton2(), gridBagConstraints2);

jContentPane.add(getJButton3(), gridBagConstraints3);

jContentPane.add(getJButton4(), gridBagConstraints4);

jContentPane.add(getJButton5(), gridBagConstraints5);

jContentPane.add(getJButton6(), gridBagConstraints6);

jContentPane.add(getJButton7(), gridBagConstraints7);

jContentPane.getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "MoveDown");

Action ActionDown = new AbstractAction() {

public void actionPerformed(ActionEvent e) {

current = current < 7 ? current = current + 1 : (current + 1 > 7 ? 0:1);

//System.out.println("press down, Button"+current+" on focus");

MenuButton[current].requestFocus(); //next button obtains focus

}

};

jContentPane.getActionMap().put("MoveDown", ActionDown);

}

return jContentPane;

}

private JButton getJButton() {

if (MenuButton[0] == null) {

MenuButton[0] = new JButton();

MenuButton[0].setText("Singapore");

}

return MenuButton[0];

}

private JButton getJButton1() {

if (MenuButton[1] == null) {

MenuButton[1] = new JButton();

MenuButton[1].setText("World");

}

return MenuButton[1];

}

private JButton getJButton2() {

if (MenuButton[2] == null) {

MenuButton[2] = new JButton();

MenuButton[2].setText("Business/Finance");

}

return MenuButton[2];

}

private JButton getJButton3() {

if (MenuButton[3] == null) {

MenuButton[3] = new JButton();

MenuButton[3].setText("Sports");

}

return MenuButton[3];

}

private JButton getJButton4() {

if (MenuButton[4] == null) {

MenuButton[4] = new JButton();

MenuButton[4].setText("Technology");

}

return MenuButton[4];

}

private JButton getJButton5() {

if (MenuButton[5] == null) {

MenuButton[5] = new JButton();

MenuButton[5].setText("Entertainment");

}

return MenuButton[5];

}

private JButton getJButton6() {

if (MenuButton[6] == null) {

MenuButton[6] = new JButton();

MenuButton[6].setText("Special Reports");

}

return MenuButton[6];

}

private JButton getJButton7() {

if (MenuButton[7] == null) {

MenuButton[7] = new JButton();

MenuButton[7].setText("Travel");

}

return MenuButton[7];

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

MainFrame thisClass = new MainFrame();

thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

thisClass.setVisible(true);

}

});

}

public MainFrame() {

super();

initialize();

}

private void initialize() {

this.setSize(1024, 768);

this.setContentPane(jContentPane());

this.setTitle("Daily News");

}

}

Thanks for your time and help

Smile1412a at 2007-7-12 8:52:35 > top of Java-index,Desktop,Core GUI APIs...
# 7

//jContentPane.getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "MoveDown");

jContentPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)

.put(KeyStroke.getKeyStroke("DOWN"), "MoveDown");

Reread the tutorial on Key Bindings to understand the difference between the two InputMaps. The button has focus, not the content pane so you can't use the convenience method to get the InputMap:

camickra at 2007-7-12 8:52:35 > top of Java-index,Desktop,Core GUI APIs...
# 8
Thanks Camickr! You're very helpful. Will look into that.Thanks again
Smile1412a at 2007-7-12 8:52:35 > top of Java-index,Desktop,Core GUI APIs...