Article View: borland.public.jbuilder.java.language
Article #18377JComboBox and JApplet
From: Andrew Akins
Date: Sat, 04 Jul 1998 00:00
Date: Sat, 04 Jul 1998 00:00
295 lines
10041 bytes
10041 bytes
I earlier had a problem with a JComboBox, not behaving properly with
mouse clicks. Basically, when I click on the down arrow, the list drops
down. As I scroll the mouse, list items highlight. However, when I click
on an item it does not select it (place it in the edit control). You can
use the arrow keyboard keys, and it works fine. I have checked for
heavyweight components interfering, but there are none.
I have, however, discovered a connection. If I change the program from
an applet/application (based on JApplet) to an application only (based
on JFrame)....it works perfectly.
I have included the entire source code for my applet...some of the
auxillary classes, of course, are not included...but you should be able
to see the structure of my code. When Linguistics is extended from
JApplet, mouse clicks in the JComboBox don't work correctly. When
Linguistics is extended from CenteredFrame (which is extended from
JFrame), the JComboBox works. For the record, IntegerField is extended
from JTextField.
Any comments would be appreciated. I want this to be extended from
JApplet so it can be used as both an applet and a application.
Heres the code:
public class Linguistics extends JApplet implements ActionListener,
ItemListener {
//Construct the applet
public Linguistics() {
words=null;
LoadLanguages();
wordListModel = new WordListModel();
currentLanguage = null;
}
//Construct the applet
public Linguistics(CenteredFrame frame) {
this();
parent = frame;
frame.setTitle("Linguistics");
frame.setIcon("../applet/linguisticsicon.gif");
}
//Construct the applet
private void LoadLanguages() {
languageNames = new String[7];
languageNames[0] = "Aslan";
languageNames[1] = "Darrian";
languageNames[2] = "Droyne";
languageNames[3] = "K'Kree";
languageNames[4] = "Vargr";
languageNames[5] = "Vilani";
languageNames[6] = "Zhodani";
languageClasses = new String[7];
languageClasses[0] = "idex.aslan.AslanLanguae";
languageClasses[1] = "idex.darrian.DarrianLanguage";
languageClasses[2] = "idex.droyne.DroyneLanguage";
languageClasses[3] = "idex.kkree.KKreeLanguage";
languageClasses[4] = "idex.vargr.VargrLanguage";
languageClasses[5] = "idex.vilani.VilaniLanguage";
languageClasses[6] = "idex.zhodani.ZhodaniLanguage";
}
//Initialize the applet
public void init() {
JMenuBar menuBar;
JMenu menuFile;
JMenuItem menuFilePrint;
JMenuItem menuFileExit;
JMenu menuEdit;
JMenuItem menuEditGenerate;
JMenuItem menuEditCopy;
Container c = getContentPane();
int loop;
menuBar = new JMenuBar();
if ( isStandalone ) {
menuFile = new JMenu("File");
menuFilePrint = new JMenuItem("Print list");
menuFilePrint.addActionListener(this);
menuFile.add(menuFilePrint);
menuFile.add(new JSeparator());
menuFileExit = new JMenuItem("Exit");
menuFileExit.addActionListener(this);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
}
menuEdit = new JMenu("Edit");
menuEditGenerate = new JMenuItem("Generate");
menuEditGenerate.addActionListener(this);
menuEdit.add(menuEditGenerate);
if ( isStandalone ) {
menuEdit.add(new JSeparator());
menuEditCopy = new JMenuItem("Copy list");
menuEditCopy.addActionListener(this);
menuEdit.add(menuEditCopy);
}
menuBar.add(menuEdit);
setJMenuBar(menuBar);
c.setLayout(new BorderLayout(2,2));
wordList = new JList(wordListModel);
JScrollPane pane = new JScrollPane(wordList);
c.add(pane, BorderLayout.CENTER);
JPanel langChoice = new JPanel();
langChoice.setLayout( new GridLayout(2,2,4,4));
langChoice.add(new JLabel(" Language: "));
languagesList = new JComboBox();
for (loop = 0; loop < languageNames.length; loop++) {
languagesList.addItem(languageNames[loop]);
}
languagesList.setSelectedIndex(0);
languagesList.addItemListener(this);
langChoice.add(languagesList);
langChoice.add(new JLabel(" Number: "));
count = new IntegerField(10,5,false);
langChoice.add(count);
c.add(langChoice, BorderLayout.NORTH);
JPanel bottom = new JPanel();
bottom.setLayout( new BorderLayout(2,2));
JPanel controls = new JPanel();
if ( isStandalone ) {
controls.setLayout(new GridLayout(2,2,6,6));
JButton button = new JButton("Generate");
button.addActionListener(this);
controls.add(button);
button = new JButton("Copy list");
button.addActionListener(this);
controls.add(button);
button = new JButton("Print list");
button.addActionListener(this);
controls.add(button);
} else {
controls.setLayout(new GridLayout(2,1,6,6));
JButton button = new JButton("Generate");
button.addActionListener(this);
controls.add(button);
}
bottom.add(controls, BorderLayout.CENTER);
statusBar = new JLabel("Linguistics v"+MAJOR_VERSION+"."+
MINOR_VERSION);
bottom.add(statusBar, BorderLayout.SOUTH);
c.add(bottom, BorderLayout.SOUTH);
setLanguage();
}
/**
* Handler function for button and menu events.
* @param evt ActionEvent event triggered.
*/
public void actionPerformed(ActionEvent evt) {
String arg = evt.getActionCommand();
if (arg.equals("Exit"))
parent.exit();
if (arg.equals("Generate"))
generateWords();
else if (arg.equals("Print list"))
printList();
else if (arg.equals("Copy list"))
copyList();
}
/**
* Handler function for checkbox events.
* @param evt ItemEvent event triggered.
*/
public void itemStateChanged(ItemEvent evt) {
if (evt.getStateChange() == ItemEvent.DESELECTED)
return;
setLanguage();
}
private void setLanguage() {
Class cls;
int index;
index = languagesList.getSelectedIndex();
try {
cls = Class.forName( languageClasses[index] );
currentLanguage = (Language)cls.newInstance();
} catch (Exception e) {
currentLanguage = null;
statusBar.setText("Could not load "+languageClasses[index]+".");
}
}
/**
* Fills the listbox with words generated by the current language.
*/
private void generateWords() {
int number = count.getValue();
int loop;
if ( currentLanguage == null ) {
words = new String[1];
words[0] = "No valid language!";
} else {
words = new String[number];
for (loop = 0; loop < number; loop++) {
words[loop] = currentLanguage.word();
}
}
wordListModel.change();
}
/**
* Prints the word list to the local printer (Application only).
*/
private void printList() {
}
/**
* Copies the word list to the clipboard (Application only).
*/
private void copyList() {
int loop;
int count;
StringBuffer copyString;
Clipboard clipboard;
StringSelection selection;
count = words.length;
if ( count > 0 ) {
copyString = new StringBuffer();
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
for (loop = 0; loop < count; loop++) {
copyString.append(words[loop]);
copyString.append("\n");
}
selection = new StringSelection(copyString.toString());
clipboard.setContents(selection, null);
}
}
//Main method
public static void main(String[] args) {
CenteredFrame frame = new CenteredFrame(new Dimension(200,300));
Linguistics applet = new Linguistics(frame);
applet.isStandalone = true;
Container c = frame.getContentPane();
c.setLayout(new BorderLayout());
c.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setVisible(true);
}
class WordListModel extends DefaultListModel {
public Object getElementAt(int index) {
return words[index];
}
public int getSize() {
if (words != null)
return words.length;
else
return 0;
}
public void change() { fireContentsChanged(this,-1,-1); }
}
CenteredFrame parent;
private static final int MAJOR_VERSION = 1;
private static final int MINOR_VERSION = 0;
private JList wordList;
private String[] words;
private IntegerField count;
private Language currentLanguage;
private JComboBox languagesList;
private JLabel statusBar;
private WordListModel wordListModel;
boolean isStandalone = false;
private String[] languageNames;
private String[] languageClasses;
}
--
+--------------------------------------------------------------------+
| Andrew Akins |
| Home: igor@ames.net - http://www.ames.net/igor/ |
| Work: andya@cms-gt.com - http://www.cms-gt.com/ |
+--------------------------------------------------------------------+
| IMTU: tc++(**) ru+ ge 3i+ jt- au+ ls+ kk+ hi+ as+ va+ dr+ so+ zh+ |
| vi+ da+ |
| Geek: GCS d- s+:+ a- C++ W++ w+++(-)$ PS+ PE t- 5++ X+ R+++ tv+ |
| b+++ DI+ D-- G e+ h---- r+++ y++++ |
+--------------------------------------------------------------------+
Technical Details
Message-ID:
<359DBE38.4BE90078@ames.net>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,fbdc20ec7baaaf2,start X-Google-Attributes: gidf74b3,public From: Andrew Akins <igor@ames.net> Subject: JComboBox and JApplet Date: 1998/07/04 Message-ID: <359DBE38.4BE90078@ames.net> X-Deja-AN: 368593634 Content-Transfer-Encoding: 7bit Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Corel-MessageType: EMail Organization: Another Netscape Collabra Server User Newsgroups: borland.public.jbuilder.java.language