Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying JPanel to JFrame another class

I have a main JFrame and three java class that contains different pie charts and these piechart are in panel. I want to call the three pie classes in main jframe. How can l do that?enter image description here

// I have three of these Chart classes creating different charts inside panels

public class PiePanel extends Observer {

Singleton connCC = Singleton.getInstance();
Connection con = null;
Statement stm = null;

PiePanel(Subject s) {
panel = new JPanel();
sub = s;
}

@Override
public void update() {
try {
    con = connCC.getDBconnection();
    stm = con.createStatement();
    ResultSet rs = stm.executeQuery("Select pet_name as pet,     count(pet_ID) AS 'count' from Pet group by pet_name");
    DefaultPieDataset dataset = new DefaultPieDataset();
    while (rs.next()) {
        dataset.setValue(rs.getString("pet"), Integer.parseInt(rs.getString("count")));
    }

    JFreeChart pieChart = ChartFactory.createPieChart("Header", dataset, true, true, false);
    PiePlot plot = (PiePlot) pieChart.getPlot();
    plot.setSimpleLabels(true);

    PieSectionLabelGenerator gen = new StandardPieSectionLabelGenerator(
            "{0}: {1} ({2})", new DecimalFormat("0"), new DecimalFormat("0%"));
    plot.setLabelGenerator(gen);

    panel.add(new ChartPanel(pieChart) {
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(335, 235);
        }
    });

    panel.validate();
} catch (Exception e) {
    JOptionPane.showMessageDialog(null, e);
}

}

//below is a class that puts all the panels inside a list

 public class Subject {
private List<Observer> panel = new ArrayList<Observer>();
public Subject(){
   panel.add(new BarPanel(this));
   panel.add(new AreaPanel(this));
   panel.add(new PiePanel(this));
}
public List<Observer> getChart(){
      return panel;
}

}

//main

public class Main {

public static void main(String[] args) {

    Subject s = new Subject();

    while (true) {
        String input = JOptionPane.showInputDialog(null, "Input value:");
        if ("d".equals(input) || "w".equals(input)) {
            try {
                //String value = Integer.parseInt(input);
                s.setState(input);
            } catch (Exception e) {
                System.exit(0);
            }
        } else {
            System.out.println("Wrong Input!");
            System.exit(0);
        }

    }
}

}

Now i want to pass this list in another JFrame class just as in the picture.

like image 478
Meli Avatar asked Dec 02 '25 09:12

Meli


1 Answers

Your problem is that you're adding three components in a default fashion to a container that uses BorderLayout, and this results in each added component in the BorderLayout.CENTER covering all the other components added previously. If you want to display three components you can:

  • Use BorderLayout constants to add the components to different locations within the BorderLayout-using container, or
  • use a different layout, here perhaps a GridLayout(3, 1), or
  • if you want a more complex GUI that shows more components, then nest JPanels, each using its own layout manager.
    • For example, if you want to display the pie charts to the side for instance, then I'd put them into a JPanel that uses a GridLayout, and then add that GridLayout using JPanel to the BorderLayout-using main GUI in the BorderLayout.LINE_END position, or whatever location you desire.

If on the other hand your goal is to swap out one JPanel for another in response to an event, then use a CardLayout to allow easy and efficient swapping of components.

like image 139
Hovercraft Full Of Eels Avatar answered Dec 03 '25 21:12

Hovercraft Full Of Eels



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!