1

I am a bloody beginner with JFreeCharts, however, this is my first expirement, but my Program does not display anything, eclipse says that it is running, but there is no window or whatever displayed. Of course I made sure to try out a proper range of things such as .validate or .setVisible true...

package at.htlklu.chart;

import java.awt.BorderLayout;

public class ChartsExperiments extends JFrame {

    private JPanel contentPane;
    private JTextField txt_1;
    private JTextField txt_2;
    private JTextField txt_3;
    private JTextField txt_4;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ChartsExperiments frame = new ChartsExperiments();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ChartsExperiments() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 852, 719);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        final JPanel panel = new JPanel();
        panel.setBackground(Color.LIGHT_GRAY);
        panel.setBounds(6, 6, 840, 447);
        contentPane.add(panel);
        panel.setLayout(new BorderLayout(0, 0));

        JLabel lblStat1 = new JLabel("statistic_1");
        lblStat1.setBounds(6, 465, 75, 16);
        contentPane.add(lblStat1);

        JLabel lblStat2 = new JLabel("statistic_2");
        lblStat2.setBounds(93, 465, 75, 16);
        contentPane.add(lblStat2);

        JLabel lblStat3 = new JLabel("statistic_3");
        lblStat3.setBounds(180, 465, 75, 16);
        contentPane.add(lblStat3);

        JLabel lblStat4 = new JLabel("statistic_4");
        lblStat4.setBounds(267, 465, 75, 16);
        contentPane.add(lblStat4);

        JButton btnView = new JButton("View");
        btnView.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int s1 = Integer.parseInt(txt_1.getText());
                int s2 = Integer.parseInt(txt_2.getText());
                int s3 = Integer.parseInt(txt_3.getText());
                int s4 = Integer.parseInt(txt_4.getText());

                DefaultCategoryDataset dataset = new DefaultCategoryDataset();

                dataset.setValue(s1, "", "Statistic_1");
                dataset.setValue(s2, "", "Statistic_2");
                dataset.setValue(s3, "", "Statistic_3");
                dataset.setValue(s4, "", "Statistic_4");

                JFreeChart chart = ChartFactory.createBarChart("Statistics", "", "", dataset, PlotOrientation.HORIZONTAL, false, false, false);
                CategoryPlot catPlot = chart.getCategoryPlot();
                catPlot.setRangeGridlinePaint(Color.BLACK);

                ChartPanel chartPanel = new ChartPanel(chart);
                chartPanel.setVisible(true);
                panel.removeAll();
                panel.setVisible(true);
                panel.add(chartPanel, BorderLayout.CENTER);
                panel.validate();


            }
        });
        btnView.setBounds(389, 489, 117, 29);
        contentPane.add(btnView);

        txt_1 = new JTextField();
        txt_1.setBounds(6, 488, 75, 28);
        contentPane.add(txt_1);
        txt_1.setColumns(10);

        txt_2 = new JTextField();
        txt_2.setColumns(10);
        txt_2.setBounds(93, 488, 75, 28);
        contentPane.add(txt_2);

        txt_3 = new JTextField();
        txt_3.setColumns(10);
        txt_3.setBounds(180, 488, 75, 28);
        contentPane.add(txt_3);

        txt_4 = new JTextField();
        txt_4.setColumns(10);
        txt_4.setBounds(267, 488, 75, 28);
        contentPane.add(txt_4);
    }
}
cello
  • 5,356
  • 3
  • 23
  • 28
  • 2
    Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammer Jan 09 '15 at 08:34

1 Answers1

1

Are you sure about this? I copied and tried your program and it's executing very well.

enter image description here

  • I just don't get the window popping up, all other programs are executing on mine as well, somehow this doesn't... – Chris Poier Jan 16 '15 at 07:09