Article View: borland.public.jbuilder.java.swing
Article #250Re: Missing events
From: Paul Furbacher [TeamB]
Date: Fri, 13 Apr 2007 05:02
Date: Fri, 13 Apr 2007 05:02
138 lines
5470 bytes
5470 bytes
Bruce Houghton wrote:
> Well, to put it as simply as I can, I created a
> JList of names(as in address book). When I click on
> a name in the JList, it won't always create an event
> even though it does cause that name to be highlighted.
>
> Now, to be more specific, I've build a POS system using
> JList's and JTables's. Touching the screen will always highlight
> the field, and a beeping sound will occure(every time), but
> it will not always trigger an event(as in below "Say Something").
Does the code below illustrate what you are attempting to do?
If so, see the JavaDoc for JList and the tutorial linked in that
JavaDoc:
http://java.sun.com/products/jfc/tsc/tech_topics/jlist_1/jlist.html
*****
package bruce.houghton.list.selection;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.BorderLayout;
/**
* <p>Double-clicking on a list item pops open a message dialog.
* Single-clicking shows the selected list item in the label below
* the list.
* </p>
*/
public class ListEventFrame
extends JFrame {
ListModel data = new AbstractListModel() {
public int getSize() { return Byte.MAX_VALUE; }
public Object getElementAt(int index) { return "Index " + index; }
};
JPanel listPanel = new JPanel();
JScrollPane scrollPane = new JScrollPane();
JList listBox = new JList(data);
GridBagLayout gridBagLayout2 = new GridBagLayout();
JPanel buttonPanel = new JPanel();
GridBagLayout gridBagLayout3 = new GridBagLayout();
JButton quitButton = new JButton();
BorderLayout borderLayout1 = new BorderLayout();
JLabel label = new JLabel();
public ListEventFrame() {
try {
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(gridBagLayout2);
listPanel.setLayout(borderLayout1);
buttonPanel.setLayout(gridBagLayout3);
quitButton.setText("Quit");
quitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
quitButton_actionPerformed(actionEvent);
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
scrollPane.setPreferredSize(new Dimension(260, 250));
listBox.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent mouseEvent) {
listBox_mouseClicked(mouseEvent);
}
});
label.setText("...");
buttonPanel.add(quitButton, new GridBagConstraints(0, 0, 1, 1,
0.0, 0.0
, GridBagConstraints.CENTER, GridBagConstraints.NONE,
new Insets(0, 0, 0, 0), 0, 0));
scrollPane.getViewport().add(listBox);
listPanel.add(scrollPane, java.awt.BorderLayout.CENTER);
listPanel.add(label, java.awt.BorderLayout.SOUTH);
this.getContentPane().add(buttonPanel,
new GridBagConstraints(0, 1, 1, 1, 0.0, 1.0
, GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL,
new Insets(0, 12, 11, 11), 0, 0));
this.getContentPane().add(listPanel,
new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0
, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH,
new Insets(12, 12, 0, 11), 0, 0));
}
public static void main(String[] args) {
ListEventFrame frame = new ListEventFrame();
frame.pack();
frame.setVisible(true);
}
public void quitButton_actionPerformed(ActionEvent actionEvent) {
System.exit(0);
}
public void listBox_mouseClicked(MouseEvent mouseEvent) {
int index = listBox.locationToIndex(mouseEvent.getPoint());
String str = (String)listBox.getModel().getElementAt(index);
System.out.println("Double clicked on Item " + index);
if (mouseEvent.getClickCount() == 2) {
JOptionPane.showMessageDialog(this, str);
return;
}
if ( mouseEvent.getClickCount() == 1 ) {
label.setText(str);
}
}
}
--
Paul Furbacher (TeamB)
Save time, search the archives:
http://info.borland.com/newsgroups/ngsearch.html
Is it in Joi Ellis's Faq-O-Matic?
http://www.visi.com/~gyles19/fom-serve/cache/1.html
Finally, please send responses to the newsgroup only.
That means, do not send email directly to me.
Thank you.
Technical Details
Message-ID:
<461f0edd$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<46166a31$1@newsgroups.borland.com> <461c669a$1@newsgroups.borland.com> <461d4e22@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 111d45,dec980017feb87ab X-Google-Attributes: gid111d45,public X-Google-Language: ENGLISH,ASCII-7-bit Date: Fri, 13 Apr 2007 01:02:48 -0400 From: "Paul Furbacher [TeamB]" <pfurbacher@mac.com> Organization: Reverse Alchemy, Inc. User-Agent: Thunderbird 1.5.0.10 (Macintosh/20070221) MIME-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing Subject: Re: Missing events References: <46166a31$1@newsgroups.borland.com> <461c669a$1@newsgroups.borland.com> <461d4e22@newsgroups.borland.com> In-Reply-To: <461d4e22@newsgroups.borland.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 69.142.194.228 Message-ID: <461f0edd$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1176440541 69.142.194.228 (12 Apr 2007 22:02:21 -0800) Lines: 138 Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail