hiii..
I got this strange problem.i m refreshing my jframe after every second.This jframe contains a button called addbutton.This addbutton opens another jframe.and that jframe contains a JComboBox .The problem is after every second the JComboBox is getting refreshed.I dont understand why.Before the user gets time to select any item in the combobox..That combobox again goes to the first element.
Can somone pls explain me hw to prevent all this??
Thanks for your time

Java Code:

public class MarketWatch {
    static JButton addButton = new JButton("Add");


    public static void main(String args[]) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                showTable();

            }
        }, 1000, 2000);
    }

    public static void showTable() {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub

                try {
                    // DriverManager.registerDriver(new
                    // oracle.jdbc.OracleDriver());
                    Class.forName("oracle.jdbc.driver.OracleDriver");
                    connMarketWatch = DriverManager.getConnection(
                            "jdbc:oracle:thin:@192.168.0.22:1521:XE", "sk_api",
                            "sk_api");

addButton.addActionListener(new ActionListener() {

                            @Override
                            public void actionPerformed(ActionEvent arg0) {
                                // TODO Auto-generated method stub
                                selectRow();



                            }
                        });
                } catch (SQLException | ClassNotFoundException e1) {
                    // TODO Auto-generated catch block
                    System.out.println("Error in connecting");
                    e1.printStackTrace();
                }
    }
    public static void selectRow()
    {
        try
        {  
            JComboBox jremoverows;
            JFrame frame = new JFrame();
            Dimension screenSize = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
                frame.setPreferredSize(new Dimension(200, 200));
                Dimension windowSize = new Dimension(frame.getPreferredSize());
                int wdwLeft = 300 + screenSize.width / 2 - windowSize.width / 2;
                int wdwTop = screenSize.height / 2 - windowSize.height / 2;

            Statement saddtoremoverow = connMarketWatch.createStatement();
            ResultSet rsremoverow = saddtoremoverow.executeQuery("select securty_symbol from copy_view_mkt_watch where removerow='remove'");
            int j=0;

            while(rsremoverow.next())
            {
                removedObjects[j]=rsremoverow.getString("SECURTY_SYMBOL");
                j++;
            }
            jremoverows = new JComboBox(removedObjects);
            AutoCompleteDecorator.decorate(jremoverows);
            jremoverows.setEditable(true);
            frame.setSize(200,200);
            frame.requestFocus();
            frame.setAlwaysOnTop(true);
            frame.toFront();

            frame.setLocation(wdwLeft, wdwTop);
            frame.add(jremoverows);
            frame.setLayout(new FlowLayout());
            frame.setVisible(true);


        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

    }

somehow, I doubt your JComboBox to refresh. the code you posted here won't even compile, since you are using variables you don't declare.

  • never to perform long runnig task from ActionListener, nor wrapped into invokeLater, Swing GUi freeze untill long and hard JDBC ended

  • why did you recreated whole GUI on fixed period, Swing JComponents are designated to be reused, did you count those JFrames

  • no idea how to help you without detail, for why reason you refreshed JComboBox

  • shots into dark

    1. creatre only one JFrame

    2. only one JComboBox

    3. JComboBox has its model, use DefaultComboBox model, there you have to make all changes, remove all items and add a new Items came from from JDBC, or to create a new DefaultComboBoxModel and change its model on runtime

    4. you have to use MutableComboBoxModel in the case that some of Item(s) in JComboBox will be removed updated added (inserted)

    5. I'm hope that AutoCompleteDecorator is from SwingX packages

    6. you have to wrap output, updates from util.Timer into invokeLater but only Swing Methods not class or void created this output, updates

    7. proper way could be usage of SwingWorker for this idea

Thanks for your reply.I have spent an entire day searching swing worker and i couldnt understand a single bit of it.
THe basic problem is the focus shifts to the frame which has table in it after every second.
Even if i put a jtextfield instead of jcombobox i find typing in jtextfiedl difficult .After each second the focus goes to jframe which has table.

Here i

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.