Categories

Main
Database
Java
Microsoft.NET
Adabas
DB2
Informix
Microsoft SQL Server
MySQL
Oracle
Pervasive.SQL
PostgreSQL
Sybase
Other
ASP
ColdFusion
Crystal Reports
Delphi, C etc
JAVA
Microsoft.NET
Perl and the DBI
PHP
ANSI SQL
Unix Shell Scripts
Visual Basic
XML & XSLT
Corel Paradox
FileMaker
Microsoft Access
Microsoft Excel
Other PC Databases
Applications & Tools
Database Concepts & Design
EJB programming & troubleshooting
EJB design
General J2EE
XML & Web services
Web tier: servlets, JSP, Web frameworks
Performance and scalability
Industry news
TSS feedback
Mobicents Contributors
Mobicents Users
JSLEE Resource Adaptor Types
Planning JavaOne 2008
Sun Tech Days
Other Java conferences
Binary Web Services and XML
Metro and JAXB
GlassFish
GlassFish Plugins
Project jMaki
GlassFish WebTier
Mural
Java Development Tools
Java WS & XML Community News
JAXP
Java SE
6uN Early Access
Java Quick Starter
Java SE Snapshots: Project Feedback
JCK Forum
Feedback and Suggestions
JDK Distros
JDK Open Source
General JSR Discussion
JCP 2.6
JXTA Community Forum
ME Interest
ME Cool Apps
ME General Help
ME Feedback & Suggestions
ME Application Developer Interest
Blu-ray Disc Java
ME Developer Days
Squawk
Mobile Developer Alliance
OpenCable
LWUIT
JavaFX Script Language Discussion
OpenJFX General Discussion
Scene Graph
General Performance Discussion
Your Java Career
NetBeans 6.0
Servlets
JSP
JSF
Portals and Portlets
EJB and Other Java EE Technologies
Distributed Java
Object Relational Mapping
JDBC
Web Services
Swing / AWT / SWT / JFace
JNLP and Web Start
Java Micro Edition
Sockets and Internet Protocols
Threads and Synchronization
Performance
Applets
I/O and Streams
Other Java APIs
Game Development
Java in General (beginner)
Java in General (intermediate)
Java in General (advanced)
Programmer Certification (SCJP)
Developer Certification (SCJD)
Associate Certification (SCJA)
Web Component Certification (SCWCD)
EJB Certification (SCBCD)
Mobile Application Certification (SCMAD)
Architect Certification (SCEA)
Web Services Certification (SCDJWS)
XML Certification
Product and Other Certifications
Mock Exam Errata
Sun Certification Results
Authors' Corral
Book Reviews
Events
Bunkhouse Porch
Teachers' Lounge
Testing
OO, Patterns, UML and Refactoring
IDEs, Version Control and other tools
Ant, Maven and Other Build Tools
Linux / UNIX
Mac OS
HTML and JavaScript
XML and Related Technologies
Agile and Other Processes
General Computing
Security
Groovy
Scala
Other Languages
Struts
Application Frameworks
Other Open Source Projects
BEA/Weblogic
IBM/Websphere
Oracle/OAS
Apache/Tomcat
JBoss
Other Java Products and Servers
JavaRanch
Cattle Drive (java college)
Moderators Only
Trash Can
Jobs Offered
Jobs Wanted
Jobs Discussion
Meaningless Drivel
Programming Diversions
Blatant Advertising
Java Announcements
New To Java
Advanced Java
Java Applets
Networking
Threads and Synchronization
Java 2D
AWT / Swing
SWT / JFace
CLDC and MIDP
CDC and Personal Profile
Sun Java Wireless Toolkit
Enterprise JavaBeans
JavaServer Pages (JSP) and JSTL
Java Servlet
JavaServer Faces
Web Frameworks
Database
XML
Lucene
NetBeans
Eclipse
IntelliJ IDEA
JCreator
Other IDEs
Java Tutorials
Java Tips
Jobs Discussion
Jobs Offered
Jobs Wanted
Professional Certification
Forum Lobby
Java Blogs
Introductions
Reviews / Advertising
Suggestions & Feedback

Resources

Java Database
Linux
Coding
Mobile
Hardware
Software Development
Software Development
iOS,OS X
iOS,OS X
ORACLE
IBM DEVELOPER
IBM DEVELOPER
MSDN
MSDN


Tags

Swing / AWT / SWT / JFace

GUI stuff

changeSelection worked in 1.4, fails in 1.5


The application I'm working on has a search function for a large table of data. In addition, you can restrict the search to a selected area of the table. This worked fine when compiled on Java 1.4 (1.4.2_06), but has stopped working on Java 1.5 (1.5.0_03). It appears the problem is with JTable.changeSelection() not actually changing the selection to the cell that was found in the search.I've been able to reproduce the problem with some small modifications to the SimpleTableSelectionDemo.java tutorial program. I've included the code below with my changes.Basically all I did was modify the mouseListener so that instead of printing the table contents, it calls changeSelection to a fixed location (simulating the search going to a matching cell).If I compile/run in 1.4, I can select a cell or region, right-click the mouse and see the specified cell selected. If I start typing the selected cell is edited.If I compile/run in 1.5, when I right-click the selected cell is not changed (the second selected cell of the select region stays selected). If I start typing the wrong cell is edited.Hopefully someone can tell me if this is a pilot error, a run-time bug, or a change in behavior (and what I need to do to get it working again in 1.5).Thanks.code:/** SimpleTableSelectionDemo.java is a 1.4 application that requires no other files.*/import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JComponent;import javax.swing.ListSelectionModel;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import java.awt.Dimension;import java.awt.GridLayout;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;/*** SimpleTableSelectionDemo is just like SimpleTableDemo,* except that it detects selections, printing information* about the current selection to standard output.*/public class SimpleTableSelectionDemo extends JPanel {private boolean DEBUG = true;private boolean ALLOW_COLUMN_SELECTION = true;private boolean ALLOW_ROW_SELECTION = true;public SimpleTableSelectionDemo() {super(new GridLayout(1,0));final String[] columnNames = {"First Name","Last Name","Sport","# of Years","Vegetarian"};final Object[][] data = {{"Mary", "Campione","Snowboarding", new Integer(5), new Boolean(false)},{"Alison", "Huml","Rowing", new Integer(3), new Boolean(true)},{"Kathy", "Walrath","Knitting", new Integer(2), new Boolean(false)},{"Sharon", "Zakhour","Speed reading", new Integer(20), new Boolean(true)},{"Philip", "Milne","Pool", new Integer(10), new Boolean(false)}};final JTable table = new JTable(data, columnNames);table.setPreferredScrollableViewportSize(new Dimension(500, 70));table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);if (ALLOW_ROW_SELECTION) { // true by defaultListSelectionModel rowSM = table.getSelectionModel();rowSM.addListSelectionListener(new ListSelectionListener() {public void valueChanged(ListSelectionEvent e) {//Ignore extra messages.if (e.getValueIsAdjusting()) return;ListSelectionModel lsm = (ListSelectionModel)e.getSource();if (lsm.isSelectionEmpty()) {System.out.println("No rows are selected.");} else {int selectedRow = lsm.getMinSelectionIndex();System.out.println("Row " + selectedRow+ " is now selected.");}}});} else {table.setRowSelectionAllowed(false);}if (ALLOW_COLUMN_SELECTION) { // false by defaultif (ALLOW_ROW_SELECTION) {//We allow both row and column selection, which//implies that we *really* want to allow individual//cell selection.table.setCellSelectionEnabled(true);}table.setColumnSelectionAllowed(true);ListSelectionModel colSM =table.getColumnModel().getSelectionModel();colSM.addListSelectionListener(new ListSelectionListener() {public void valueChanged(ListSelectionEvent e) {//Ignore extra messages.if (e.getValueIsAdjusting()) return;ListSelectionModel lsm = (ListSelectionModel)e.getSource();if (lsm.isSelectionEmpty()) {System.out.println("No columns are selected.");} else {int selectedCol = lsm.getMinSelectionIndex();System.out.println("Column " + selectedCol+ " is now selected.");}}});}if (DEBUG) {table.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {//                  printDebugData(table);// ==============================================// Changed to do a changeSelection() on a right-click// without clearing any existing selected area.if (e.getButton() == MouseEvent.BUTTON3) {System.out.println("mouse clicked");table.changeSelection(2, 3, true, true);}// ==============================================}});}//Create the scroll pane and add the table to it.JScrollPane scrollPane = new JScrollPane(table);//Add the scroll pane to this panel.add(scrollPane);}private void printDebugData(JTable table) {int numRows = table.getRowCount();int numCols = table.getColumnCount();javax.swing.table.TableModel model = table.getModel();System.out.println("Value of data: ");for (int i=0; i < numRows; i++) {System.out.print("    row " + i + ":");for (int j=0; j < numCols; j++) {System.out.print("  " + model.getValueAt(i, j));}System.out.println();}System.out.println("--------------------------");}/*** Create the GUI and show it.  For thread safety,* this method should be invoked from the* event-dispatching thread.*/private static void createAndShowGUI() {//Make sure we have nice window decorations.JFrame.setDefaultLookAndFeelDecorated(true);//Create and set up the window.JFrame frame = new JFrame("SimpleTableSelectionDemo");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Create and set up the content pane.SimpleTableSelectionDemo newContentPane = new SimpleTableSelectionDemo();newContentPane.setOpaque(true); //content panes must be opaqueframe.setContentPane(newContentPane);//Display the window.frame.pack();frame.setVisible(true);}public static void main(String[] args) {//Schedule a job for the event-dispatching thread://creating and showing this application's GUI.javax.swing.SwingUtilities.invokeLater(new Runnable() {public void run() {createAndShowGUI();}});}}

   
   

 

      Well it's been a while with no replies.  Can anyone help me here?  Is there a change in behavior (I need to "fix" my code) or is there are true problem with Java 1.5?


Related Links

Timer
TableCellRenderer and JCheckBox
tools for swing
Default Constructor
a little confused!
abstract & interface
Show JButton with ImageICon in a JTable Cell
package
how to launch a swing app remotely
Missing Characters in Display
Swing
Program Structure
How do I add Text fields on seperate lines
Can I create a ActionEvent to force the event happen?
Come on Java Guru's -Ctrl left mouse click in a JTable-
Passing values between windows/frames