Solved

Unwanted ListSelectionEvent when using JComboBox

Posted on 1998-07-27
9
275 Views
Last Modified: 2010-04-16
In my JApplet a JTable is contained as well as a JComboBox which is used to determine the content of the JTable.

At first no row in the JTable is selected and so some JMenuItems of a JMenu which also contained in the JApplet are disabled. These should become only enabled when a row in the JTable is selected.

When the JComboBox' is used its list drops down over a part of the JTable.
Whenever the JComboBox' list drops down and I choose an item a ListSelectionEvent is fired from the table's DefaultListSelectionModel.
So it seems that clicking on a JComboBox' item goes through this component and reaches a row from the JTable.

A selected row, however, is not displayed then, all rows appears unselected.

How can I prevent from this?
0
Comment
Question by:dirku
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
9 Comments
 

Author Comment

by:dirku
ID: 1228424
Edited text of question
0
 

Author Comment

by:dirku
ID: 1228425
Edited text of question
0
 
LVL 5

Expert Comment

by:msmolyak
ID: 1228426
What does the combo box have to do with the table? Why selecting an item from the combo box should have any bearing on the table model? It is not very clear.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 4

Accepted Solution

by:
evijay earned 40 total points
ID: 1228427
in the list selection listener, be sure to add
e.getValueIsAdjusting() check as shown below



    public void valueChanged(ListSelectionEvent e) {
        if (!e.getValueIsAdjusting()) {

                      // do here the processing
        }
    }
0
 

Author Comment

by:dirku
ID: 1228428
When using System.out.println(LSe.getSource()); (where LSe is a ListSelectionEvent) the event looks like this com.sun.java.swing.event.ListSelectionEvent[ source=com.sun.java.swing.DefaultListSelectionModel 1898706 ={} firstIndex= 0 lastIndex= -1 isAdjusting= false ].
Thus, if I don't set the adjusting to true from the choice in the JComboBox your suggstion will never work, won't it?

I never worked with the adjusting property so I don't know how to set.
What must I do to set adjusting true and where or when?
0
 

Author Comment

by:dirku
ID: 1228429
For msmolyak:
In the JComboBox all the portfolios of a user are listed.
When starting the JApplet all portfolio names will be added and all portfolio data will be displayed. However, if the portfolios contain a large number of entries a user might want to see only one of his, say ten, portfolios. Then he simply needs to make a choice in the JComboBox and only the values of the selected portfolio will be displayed.
This is the need for the JComboBox related to the JTable in this JApplet.
0
 

Author Comment

by:dirku
ID: 1228430
I found a method in JTable called setValueIsAdjusting. Do I understand the explanation right that its function is like removing a listener object and after handling the event use the addXXXListener method?
0
 

Author Comment

by:dirku
ID: 1228431
I tried out what you suggested but it doesn't take any effect. However, my problem seems to be nonexistent anymore (wether using setValueIsAdjusting or not).

Maybe you could answer me another questions that I can grade you? Hope this is OK?

In my JApplet I read some data from a database and I use a DefaultTableModel to store the values in a Vecotr of Vectors.
Here's the code which is used to store the data from the ResultSet into the Vecotrs:

            try
            {
                  if( queryResult == null)
                  {
                        System.err.println("ResultSet geschlossen.");
                        return false;
                  }

                  ResultSetMetaData rsmd = queryResult.getMetaData();
                  int columns = rsmd.getColumnCount();
                  
                  columnNames = new Vector(columns);
                  for(int i=0; i<columns; i++)
                  {
                        columnNames.addElement(rsmd.getColumnLabel(i+1));
                        System.out.println(columnNames.elementAt(i).toString());
                  }

                  rows = new Vector();
                  while( queryResult.next() )
                  {
                        Vector cols = new Vector(columns);
                        for(int i=1; i<=columns; i++)
                        {
                              int type = rsmd.getColumnType(i);
                              cols.addElement(readValue(queryResult, type, i));
                        }
                        rows.addElement(cols);
                  }//end while( queryResult.next() )


      protected final Object readValue(ResultSet queryResult, int type, int col)
      {
            Object value = null;

            try
            {
                  switch(type)
                  {
                        case Types.CHAR:
                        case Types.VARCHAR:
                        case Types.LONGVARCHAR:
                                                            value = queryResult.getString(col).trim();
                                                            break;
                        case Types.BIT:
                                                            Boolean bool = new Boolean(queryResult.getBoolean(col));
                                                            value = bool;
                                                            break;
                        case Types.TINYINT:
                        case Types.SMALLINT:
                        case Types.INTEGER:
                                                            Integer num = new Integer(queryResult.getString(col));
                                                            value = num;
                                                            break;
                        case Types.BIGINT:
                                                            Long numL = new Long(queryResult.getString(col));
                                                            value = numL;
                                                            break;
                        case Types.REAL:
                                                            Float real = new Float(queryResult.getString(col));
                                                            value = real;
                                                            break;
                        case Types.FLOAT:
                        case Types.DOUBLE:
                                                            Double dbl = new Double(queryResult.getString(col));
                                                            value = dbl;
                                                            break;
                        case Types.NUMERIC:
                        case Types.DECIMAL:
                                                            BigDecimal bDec;
                                                            bDec = new BigDecimal(queryResult.getString(col));
                                                            bDec.setScale(precision);
                                                            value = bDec;
                                                            break;
                        case Types.DATE:
                                                            value = queryResult.getDate(col);
                                                            break;
                        case Types.TIME:
                                                            value = queryResult.getTime(col);
                                                            break;
                        case Types.TIMESTAMP:
                                                            value = queryResult.getTimestamp(col);
                                                            break;
                        case Types.BINARY:
                        case Types.VARBINARY:
                        case Types.LONGVARBINARY:
                                                            value = queryResult.getBytes(col);
                                                            break;
                        default:            value = queryResult.getObject(col);
                  }//end switch
                  return value;
            }
            catch(SQLException SQLe)
            {
                  System.err.println("SQL-Fehler: " + SQLe.toString());
                  return null;
            }
      }//end readValue

This works really well. As you can see I set the scale of the BigDecimals to 2 before storing them into the Vector.
The problem:
When reading BigDecimal values from the Vector they ever have a scale of 4! I don't understand this.
Furthermore I do some calculations with the values in the table and add some further columns to the table in which the calculated values are placed. The calculated values have a scale of 4, too, even although I set the scale explicitely to 2.
After calculating I store the values in the Vector so that they can be shown in the table, of course.

So, how can I accomplish that BigDecimals are shown in the table with a scale/precision of 2?
0
 

Author Comment

by:dirku
ID: 1228432
Oops! The variable "precision" needs to be substituted with the value 2, of course. The int variable precision is set to 2 when I declare it in the class. besides there is a setPrecision(int val) in the class.
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question