Question

Note: this question is a follow up question from this question.

I have just learned the following from my last question:

JPanel has FLowLayout (with the same output as from NullLayout! on resize), accepting only PreferredSize, child aren't resizable with its container, is required to use BorderLayout/GridLayout for simple graph

A demonstration of this principle was also given:

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.math.plot.Plot2DPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.math.plot.Plot2DPanel;

public class MyPlot {

    private JFrame frame = new JFrame("JMathPlot library in a swing application.");
    private JPanel panel = new JPanel();

    public MyPlot() {
        double[] x = new double[]{0, 1, 2, 3, 4, 5};
        double[] y = new double[]{10, 11, 12, 14, 15, 16};
        Plot2DPanel plot = new Plot2DPanel() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 200);
            }
        };
        plot.addLinePlot("my plot", x, y); // add a line plot to the PlotPanel
        panel.setLayout(new BorderLayout());
        panel.add(plot);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.pack();
        frame.setLocation(150, 150);
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MyPlot();
            }
        });
    }
}

Now what I want to achieve is to integrate the graph into a parent JPanel to use it in my swing application.

A visualisation of this would be:

enter image description here

How do I implement the graph in my 'parent' JPanel without violating the constraints for making it work properly?

(I looked into JInternalFrame but couldn't come up with a correct implementation for my problem)

If JInternalFrame is indeed the solution, how should I use it? (Please supply code)

Était-ce utile?

La solution 2

i don't know if that's what you're looking about but i will give you this source code, maybe it will help you:

public class dz extends JFrame {

private JPanel contentPane;

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

/**
 * Create the frame.
 */
public dz() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 917, 788);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    SpringLayout sl_contentPane = new SpringLayout();
    contentPane.setLayout(sl_contentPane);

    // define your data
    double[] x = { 0, 1, 2, 3, 4, 5 };
    double[] y = { 45, 89, 6, 32, 63, 12 };

    // create your PlotPanel (you can use it as a JPanel)
    Plot2DPanel plot = new Plot2DPanel();

    // define the legend position
    plot.addLegend("SOUTH");

    // add a line plot to the PlotPanel
    plot.addLinePlot("my plot", x, y);




    sl_contentPane.putConstraint(SpringLayout.EAST, plot, 600, SpringLayout.WEST, contentPane);
    sl_contentPane.putConstraint(SpringLayout.NORTH, plot, 15, SpringLayout.NORTH, contentPane);
    sl_contentPane.putConstraint(SpringLayout.WEST, plot, 15, SpringLayout.WEST, contentPane);
    sl_contentPane.putConstraint(SpringLayout.SOUTH, plot, 600, SpringLayout.NORTH, contentPane);

/*  panel.setLayout(new GridBagLayout());
    panel.add(plot);
    plot.validate();
    plot.repaint();
    plot.setBounds(50,50,100,100);*/

    contentPane.add(plot);

    JPanel panel = new JPanel();
    panel.setBackground(Color.BLACK);
    sl_contentPane.putConstraint(SpringLayout.NORTH, panel, 20, SpringLayout.NORTH, contentPane);
    sl_contentPane.putConstraint(SpringLayout.WEST, panel, 16, SpringLayout.EAST, plot);
    sl_contentPane.putConstraint(SpringLayout.SOUTH, panel, 408, SpringLayout.NORTH, contentPane);
    sl_contentPane.putConstraint(SpringLayout.EAST, panel, 251, SpringLayout.EAST, plot);
    contentPane.add(panel);


}

}

Autres conseils

bacis stuff, nothing special, most of apps windows is similair desingned

  • JFrame (BorderLayout in API) holding two JPanels

    1. 1st JPanel (GridLayout) with two components (JFrame.WEST/EAST)

      • JTree/JList in JScrollPane for displaying generics options saved in util.List or Tree/Map

      • there will be placed randomPanel

    2. JPanel with plot (JFrame.CENTER)

  • you can to use JSplitPane for two JPanels (a.m.) with hidden, disables, or standard Divider

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top