Results 1 to 5 of 5
  1. #1
    nicoIas is offline Member
    Join Date
    Nov 2012
    Location
    Czech republic
    Posts
    3
    Rep Power
    0

    Default Cancel the selectionof list by .clearSelection();

    Hi,

    I am really sorry to put here whole code example, but do not know how better show my problem.

    No one helped me on Czech forums, so I am here... that's why my english can seem queer or comic for you.

    I could not deselect by .clearSelection();
    It is normaly working, but together with sql query it somehow hangs (or jam) and does not deselect.
    Once you click button1 (8 lines from the end) I would like to cancel the selection in list1, but it does not.

    Help me please.

    Thank you in advance,
    NicoIas

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.sql.*;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    
    public class MainClass {
        public static void main(String[] args) {
            nazev apl = new nazev();
        }
    }
    class nazev extends JFrame {
    
    int IvarA;
    JTabbedPane tabbedPane = new JTabbedPane();
    JPanel panel1 = new JPanel();
    JButton button1 = new JButton("Clear");
    String[] data1 = {"DTW", "NTB", "RNBA", "CK", "PDR"};
    JList list1 = new JList(data1);
    DefaultListModel data2 = new DefaultListModel();
    JList list2 = new JList(data2);
    
    public nazev () {
    
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible (true);
        getContentPane().setLayout (new BorderLayout());
        getContentPane().add(panel1,BorderLayout.CENTER);
    
        panel1.add(button1);
        panel1.add(list2);
        list2.setPreferredSize(new Dimension (100,100));
        panel1.add(list1);
        list1.setPreferredSize(new Dimension (100,100));
        button1.addActionListener (new ActionButton1());
        list1.addListSelectionListener( new ListSelectionListener1());
    
        setSize (300,140);
        }
            class ListSelectionListener1 implements ListSelectionListener {
            public void valueChanged(ListSelectionEvent e) {
    
            if (e.getValueIsAdjusting())
            return;
    
            Object[] selected1 = list1.getSelectedValues();
                   try {
                    String url = "jdbc:odbc:databaze1";
                    String jmeno1;
                    Statement pozadavek = null;
                    ResultSet vysledek1 = null;
                    Connection dB = null;
                    try {
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        dB = DriverManager.getConnection(url);
                    } catch (ClassNotFoundException v) {
                        System.err.println("Nepodařilo se zavést most JDBC/ODBC." + v.getMessage());
                        System.exit(1);
                    } catch (SQLException v) {
                        System.err.println("Nepodařilo se připojit k databázi." + v.getMessage());
                        System.exit(2);
                    }
                    try {
    String query1 = "Select TaskName FROM Tracker WHERE Code IN(";
    for (int i = 0, n = selected1.length; i < n; i++) {
    if (i > 0) query1 += " ,";
    query1 += "'" + selected1[i] + "'";
    }
    query1 += ");";
    
                        pozadavek = dB.createStatement();
                        vysledek1 = pozadavek.executeQuery(query1);
    
                    } catch (SQLException v) {
                        System.err.println("Chyba SQL." + v.getMessage());
                        //System.exit(3);
                    }
                    boolean zaznamy1 = vysledek1.next();
                    if (!zaznamy1) {
                        //System.out.println("Nebyla vrácena žádná data");
                        //System.exit(4);
                    }
                    try {
                        do {
                        jmeno1 = vysledek1.getString(1);
                        String[] items1 = {jmeno1};
                        data2.add(IvarA, items1[IvarA]);
                        } while (vysledek1.next());
    
                    } catch (SQLException v) {
                        System.err.println("Chyba při zobrazaní dat." + v.getMessage());
                        //System.exit(5);
    
                    } finally {
                        dB.close();
                    }
                    }
                    catch (SQLException v) {
                        System.err.println("Chyba Exit 6." + v.getMessage());
                        //System.exit(6);
                    }
              }
        }
            class ActionButton1 implements ActionListener {
            public void actionPerformed(ActionEvent e) {
    
                        list1.clearSelection();
    
                }
            }
        }

  2. #2
    nicoIas is offline Member
    Join Date
    Nov 2012
    Location
    Czech republic
    Posts
    3
    Rep Power
    0

    Default Re: Cancel the selectionof list by .clearSelection();

    No idea?

    Look, when I cancel all query code, then it work well... you can try.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.sql.*;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    
    public class MainClass {
        public static void main(String[] args) {
            nazev apl = new nazev();
        }
    }
    class nazev extends JFrame {
    
    int IvarA;
    JTabbedPane tabbedPane = new JTabbedPane();
    JPanel panel1 = new JPanel();
    JButton button1 = new JButton("Clear");
    String[] data1 = {"DTW", "NTB", "RNBA", "CK", "PDR"};
    JList list1 = new JList(data1);
    DefaultListModel data2 = new DefaultListModel();
    JList list2 = new JList(data2);
    
    public nazev () {
    
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible (true);
        getContentPane().setLayout (new BorderLayout());
        getContentPane().add(panel1,BorderLayout.CENTER);
    
        panel1.add(button1);
        panel1.add(list2);
        list2.setPreferredSize(new Dimension (100,100));
        panel1.add(list1);
        list1.setPreferredSize(new Dimension (100,100));
        button1.addActionListener (new ActionButton1());
        list1.addListSelectionListener( new ListSelectionListener1());
    
        setSize (300,140);
        }
            class ListSelectionListener1 implements ListSelectionListener {
            public void valueChanged(ListSelectionEvent e) {
    
     
              }
        }
            class ActionButton1 implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                data2.clear();
                list2.clearSelection();
                        list1.clearSelection();
    
                }
            }
        }

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Cancel the selectionof list by .clearSelection();

    First, test your SQL code in isolation, that is write some code that only runs that with various parameters, logging everything.
    To do this it would be easiest to create a class whose job is to access the database (a Data Access Object, DAO).

    When you are sure that is working, you can then plug it into this code.

    Note, I would suggest you learn about concurrency and Swing threads. The access of the database should be done on its own thread, not on the EDT. This is why you are seeing the application hang, because something has happened on the SQL call.

    Here's some tutorial links that might help on the concurrency side:
    General Swing Tutorial.
    Concurrency tutorial (a subsection of the above).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    nicoIas is offline Member
    Join Date
    Nov 2012
    Location
    Czech republic
    Posts
    3
    Rep Power
    0

    Default Re: Cancel the selectionof list by .clearSelection();

    Hi,

    I found quite simple solution.

    Do not know correct English and java term, but I will try.
    I used forced redrawing and it work well - .repaint();

    Java Code:
            class ActionButton1 implements ActionListener {
            public void actionPerformed(ActionEvent e) {
    
                        list1.repaint();
                        list1.clearSelection();
                }


    I read Concurrency tutorial and some literature in native language, but I am somehow lost.

    Could you please show me on my example how to create own treads for access of the database? Or describe what to do?
    Never mind if not.

    Thank you, N.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Cancel the selectionof list by .clearSelection();

    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Cancel button ? In java
    By smasm in forum New To Java
    Replies: 21
    Last Post: 10-01-2011, 06:24 AM
  2. RadioButton clearSelection Problem
    By Aniczka in forum New To Java
    Replies: 3
    Last Post: 10-31-2009, 02:26 PM
  3. Struts 2 cancel button
    By kirtichopra2003 in forum Advanced Java
    Replies: 0
    Last Post: 09-29-2009, 03:40 PM
  4. Cancel Particular scheduled task
    By hjava in forum New To Java
    Replies: 0
    Last Post: 08-11-2009, 11:10 PM
  5. How should I cancel this thread ?
    By playwin2 in forum Threads and Synchronization
    Replies: 7
    Last Post: 08-27-2008, 06:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •