1

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)

Community
  • 1
  • 1
Jean-Paul
  • 19,910
  • 9
  • 62
  • 88
  • Why not have a look at [JInternalFrame](http://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.html), for this task ... ? +1, since you learned loads from your previous post, which was no doubt, very well answered by the genius himself :-) – nIcE cOw Aug 19 '13 at 16:41
  • @nIcEcOw Your comment is incomprehensible. Are you saying that JInternalFrame does indeed hold the solution? – Jean-Paul Aug 19 '13 at 17:02
  • 1
    Yeah I guess so, though I never touched `JInternalFrame` so far, though as you described your question, they appear to me suitable for this sort a task. That is why I posted a comment instead of an answer :-) – nIcE cOw Aug 19 '13 at 17:39

2 Answers2

2

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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • And what if the JFrame must have absolute layout? – Jean-Paul Aug 19 '13 at 17:33
  • then you have to calculate inner area for JComponents placed in JFrame, assumed its about basic algebra – mKorbel Aug 19 '13 at 18:14
  • Alright, let's say I have 10 `random JPanel's` like in my picture, such that the parent JPanel has absolute (XY) layout. Now how would I use JComponents then? (I don't get what you mean with basic algebra) – Jean-Paul Aug 19 '13 at 19:46
  • I don't get what you mean with basic algebra == insets from container to dovide with childs – mKorbel Aug 20 '13 at 06:44
  • basicall time for Null Layout is greater than learning/trying standard or custom LayoutManager, especially you can to determine which is resisable, how is resizable, ratio in GUI, etc. – mKorbel Aug 20 '13 at 06:48
1

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);


}

}

AlexVogel
  • 10,601
  • 10
  • 61
  • 71