Example usage for org.jfree.chart ChartFrame setLocationByPlatform

List of usage examples for org.jfree.chart ChartFrame setLocationByPlatform

Introduction

In this page you can find the example usage for org.jfree.chart ChartFrame setLocationByPlatform.

Prototype

public void setLocationByPlatform(boolean locationByPlatform) 

Source Link

Document

Sets whether this Window should appear at the default location for the native windowing system or at the current location (returned by getLocation ) the next time the Window is made visible.

Usage

From source file:Forms.SalesChart.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    DefaultPieDataset pieDataset = new DefaultPieDataset();

    String conString = "jdbc:mysql://localhost:3306/nafis";
    String username = "root";
    String passward = "";

    String sql = "SELECT * FROM sold";

    try {/*www .j a  v  a  2s. co  m*/
        Connection con = (Connection) DriverManager.getConnection(conString, username, passward);

        Statement s = (Statement) con.prepareStatement(sql);

        ResultSet rs = s.executeQuery(sql);

        HashMap<String, Integer> map = new HashMap<String, Integer>();

        while (rs.next()) {

            String name = rs.getString(2);

            String stock = rs.getString(3);
            String type = rs.getString(8);

            Integer oldVal = map.get(type);

            //System.out.println(oldVal);

            if (oldVal == null) {
                map.put(type, Integer.parseInt(stock));
            } else {
                map.put(type, oldVal + Integer.parseInt(stock));
            }

        }

        for (HashMap.Entry m : map.entrySet()) {
            //System.out.println(m.getKey()+" "+m.getValue());  
            pieDataset.setValue(m.getKey() + "", Integer.parseInt(m.getValue() + ""));
        }

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

    JFreeChart chart = ChartFactory.createPieChart3D("pie chart", pieDataset, true, true, false);
    PiePlot3D p = (PiePlot3D) chart.getPlot();
    p.setStartAngle(0);
    p.setDirection(Rotation.CLOCKWISE);
    p.setForegroundAlpha(0.5f);
    p.getBackgroundPaint();

    ChartFrame frame = new ChartFrame("Pie Chart", chart);
    frame.setLocationByPlatform(true);

    frame.setVisible(true);
    frame.setSize(750, 600);
}

From source file:e3fraud.gui.MainWindow.java

public void actionPerformed(ActionEvent e) {

    //Handle open button action.
    if (e.getSource() == openButton) {
        int returnVal = fc.showOpenDialog(MainWindow.this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            //parse file
            this.baseModel = FileParser.parseFile(file);
            log.append(currentTime.currentTime() + " Opened: " + file.getName() + "." + newline);
        } else {/*from  w  w w.j av a2  s.  co m*/
            log.append(currentTime.currentTime() + " Open command cancelled by user." + newline);
        }
        log.setCaretPosition(log.getDocument().getLength());

        //handle Generate button
    } else if (e.getSource() == generateButton) {
        if (this.baseModel != null) {
            //have the user indicate the ToA via pop-up
            JFrame frame1 = new JFrame("Select Target of Assessment");
            Map<String, Resource> actorsMap = this.baseModel.getActorsMap();
            String selectedActorString = (String) JOptionPane.showInputDialog(frame1,
                    "Which actor's perspective are you taking?", "Choose main actor",
                    JOptionPane.QUESTION_MESSAGE, null, actorsMap.keySet().toArray(),
                    actorsMap.keySet().toArray()[0]);
            if (selectedActorString == null) {
                log.append(currentTime.currentTime() + " Attack generation cancelled!" + newline);
            } else {
                lastSelectedActorString = selectedActorString;
                //have the user select a need via pop-up
                JFrame frame2 = new JFrame("Select graph parameter");
                Map<String, Resource> needsMap = this.baseModel.getNeedsMap();
                String selectedNeedString = (String) JOptionPane.showInputDialog(frame2,
                        "What do you want to use as parameter?", "Choose need to parametrize",
                        JOptionPane.QUESTION_MESSAGE, null, needsMap.keySet().toArray(),
                        needsMap.keySet().toArray()[0]);
                if (selectedNeedString == null) {
                    log.append("Attack generation cancelled!" + newline);
                } else {
                    lastSelectedNeedString = selectedNeedString;
                    //have the user select occurence interval via pop-up
                    JTextField xField = new JTextField("1", 4);
                    JTextField yField = new JTextField("500", 4);
                    JPanel myPanel = new JPanel();
                    myPanel.add(new JLabel("Mininum occurences:"));
                    myPanel.add(xField);
                    myPanel.add(Box.createHorizontalStrut(15)); // a spacer
                    myPanel.add(new JLabel("Maximum occurences:"));
                    myPanel.add(yField);
                    int result = JOptionPane.showConfirmDialog(null, myPanel,
                            "Please Enter occurence rate interval", JOptionPane.OK_CANCEL_OPTION);

                    if (result == JOptionPane.CANCEL_OPTION) {
                        log.append("Attack generation cancelled!" + newline);
                    } else if (result == JOptionPane.OK_OPTION) {
                        startValue = Integer.parseInt(xField.getText());
                        endValue = Integer.parseInt(yField.getText());

                        selectedNeed = needsMap.get(selectedNeedString);
                        selectedActor = actorsMap.get(selectedActorString);

                        //Have a Worker thread to the time-consuming generation and raking (to not freeze the GUI)
                        GenerationWorker generationWorker = new GenerationWorker(baseModel, selectedActorString,
                                selectedActor, selectedNeed, selectedNeedString, startValue, endValue, log,
                                lossButton, gainButton, lossGainButton, gainLossButton, groupingButton,
                                collusionsButton) {
                            //make it so that when Worker is done
                            @Override
                            protected void done() {
                                try {
                                    progressBar.setVisible(false);
                                    System.err.println("I made it invisible");
                                    //the Worker's result is retrieved
                                    treeModel.setRoot(get());
                                    tree.setModel(treeModel);

                                    tree.updateUI();
                                    tree.collapseRow(1);
                                    //tree.expandRow(0);
                                    tree.setRootVisible(false);
                                    setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                                } catch (InterruptedException | ExecutionException ex) {
                                    Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
                                    setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                                    log.append("Out of memory; please increase heap size of JVM");
                                    PopUps.infoBox(
                                            "Encountered an error. Most likely out of memory; try increasing the heap size of JVM",
                                            "Error");
                                }
                            }
                        };
                        this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                        progressBar.setVisible(true);
                        progressBar.setIndeterminate(true);
                        progressBar.setString("generating...");
                        generationWorker.addPropertyChangeListener(new PropertyChangeListener() {
                            public void propertyChange(PropertyChangeEvent evt) {
                                if ("phase".equals(evt.getPropertyName())) {
                                    progressBar.setMaximum(100);
                                    progressBar.setIndeterminate(false);
                                    progressBar.setString("ranking...");
                                } else if ("progress".equals(evt.getPropertyName())) {
                                    progressBar.setValue((Integer) evt.getNewValue());
                                }
                            }
                        });
                        generationWorker.execute();
                    }
                }
            }
        } else {
            log.append("Load a model file first!" + newline);
        }
    } //handle the refresh button
    else if (e.getSource() == refreshButton) {
        if (lastSelectedNeedString != null && lastSelectedActorString != null) {
            Map<String, Resource> actorsMap = this.baseModel.getActorsMap();
            Map<String, Resource> needsMap = this.baseModel.getNeedsMap();
            selectedNeed = needsMap.get(lastSelectedNeedString);
            selectedActor = actorsMap.get(lastSelectedActorString);

            //Have a Worker thread to the time-consuming generation and raking (to not freeze the GUI)
            GenerationWorker generationWorker = new GenerationWorker(baseModel, lastSelectedActorString,
                    selectedActor, selectedNeed, lastSelectedNeedString, startValue, endValue, log, lossButton,
                    gainButton, lossGainButton, gainLossButton, groupingButton, collusionsButton) {
                //make it so that when Worker is done
                @Override
                protected void done() {
                    try {
                        progressBar.setVisible(false);
                        //the Worker's result is retrieved
                        treeModel.setRoot(get());
                        tree.setModel(treeModel);
                        tree.updateUI();
                        tree.collapseRow(1);
                        //tree.expandRow(0);
                        tree.setRootVisible(false);
                        setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    } catch (InterruptedException | ExecutionException ex) {
                        Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
                        setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                        log.append("Most likely out of memory; please increase heap size of JVM");
                        PopUps.infoBox(
                                "Encountered an error. Most likely out of memory; try increasing the heap size of JVM",
                                "Error");
                    }
                }
            };
            setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            progressBar.setVisible(true);
            progressBar.setIndeterminate(true);
            progressBar.setString("generating...");
            generationWorker.addPropertyChangeListener(new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent evt) {
                    if ("phase".equals(evt.getPropertyName())) {
                        progressBar.setMaximum(100);
                        progressBar.setIndeterminate(false);
                        progressBar.setString("ranking...");
                    } else if ("progress".equals(evt.getPropertyName())) {
                        progressBar.setValue((Integer) evt.getNewValue());
                    }
                }
            });
            generationWorker.execute();

        } else {
            log.append(currentTime.currentTime() + " Nothing to refresh. Generate models first" + newline);
        }

    } //handle show ideal graph button
    else if (e.getSource() == idealGraphButton) {
        if (this.baseModel != null) {
            graph1 = GraphingTool.generateGraph(baseModel, selectedNeed, startValue, endValue, true);//expected graph 
            ChartFrame chartframe1 = new ChartFrame("Ideal results", graph1);
            chartframe1.setPreferredSize(new Dimension(CHART_WIDTH, CHART_HEIGHT));
            chartframe1.pack();
            chartframe1.setLocationByPlatform(true);
            chartframe1.setVisible(true);
        } else {
            log.append(currentTime.currentTime() + " Load a model file first!" + newline);
        }
    } //Handle the graph extend button//Handle the graph extend button
    else if (e.getSource() == expandButton) {
        //make sure there is a graph to show
        if (graph2 == null) {
            log.append(currentTime.currentTime() + " No graph to display. Select one first." + newline);
        } else {
            //this makes sure both graphs have the same y axis:
            //            double lowerBound = min(graph1.getXYPlot().getRangeAxis().getRange().getLowerBound(), graph2.getXYPlot().getRangeAxis().getRange().getLowerBound());
            //            double upperBound = max(graph1.getXYPlot().getRangeAxis().getRange().getUpperBound(), graph2.getXYPlot().getRangeAxis().getRange().getUpperBound());
            //            graph1.getXYPlot().getRangeAxis().setRange(lowerBound, upperBound);
            //            graph2.getXYPlot().getRangeAxis().setRange(lowerBound, upperBound);
            chartPane.removeAll();
            chartPanel = new ChartPanel(graph2);
            chartPanel.setPreferredSize(new Dimension(CHART_WIDTH, CHART_HEIGHT));
            chartPane.add(chartPanel);
            chartPane.add(collapseButton);
            extended = true;
            this.setPreferredSize(new Dimension(this.getWidth() + CHART_WIDTH, this.getHeight()));
            JFrame frame = (JFrame) getRootPane().getParent();
            frame.pack();
        }
    } //Handle the graph collapse button//Handle the graph collapse button
    else if (e.getSource() == collapseButton) {
        System.out.println("resizing by -" + CHART_WIDTH);
        chartPane.removeAll();
        chartPane.add(expandButton);
        this.setPreferredSize(new Dimension(this.getWidth() - CHART_WIDTH, this.getHeight()));
        chartPane.repaint();
        chartPane.revalidate();
        extended = false;
        JFrame frame = (JFrame) getRootPane().getParent();
        frame.pack();
    }
}