Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Using an editable JComboBox  (Read 784 times)
0 Members and 1 Guest are viewing this topic.
Offline King_of_Men

Senior Newbie




EUII player


« Posted 2004-06-14 03:20:09 »

I am trying to permit the player to choose from predefined factions, but allow him to rename his own if it suits him. For this, I'm trying to use an editable JComboBox. However, the box refuses to be edited. That is, it comes up on the screen as it should, and I can select my hardcoded strings; but when I click on it to try editing, it doesn't seem to gain the keyboard focus. Do I need to do anything special, other than call setEditable(true), to make it do so? Here's my code so far :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
   constructor {
      (...)
      familyChoice = Family.getFamilyChoice();
      familyChoice.setBounds(250, 50, 175, 30);
      familyChoice.setVisible(false);
      familyChoice.setEditable(true);
      getLayeredPane().add(familyChoice, JLayeredPane.PALETTE_LAYER);
   }

  // In class Family :
 public static JComboBox getFamilyChoice () {

      String[] temp = new String[allFamilies.length];
      for (int i = 0; i < temp.length; i++)
         temp[i] = allFamilies[i].name;

      return new JComboBox(temp);
   }


I call setVisible(true) in actionPerformed, and it pops up just as it should. I am not overwriting any of the painting methods. Suggestions?

To win one hundred victories, in one hundred battles, is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #1 - Posted 2004-06-14 13:26:52 »

You are calling setEditable correctly, this seems to be spcifically a focus issue.  I noticed you are using LayeredPane stuff.. could that be affecting focus?  Can you use the TAB key to move focus to the control?  I.e. can you use any keyboard actions to select an item in that combo-box?

Offline King_of_Men

Senior Newbie




EUII player


« Reply #2 - Posted 2004-06-14 22:52:35 »

It doesn't seem to be the layeredPane, I get the same result just using the contentPane. Nix on the TAB, though I didn't see what would happen if I wrote a keyboard listener to move the focus around. I did try calling requestFocus in actionPerformed (when a button was clicked), but no dice.

However, I've decided that a TextField, with buttons on either side to traverse the names, is a cooler solution anyway. So it's not a huge priority. Would be nice to know what's going on, though. Let's hope I can give focus to a TextField.  Smiley

To win one hundred victories, in one hundred battles, is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline CaptainJester

JGO Knight


Medals: 10
Projects: 2


Make it work; make it better.


« Reply #3 - Posted 2004-06-16 11:39:44 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
import javax.swing.*;

public class TestApp extends JFrame {
      public TestApp() {
            Container c = getContentPane();
            JComboBox jc = new JComboBox();
            c.add(jc, BorderLayout.NORTH);
            jc.setEditable(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            pack();
            show();
      }
      public static void main(String args[]) {
            TestApp t = new TestApp();
      }
}


This worked for me.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (78 views)
2013-05-17 21:29:12

alaslipknot (89 views)
2013-05-16 21:24:48

gouessej (121 views)
2013-05-16 00:53:38

gouessej (113 views)
2013-05-16 00:17:58

theagentd (126 views)
2013-05-15 15:01:13

theagentd (113 views)
2013-05-15 15:00:54

StreetDoggy (156 views)
2013-05-14 15:56:26

kutucuk (179 views)
2013-05-12 17:10:36

kutucuk (179 views)
2013-05-12 15:36:09

UnluckyDevil (186 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.136 seconds with 20 queries.