如何使用swingworker在另一个班级中使用一个班级的按钮

时间:2017-12-07 10:37:19

标签: java swing

我试图在单击按钮后加载进度条,按钮在另一个类中,并且swing工作者是类中的类。这是一些代码

    public class BigramProgbarTest extends JFrame
    {
        //Create the connection to Neo4j


        //MAIN METHOD
        public static void main(String[] args)
        {

            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    //INITIALIZE JFRAME FORM
                    BigramProgbarTest form=new BigramProgbarTest();
                    form.setVisible(true);
                }
            });

        }
    private static JButton btn;
        //CONSTRUCTOR
        public BigramProgbarTest()
        {

            //the form
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(200,200,800,300);
            setTitle("Netword Data Table");
            getContentPane().setLayout(null);

            //ADD SCROLLPANE
            JScrollPane scroll=new JScrollPane();
            scroll.setBounds(70,80,600,200);
            getContentPane().add(scroll);

            //THE TABLE
            final JTable table=new JTable();
            scroll.setViewportView(table);


            //THE MODEL OF THE TABLE
            DefaultTableModel model=new DefaultTableModel()
            {

                    }
                }
            };
            //Create and run the query here for table



            //ASSIGN THE MODEL TO TABLE
            table.setModel(model);

            model.addColumn("Select"); //Column for check boxes
            model.addColumn("Bigrams"); //Column for Bigrams

            //A while loop here 

            }   //ENDWHILE      

            //OBTAIN SELECTED ROW

        //  BigramProgbarTest fr = new BigramProgbarTest();
                JButton btn=new JButton("Get Selected");

                btn.addActionListener(new ActionListener()
                {
                    @Override
                    public void actionPerformed(ActionEvent arg0) 
                    {//A method for the button to do something with seleted rows
                    }
}
}
});

    //ADD BUTTON TO FORM
    btn.setBounds(71,39,130,30);
    getContentPane().add(btn);

所以在这里你可以看到该类有表格和滚动窗格(只是在它们有影响的情况下显示它们)。我在这个班级的另一个班级里有一个摇摆工人班和进度栏。以下是swingworker类的一些代码。

        JProgressBar progressBar = new JProgressBar();
            progressBar.setBounds(211, 42, 146, 14);
            getContentPane().add(progressBar);
            ProgressWorker worker = new ProgressWorker(progressBar);
            worker.execute();

        }


        class ProgressWorker extends SwingWorker<Void, Integer> {
            private final JProgressBar progress;

            public ProgressWorker(JProgressBar progress) {
                this.progress = progress;
            }

            //btn.addActionListener(new ActionListener() //Trying to find somewhere to use this 
            @Override
            protected Void doInBackground() throws Exception  { //This should be done after a button click

//Some code and for loop for the progress bar

    final int progr = (int) ((100L * (results.length - i2)) / results.length);
                publish(progr);

                }//ENDFOR each record// |  | ENDFOR each record//

                }

            return null;
        }
        //final int progr = ((int) ((100L * (recordLoop - firstRecord)) / (lastRecord-firstRecord)));

        @Override
        protected void process(List<Integer> chunks) {
            progress.setValue(chunks.get(chunks.size() - 1));
            super.process(chunks); //This is the process of how the progress bar will load 
        }

        @Override
        protected void done() {
            progress.setValue(100); //This is the value when process is complete  
        }
    }
    }

现在我期望这样做是在单击按钮后运行swingworker类。

目前,进度条只是在点击任何内容之前加载到右边,甚至在我选择复选框之前。所以我希望swingworker能够在执行任何任务之前听取主类的按钮点击。

0 个答案:

没有答案