asking urgent help for UI problems

Hope to get any reply and help from you!

Now, as for the UIs we want to get, we require that when users press one button on UI1 with "Enter" in keyboard(not using mouse), another UI2 would appear in front. Moreover, when users press "ESC" in keyboard, it can also return back from UI2 to UI1.

However, we have some following problems as below:

1. Now we use the following way for ui programming. However, we always find that the UI would be dead without any response for any key press. What's wrong with the following porgramming style and way:

//UI1 class

...

dispse();

UI2 ui2 =new UI2();

ui2.setVisible(true);

...

//UI2 class

...

dispse();

UI1 ui1 =new UI1();

ui1.setVisible(true);

Many thanks for you! I hope it would not cost you much time!

best wishes for you!

[1067 byte] By [Smile1412a] at [2007-11-27 8:41:04]
# 1
have you set the proper Listeners for each UI, that will listen for user-generated keyboard events?
lem@phila at 2007-7-12 20:39:51 > top of Java-index,Desktop,Core GUI APIs...
# 2
Hi there. We're currently using different Key binders for both frames. There will only be 1 active frame, which is either ui1 or ui2.
Smile1412a at 2007-7-12 20:39:51 > top of Java-index,Desktop,Core GUI APIs...
# 3
can you post all relevant and related code regarding the matter...
lem@phila at 2007-7-12 20:39:51 > top of Java-index,Desktop,Core GUI APIs...
# 4

Sorry if the code is rather long. However most of them are for the design of GUI only.

Thanks in advance.

import javax.swing.SwingUtilities;

import java.awt.Color;

import java.awt.Font;

import javax.swing.AbstractAction;

import javax.swing.Action;

import javax.swing.JDialog;

import javax.swing.JPanel;

import javax.swing.JFrame;

import javax.swing.JButton;

import javax.swing.KeyStroke;

import java.awt.Rectangle;

import java.awt.event.ActionEvent;

import java.awt.event.KeyEvent;

import javax.swing.JLabel;

public class sample extends JFrame

{

private static final long serialVersionUID = 1;

private JPanel jContentPane = null;

private JButton[] EntButton = new JButton[3];

private JLabel jLabel = null;

int current = 0;

Font originalfont = new Font("serif", Font.BOLD, 20);

Font changedfont = new Font("serif", Font.BOLD, 30);

Color blue = new Color (0,0,255);

Color violet = new Color (182,122,132);

Color red = new Color (255,0,0);

Color black = new Color (0,0,0);

private JButton getJButton() {

EntButton[0] = new JButton();

EntButton[0].setText("Movies");

EntButton[0].setBounds(new Rectangle(256, 190, 180, 40));

EntButton[0].setBackground(blue);

EntButton[0].setFont(originalfont);

EntButton[0].setForeground(red);

return EntButton[0];

}

private JButton getJButton1() {

EntButton[1] = new JButton();

EntButton[1].setBackground(violet);

EntButton[1].setBounds(new Rectangle(280, 250, 135, 31));

EntButton[1].setFont(originalfont);

EntButton[1].setText("TV Series");

return EntButton[1];

}

private JButton getJButton2() {

EntButton[2] = new JButton();

EntButton[2].setBackground(violet);

EntButton[2].setBounds(new Rectangle(280, 310, 135, 31));

EntButton[2].setFont(originalfont);

EntButton[2].setText("News");

return EntButton[2];

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

sample thisClass = new sample();

thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

thisClass.setVisible(true);

}

});

}

public sample() {

super();

initialize();

}

private void initialize() {

this.setSize(1024,768);

this.setContentPane(getJContentPane());

//this.setUndecorated(true);

EntButton[0].requestFocus(true);

EntButton[0].setFocusPainted(false);

}

private JPanel getJContentPane() {

if (jContentPane == null) {

jLabel = new JLabel();

jLabel.setBounds(new Rectangle(345, 130, 135, 31));

jLabel.setText("Video");

jContentPane = new JPanel();

jContentPane.setLayout(null);

jContentPane.add(getJButton(), null);

jContentPane.add(getJButton1(), null);

jContentPane.add(getJButton2(), null);

jContentPane.add(jLabel, null);

jContentPane.getInputMap(jContentPane.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,0), "MoveDown"); //KeyStroke.getKeyStroke("DOWN")

Action ActionDown = new AbstractAction() {

public void actionPerformed(ActionEvent e)

{

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

EntButton[current].setBackground(blue);

EntButton[current].setFont(changedfont);

EntButton[current].setForeground(red);

if (current==1){

EntButton[1].setBounds(new Rectangle(256, 250, 180, 40));

EntButton[0].setBackground(violet);

EntButton[0].setFont(originalfont);

EntButton[0].setForeground(black);

EntButton[0].setBounds(new Rectangle(280, 190, 135, 31));

}

if (current==2){

EntButton[2].setBounds(new Rectangle(256, 310, 180, 40));

EntButton[1].setBackground(violet);

EntButton[1].setFont(originalfont);

EntButton[1].setForeground(black);

EntButton[1].setBounds(new Rectangle(280, 250, 135, 31));

}

if (current==0){

EntButton[0].setBounds(new Rectangle(256, 190, 180, 40));

EntButton[2].setBackground(violet);

EntButton[2].setFont(originalfont);

EntButton[2].setForeground(black);

EntButton[2].setBounds(new Rectangle(280, 310, 135, 31));

}

}

};

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

jContentPane.getInputMap(jContentPane.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP,0), "MoveUp");

Action ActionUp = new AbstractAction() {

public void actionPerformed(ActionEvent e)

{

current = current < 3 ? current = current - 1 : (current - 1 > 0 ? 1:0);

if (current == -1)

current = 2;

EntButton[current].setBackground(blue);

EntButton[current].setFont(changedfont);

EntButton[current].setForeground(red);

if (current==0){

EntButton[0].setBounds(new Rectangle(256, 190, 180, 40));

EntButton[1].setBackground(violet);

EntButton[1].setFont(originalfont);

EntButton[1].setForeground(black);

EntButton[1].setBounds(new Rectangle(280, 250, 135, 31));

}

if (current==1){

EntButton[1].setBounds(new Rectangle(256, 250, 180, 40));

EntButton[2].setBackground(violet);

EntButton[2].setFont(originalfont);

EntButton[2].setForeground(black);

EntButton[2].setBounds(new Rectangle(280, 310, 135, 31));

}

if (current==2){

EntButton[2].setBounds(new Rectangle(256, 310, 180, 40));

EntButton[0].setBackground(violet);

EntButton[0].setFont(originalfont);

EntButton[0].setForeground(black);

EntButton[0].setBounds(new Rectangle(280, 190, 135, 31));

}

}

};

jContentPane.getActionMap().put("MoveUp", ActionUp);

jContentPane.getInputMap(jContentPane.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("ESCAPE"), "MoveESC");

Action ActionESC = new AbstractAction() {

public void actionPerformed(ActionEvent e)

{

System.exit(0);

}

};

jContentPane.getActionMap().put("MoveESC", ActionESC);

jContentPane.getInputMap(jContentPane.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0), "MoveEnter");

Action ActionEnter = new AbstractAction() {

public void actionPerformed(ActionEvent e)

{

if(current==0)

{

dispose();

//JDialog MoviesMenu = new MoviesMenu();

MoviesMenu MoviesMenu = new MoviesMenu();

MoviesMenu.show();

//MoviesMenu.setVisible(true);

}

}

};

jContentPane.getActionMap().put("MoveEnter", ActionEnter);

}

return jContentPane;

}

//second UI start

public class MoviesMenu extends JDialog {

private static final long serialVersionUID = 1;

private JPanel jMovieContentPane;

private JButton[] MoviesButton = new JButton[3];

private JLabel jLabel,jLabel1;

private int current1 = 0;

private JButton getJButton() {

MoviesButton[0] = new JButton();

MoviesButton[0].setBounds(new Rectangle(110, 230, 450, 40));

MoviesButton[0].setText("Vid 1");

MoviesButton[0].setBackground(blue);

MoviesButton[0].setFont(changedfont);

MoviesButton[0].setForeground(red);

return MoviesButton[0];

}

private JButton getJButton1() {

MoviesButton[1] = new JButton();

MoviesButton[1].setBounds(new Rectangle(135, 320, 400, 46));

MoviesButton[1].setText("Vid 2");

MoviesButton[1].setBackground(violet);

MoviesButton[1].setFont(originalfont);

return MoviesButton[1];

}

private JButton getJButton2() {

MoviesButton[2] = new JButton();

MoviesButton[2].setBounds(new Rectangle(135, 410, 400, 46));

MoviesButton[2].setText("Vid 3");

MoviesButton[2].setBackground(violet);

MoviesButton[2].setFont(originalfont);

return MoviesButton[2];

}

public MoviesMenu() {

super();

initialize();

}

private void initialize() {

this.setSize(1024,768);

this.setContentPane(getjMovieContentPane());

this.setTitle("JFrame");

this.setUndecorated(true);

MoviesButton[0].requestFocus(true);

MoviesButton[0].setFocusPainted(false);

}

private JPanel getjMovieContentPane() {

if (jMovieContentPane == null) {

jLabel1 = new JLabel();

jLabel1.setBounds(new Rectangle(650, 270, 361, 226));

jLabel1.setText("Text Introduction");

jLabel = new JLabel();

jLabel.setBounds(new Rectangle(650, 105, 226, 136));

jLabel.setText("VIDEO IMAGE");

jMovieContentPane = new JPanel();

jMovieContentPane.setLayout(null);

jMovieContentPane.add(getJButton(), null);

jMovieContentPane.add(getJButton1(), null);

jMovieContentPane.add(getJButton2(), null);

jMovieContentPane.add(jLabel, null);

jMovieContentPane.add(jLabel1, null);

jMovieContentPane.getInputMap(jMovieContentPane.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,0), "MoveDown"); //KeyStroke.getKeyStroke("DOWN")

Action ActionDown = new AbstractAction() {

public void actionPerformed(ActionEvent e)

{

current1++;

if (current1==0){

MoviesButton[0].setBounds(new Rectangle(110, 230, 450, 40));

MoviesButton[2].setBackground(violet);

MoviesButton[2].setFont(originalfont);

MoviesButton[2].setForeground(black);

MoviesButton[2].setBounds(new Rectangle(135, 410, 400, 46));

MoviesButton[2].repaint();

}

if (current1==1){

MoviesButton[1].setBounds(new Rectangle(110, 320, 450, 46));

MoviesButton[0].setBackground(violet);

MoviesButton[0].setFont(originalfont);

MoviesButton[0].setForeground(black);

MoviesButton[0].setBounds(new Rectangle(135, 230, 400, 40));

MoviesButton[0].repaint();

}

if (current1==2){

MoviesButton[2].setBounds(new Rectangle(110, 410, 450, 46));

MoviesButton[1].setBackground(violet);

MoviesButton[1].setFont(originalfont);

MoviesButton[1].setForeground(black);

MoviesButton[1].setBounds(new Rectangle(135, 320, 400, 46));

MoviesButton[1].repaint();

}

if(current1>2){

current1=0;

MoviesButton[0].setBounds(new Rectangle(110, 230, 450, 40));

MoviesButton[2].setBackground(violet);

MoviesButton[2].setFont(originalfont);

MoviesButton[2].setForeground(black);

MoviesButton[2].setBounds(new Rectangle(135, 410, 400, 46));

MoviesButton[2].repaint();

}

MoviesButton[current1].setBackground(blue);

MoviesButton[current1].setFont(changedfont);

MoviesButton[current1].setForeground(red);

MoviesButton[current1].repaint();

}

};

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

jMovieContentPane.getInputMap(jMovieContentPane.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP,0), "MoveUp");

Action ActionUp = new AbstractAction() {

public void actionPerformed(ActionEvent e)

{

current1--;

if (current1==2){

MoviesButton[2].setBounds(new Rectangle(110, 410, 450, 46));

MoviesButton[0].setBackground(violet);

MoviesButton[0].setFont(originalfont);

MoviesButton[0].setForeground(black);

MoviesButton[0].setBounds(new Rectangle(135, 230, 400, 40));

MoviesButton[0].repaint();

}

if (current1==0){

MoviesButton[0].setBounds(new Rectangle(110, 230, 450, 40));

MoviesButton[1].setBackground(violet);

MoviesButton[1].setFont(originalfont);

MoviesButton[1].setForeground(black);

MoviesButton[1].setBounds(new Rectangle(135, 320, 400, 46));

MoviesButton[1].repaint();

}

if (current1==1){

MoviesButton[1].setBounds(new Rectangle(110, 320, 450, 46));

MoviesButton[2].setBackground(violet);

MoviesButton[2].setFont(originalfont);

MoviesButton[2].setForeground(black);

MoviesButton[2].setBounds(new Rectangle(135, 410, 400, 46));

MoviesButton[2].repaint();

}

if(current1==-1){

current1=2;

MoviesButton[2].setBounds(new Rectangle(110, 410, 450, 46));

MoviesButton[0].setBackground(violet);

MoviesButton[0].setFont(originalfont);

MoviesButton[0].setForeground(black);

MoviesButton[0].setBounds(new Rectangle(135, 230, 400, 40));

MoviesButton[0].repaint();

}

MoviesButton[current1].setBackground(blue);

MoviesButton[current1].setFont(changedfont);

MoviesButton[current1].setForeground(red);

MoviesButton[current1].repaint();

}

};

jMovieContentPane.getActionMap().put("MoveUp", ActionUp);

jMovieContentPane.getInputMap(jMovieContentPane.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("ESCAPE"), "MoveESC");

Action ActionESC = new AbstractAction() {

public void actionPerformed(ActionEvent e)

{

dispose();

JFrame sample = new sample();

//sample.validate();

sample.show();

}

};

jMovieContentPane.getActionMap().put("MoveESC", ActionESC);

}

return jMovieContentPane;

}

}

}

Smile1412a at 2007-7-12 20:39:51 > top of Java-index,Desktop,Core GUI APIs...
# 5

//UI1 class

...

dispse();

UI2 ui2 = new UI2();

ui2.setVisible(true);

...

//UI2 class

...

dispse();

UI1 ui1 = new UI1();

ui1.setVisible(true);

perhaps the creation of new Objects all the time is causing the problem(?).

try it this way (simple demo version)

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class MoviesFrame

{

Vid1Dialog v1d;

public void buildGUI()

{

final JFrame f = new JFrame("Movies Frame");

v1d = new Vid1Dialog();

v1d.buildGUI(f);

f.setSize(400,300);

f.setLocationRelativeTo(null);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false);

Action enterAction = new AbstractAction(){

public void actionPerformed(ActionEvent e){

f.setVisible(false);

v1d.d.setVisible(true);

}

};

f.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(enter, "ENTER");

f.getRootPane().getActionMap().put("ENTER", enterAction);

f.setVisible(true);

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new Runnable(){

public void run(){

new MoviesFrame().buildGUI();

}

});

}

}

class Vid1Dialog

{

JDialog d;

public void buildGUI(final Frame parent)

{

d = new JDialog(parent);

d.setTitle("Vid 1 Dialog");

d.setModal(true);

d.setSize(200,100);

d.setLocationRelativeTo(parent);

d.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

KeyStroke escape = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);

Action escapeAction = new AbstractAction(){

public void actionPerformed(ActionEvent e){

d.setVisible(false);

parent.setVisible(true);

}

};

d.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escape, "ESCAPE");

d.getRootPane().getActionMap().put("ESCAPE", escapeAction);

}

}

Michael_Dunna at 2007-7-12 20:39:51 > top of Java-index,Desktop,Core GUI APIs...
# 6
Thanks for helping out
Smile1412a at 2007-7-12 20:39:51 > top of Java-index,Desktop,Core GUI APIs...