Apolyton Archive  |  Preserved copy of the Apolyton Civilization Site and its forums as they stood in September 2005. Read-only; nothing here can be posted to or replied to.  |  Forum index |  About this archive |  The 1998–2001 UBB forums
Today on Apolyton WARDELL INTERVIEW PROMO A.C.S. HISTORY CHAPTER 4 GET CIV4 /w FREE PLUS! A.C.S. PHOTO GALLERY GET A.O.M. V1.1
Apolyton Civilization Forums
main| civ2| civ3| civ4| smac| ctp2| ron| moo3| galciv| galciv2| alt| about|
ApolytonPLUS | register | search | faq | new posts | pm (-/-) | upload | members
hall of fame new! | civgroups | civgroups news | interviews | the column | radio | chat | directory | news | store | PLUS
Apolyton Civilization Forums : Powered by vBulletin version 2.0.3 Apolyton Civilization Forums > Alternative Civs > Clash of Civilizations > Coding Problem: I can't get a KeyListener to work... Suggestions?
Show a Printable Version | Email This Page to Someone! | Receive updates to this thread | Report this to Apolyton news!
04.Sep: `FC` 2.0.5 COMPLETED AND RELEASED
27.Jul: `FC` 2.0.4 COMPLETED AND RELEASED
16.Jul: `FC` 2.0.3 COMPLETED AND RELEASED

bottom of page
  
Author
Thread    < Last Thread     Next Thread > Post New Thread     Post A Reply
Mark_Everson is offline Mark_Everson
Clash of Civilizations Project Lead
Canton, MI
Jan 1970
time: 00:13
Question  Old Post 10-10-1999 20:26 Visit Mark_Everson's homepage!
Edit/Delete Message Reply w/Quote
#1 Report this post to a moderator
Coding Problem: I can't get a KeyListener to work... Suggestions? Support Apolyton

Looking for suggestions on what I'm doing wrong from someone in the group who knows
java better than I. The idea is to let keyPress events that happen when the ToolBar
is the focus get passed on to the map which is where the key press events would be used.
This is just so if you change the focus to the tool bar by pushing a button on it, that
you can still enter commands to the map. But my key events vanish into the void....
KeyPressed is never called... Here's the code, I've made the relevant parts
bold. What am I screwing up?
I haven't done any of this stuff in Ages so its probably a stupid error

Thanks,

Mark

code:
/* class ToolBarFrame,[ PI ](aka Toolbar) is designed to always be available. It has Buttons for: 1. new turn (and x10, x100), [FI] 2. map button to generate local maps, [FI] 3. tech to access tech devel chart for the civ, [PI] and 4. Econ button [PI] 5. Diplomacy [P] 6. Government [P] 7. Other Buttons… whatever’s useful 8. Pull Downs for various utility functions - suggestions? [P] */ import java.awt.*; import java.awt.event.*; public class ToolBarFrame extends GameFrame implements KeyListener{ void autoPilotCheckbox_Action(java.awt.event.ItemEvent event) { Game.setAutoPilot(autoPilotCheckbox.getState()); } void econButton_Action(java.awt.event.ActionEvent event) { Game.getPlayerCiv().civEconStrat[0].editEconStrategy(); } void tenTurnButton_Action(java.awt.event.ActionEvent event) { Game.tenTurn(); } void toTechButton_Action(java.awt.event.ActionEvent event) { (new TechFrame( Game.getPlayerCiv().provs[1])).show(); } void toMapButton_Action(java.awt.event.ActionEvent event) { (new MapSquaresFrame(20,15)).show(); } void oneTurnButton_Action(java.awt.event.ActionEvent event) { Game.newTurn(); } void hundredTurnButton_Action(java.awt.event.ActionEvent event) { Game.hundredTurn(); } void Open_Action(java.awt.event.ActionEvent event) { openFileDialog1.show(); } void About_Action(java.awt.event.ActionEvent event) { (new AboutDialog(this, true)).show(); } void Exit_Action(Event event) { (new QuitDialog(this, true)).show(); } ////////////////////////////////////////////////////////////////////////////////////////// // // kludge to show turn number void refresh(){ int strLoc = 0; int moneyHere = (new Float(Game.getPlayerCiv().money)).intValue(); if((strLoc = getTitle().indexOf("Turn:")) > 0) setTitle((getTitle()).substring(0,strLoc) + "Turn:"+Game.getTurnNumber() + ", $= " + moneyHere + ", Export: " + (int)Game.getPlayerCiv().totalExportAvail + " Mil. Power: "+ (int) Game.getPlayerCiv().getTotalMilPower()+" / " + (int) Game.getPlayerCiv().coreAI.offensivePower); } public ToolBarFrame() { //{{INIT_CONTROLS setLayout(new BorderLayout(0,0)); setForeground(java.awt.Color.blue); setSize(512,70); setVisible(false); openFileDialog1.setMode(FileDialog.LOAD); openFileDialog1.setTitle("Open"); //$$ openFileDialog1.move(12,96); checkbox1.setLabel("checkbox"); add(BorderLayout.CENTER, checkbox1); checkbox1.setBounds(0,48,512,53); checkbox1.setVisible(false); // toolBarPanel1.setAlignmentY(0.363636F); add(BorderLayout.NORTH, toolBarPanel1); toolBarPanel1.setBackground(java.awt.Color.lightGray); toolBarPanel1.setBounds(0,0,512,48); oneTurnButton.setLabel("1 Turn"); oneTurnButton.setActionCommand("1 Turn"); toolBarPanel1.add(oneTurnButton); oneTurnButton.setBounds(0,0,44,40); tenTurnButton.setLabel("10 Turns"); tenTurnButton.setActionCommand("10 Turns"); toolBarPanel1.add(tenTurnButton); tenTurnButton.setBounds(54,0,45,40); hundredTurnButton.setLabel("100 Turns"); hundredTurnButton.setActionCommand("100 Turns"); toolBarPanel1.add(hundredTurnButton); hundredTurnButton.setBounds(109,0,56,40); toMapButton.setLabel("Map"); toMapButton.setActionCommand("Map"); toolBarPanel1.add(toMapButton); toMapButton.setBounds(185,0,44,40); toTechButton.setLabel("Tech"); toTechButton.setActionCommand("Tech"); toolBarPanel1.add(toTechButton); toTechButton.setBounds(239,0,44,40); econButton.setLabel("Econ"); econButton.setActionCommand("Econ"); toolBarPanel1.add(econButton); econButton.setBounds(293,0,45,40); autoPilotCheckbox.setState(false); autoPilotCheckbox.setLabel("Auto AI"); toolBarPanel1.add(autoPilotCheckbox); autoPilotCheckbox.setFont(new Font("Dialog", Font.BOLD, 14)); autoPilotCheckbox.setBounds(388,8,62,24); haltCheckbox.setLabel("HALT"); toolBarPanel1.add(haltCheckbox); haltCheckbox.setBounds(435,8,60,23); setTitle(" Turn: 0"); //}} //{{INIT_MENUS menu1.setLabel("File"); menu1.add("New"); menu1.add(miOpen); miOpen.setLabel("Open..."); menu1.add("Save"); menu1.add("Save As..."); menu1.addSeparator(); menu1.add(miSystem); miSystem.setLabel("Exit"); mainMenuBar.add(menu1); menu2.setLabel("Edit"); menu2.add("Cut"); menu2.add("Copy"); menu2.add("Paste"); mainMenuBar.add(menu2); menu3.setLabel("Help"); menu3.add(miAbout); miAbout.setLabel("About"); mainMenuBar.add(menu3); mainMenuBar.setHelpMenu(menu3); //$$ mainMenuBar.move(60,96); setMenuBar(mainMenuBar); //}} //{{REGISTER_LISTENERS addKeyListener(this); Action lAction = new Action(); hundredTurnButton.addActionListener(lAction); oneTurnButton.addActionListener(lAction); toMapButton.addActionListener(lAction); toTechButton.addActionListener(lAction); tenTurnButton.addActionListener(lAction); econButton.addActionListener(lAction); Item lItem = new Item(); autoPilotCheckbox.addItemListener(lItem); miOpen.addActionListener(lAction); miAbout.addActionListener(lAction); miSystem.addActionListener(lAction); //}} } public ToolBarFrame(String title) { this(); setTitle(title); } public synchronized void show() { move(0, 0); super.show(); } public void keyTyped(KeyEvent e){ } public void keyReleased(KeyEvent e){ } /** Doesn't work, Idea is Key presses when tool bar has focus will be sent to a MapCanvas */ public void keyPressed(KeyEvent e){ IO.out.println("In key pressed of tool bar frame a "); if( mapCanvasToSendKeyPressesTo == null ){ mapCanvasToSendKeyPressesTo = GUI.findMapCanvasToSendKeyPressesTo(); IO.out.println("In key pressed of tool bar frame b "); } if( mapCanvasToSendKeyPressesTo != null ){ mapCanvasToSendKeyPressesTo.keyPressedFromGUI(e); IO.out.println("In key pressed of tool bar frame c "); } IO.out.println("In key pressed of tool bar frame d "); } //{{DECLARE_CONTROLS java.awt.FileDialog openFileDialog1 = new java.awt.FileDialog(this); java.awt.Checkbox checkbox1 = new java.awt.Checkbox(); Panel toolBarPanel1 = new Panel(); Button oneTurnButton = new Button(); Button tenTurnButton = new Button(); Button hundredTurnButton = new Button(); Button toMapButton = new Button(); Button toTechButton = new Button(); Button econButton = new Button(); java.awt.Checkbox autoPilotCheckbox = new java.awt.Checkbox(); java.awt.Checkbox haltCheckbox = new java.awt.Checkbox(); MapCanvas mapCanvasToSendKeyPressesTo = null; //}} //{{DECLARE_MENUS java.awt.MenuBar mainMenuBar = new java.awt.MenuBar(); java.awt.Menu menu1 = new java.awt.Menu(); java.awt.MenuItem miOpen = new java.awt.MenuItem(); java.awt.MenuItem miSystem = new java.awt.MenuItem(); java.awt.Menu menu2 = new java.awt.Menu(); java.awt.Menu menu3 = new java.awt.Menu(); java.awt.MenuItem miAbout = new java.awt.MenuItem(); //}} class Action implements java.awt.event.ActionListener { public void actionPerformed(java.awt.event.ActionEvent event) { Object object = event.getSource(); if (object == hundredTurnButton) hundredTurnButton_Action(event); else if (object == oneTurnButton) oneTurnButton_Action(event); else if (object == toMapButton) toMapButton_Action(event); else if (object == toTechButton) toTechButton_Action(event); else if (object == tenTurnButton) tenTurnButton_Action(event); else if (object == econButton) econButton_Action(event); else if (object == miOpen) Open_Action(event); else if (object == miAbout) About_Action(event); else if (object == miSystem) System.exit(0); //System(event); } } class Item implements java.awt.event.ItemListener { public void itemStateChanged(java.awt.event.ItemEvent event) { Object object = event.getSource(); if (object == autoPilotCheckbox) autoPilotCheckbox_Action(event); } } }


[This message has been edited by Mark_Everson (edited October 10, 1999).]
[This message has been edited by Mark_Everson (edited October 12, 1999).]

JimC is offline JimC
Chieftain
Birmingham, England
May 1999
time: 05:13
Lightbulb  Old Post 11-10-1999 16:04 Visit JimC's homepage!
Edit/Delete Message Reply w/Quote
#2 Report this post to a moderator
Inflate your Upload Space

I haven't used KeyListeners for a while, but I seem to remember having a problem with them before.

All I can think of is - try adding the KeyListener to a Panel/Applet within a Frame, instead of to a Frame directly.

Jim

Mark_Everson is offline Mark_Everson
Clash of Civilizations Project Lead
Canton, MI
Jan 1970
time: 00:13
Thumbs up  Old Post 11-10-1999 16:55 Visit Mark_Everson's homepage!
Edit/Delete Message Reply w/Quote
#3 Report this post to a moderator
Support Apolyton or Terrorists Win

Hey Jim:

That's a good idea. I actually thought of it, but then forgot it in the midst of trying other things. That may be it...

Ferdi is offline Ferdi
Warlord
Europe, Brussels
Oct 1999
time: 06:13
Post  Old Post 11-10-1999 17:57
Edit/Delete Message Reply w/Quote
#4 Report this post to a moderator
Support Apolyton buy from Amazon


If I am not mistaken, you have to add a MapCanvas object as KeyListener to each ToolBarFrame's component which is focus traversable. So, when the focus is on a ToolBarFrame's button its key events are caught by the MapCanvas object.

Mark_Everson is offline Mark_Everson
Clash of Civilizations Project Lead
Canton, MI
Jan 1970
time: 00:13
Smile  Old Post 11-10-1999 23:36 Visit Mark_Everson's homepage!
Edit/Delete Message Reply w/Quote
#5 Report this post to a moderator
Support Apolyton, buy Civilization III: Complete

Hi Ferdi:

Good Call, I'm sure that's it! And that's also why I and JimC have had better luck with Canvases, they don't have all those buttons and such to steal the focus!

Thanks for the help. Interested in being the Java Jock I need to help figure out a good distribution scheme for the demo?

-Mark

Mark_Everson is offline Mark_Everson
Clash of Civilizations Project Lead
Canton, MI
Jan 1970
time: 00:13
Smile  Old Post 12-10-1999 07:54 Visit Mark_Everson's homepage!
Edit/Delete Message Reply w/Quote
#6 Report this post to a moderator
Suffering from ads?

Thanks Ferdi, that was indeed the problem.

Mark

Ferdi is offline Ferdi
Warlord
Europe, Brussels
Oct 1999
time: 06:13
Post  Old Post 12-10-1999 12:05
Edit/Delete Message Reply w/Quote
#7 Report this post to a moderator
Tired of ads?

Mark,

Sure, your job is great, I've read quicky what you are about to include into The Clash of Civilizations and I have only one word : Excellent!
Yes, I am interested to help you but I fear not to have the time to do that. I am very busy for the moment, only a few minutes in the morning to read some posts in this forum!
Anyway, if you have any question in Java I can try to answer you but the reply could take a while...(we are not in the same time zone)
Also don't forget that English isn't my native tongue (French) and it's more difficult for me to tell you with the good words what I have in my mind .

Good luck,
Ferdi

Mark_Everson is offline Mark_Everson
Clash of Civilizations Project Lead
Canton, MI
Jan 1970
time: 00:13
Post  Old Post 12-10-1999 16:16 Visit Mark_Everson's homepage!
Edit/Delete Message Reply w/Quote
#8 Report this post to a moderator
Support Apolyton, buy Civilization 2

Ferdi:

Well thank you for the kind words, and the offer. Right now I don't have any quick questions, but will keep you in mind. I hope you get some time free, I would love to be able to drag you into the project. If you have any takes on what we're doing wrong with the demos I'd certainly like to hear what you have to say.

bonne chance,

Mark

  < Last Thread     Next Thread > Post New Thread     Post A Reply
All times are GMT. The time now is 05:13.
Apolyton Time is 00:13.
    top of page
Rate This Thread:
Forum Jump:
Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is ON
vB code is ON
Smilies are ON
[IMG] code is ON
 




Contact Us - Apolyton Civilization Site - Support Us!

Building a better Apolyton through better information. Click here and take our poll!
Non-US visitors, click here!

Powered by: vBulletin Version 2.0.3
Copyright ©2000, 2001, Jelsoft Enterprises Limited.

Page generated in 0.0448 seconds (88.54% PHP - 11.46% MySQL) with 30 queries
Page Loading Time:

Support Apolyton: Amazon USA | Amazon UK | Amazon DE | Amazon FR |
Support Apolyton and get FREE PLUS, Buy from Chips&Bits: Galactic Civilizations | Galactic Civilizations: Deluxe Edition | Call to Power 2 | Civilization: The Boardgame | GURPS/ Alpha Centauri | Alpha Centauri | Civilization IV | Civilization III: Complete |


Front Page | Civilization IV | Civilization III | Civilization II | Call to Power II | Alpha Centauri | Master of Orion III
Rise of Nations | Galactic Civilizations | Galactic Civilizations II | Misc
Alt.Civs | Civ I | C:CtP I | About | News | Directory | Apolyton Store | Forums | Chat | Columns | Interviews | Newsletter
Scenario League | CSC | Clash of Civs | Spanish Site | CtP Maps | Cradle of Civ | WesW's Ctp1/2 Site | Civ3 Haven

apolyton.net | apolyton.com | civilization2.net | civilization3.net | civilization4.net | civilizationiv.info | calltopower.net | galciv.net | galciv2.net | moo3.net