JFreeChart hide behind other one

80 views Asked by At

I was trying to build UI in IntelliJ but it was hard so I tried to build it in Eclipse. Now UI looks little better but still poor. One of chart (the cener one) should be biger and second one on south should be smaller but flat and long. I tried to use preffered size or validate or repaint but still nothing. How to anchor them to side borders, and avoid to cover them self?

public class MainWindow extends JFrame{

private JPanel contentPane;

private XYSeries daneXYSciezki;
private XYSeries daneXYWys;

private final XYSeriesCollection wykCenter;
private final XYSeriesCollection wykSouth;

public MainWindow(){
   setTitle("ParserGPS");
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   setBounds(100, 100, 880, 640);
   contentPane = new JPanel();
   contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
   setContentPane(contentPane);
   contentPane.setLayout(new BorderLayout(0, 0));

   JPanel panelNorth = new JPanel();
   FlowLayout flowLayout = (FlowLayout) panelNorth.getLayout();
   flowLayout.setAlignment(FlowLayout.LEFT);
   contentPane.add(panelNorth, BorderLayout.NORTH);

   JButton btnWczytajPlik = new JButton("Wczytaj plik");
   panelNorth.add(btnWczytajPlik);

   JLabel lblOdlego = new JLabel("Odległość:");
   panelNorth.add(lblOdlego);

   JLabel lblm = new JLabel("0m");
   panelNorth.add(lblm);

   JLabel lblCzas = new JLabel("Czas:");
   panelNorth.add(lblCzas);

   JLabel label = new JLabel("00:00:00");
   panelNorth.add(label);

   JLabel lblPrdko = new JLabel("Prędkość:");
   panelNorth.add(lblPrdko);

   JLabel lblkmh = new JLabel("0km/h");
   panelNorth.add(lblkmh);

   JLabel lblNrSat = new JLabel("Nr sat:");
   panelNorth.add(lblNrSat);

   JLabel label_1 = new JLabel("0");
   panelNorth.add(label_1);

   JLabel lblGga = new JLabel("GGA:");
   lblGga.setHorizontalAlignment(SwingConstants.CENTER);
   panelNorth.add(lblGga);

   JLabel label_2 = new JLabel("0/0");
   panelNorth.add(label_2);

   JLabel lblGsa = new JLabel("GSA:");
   panelNorth.add(lblGsa);

   JLabel label_3 = new JLabel("0/0");
   panelNorth.add(label_3);

   JLabel lblNewLabel = new JLabel("RMC:");
   panelNorth.add(lblNewLabel);

   JLabel label_4 = new JLabel("0/0");
   panelNorth.add(label_4);

   JLabel lblGll = new JLabel("GLL:");
   panelNorth.add(lblGll);

   JLabel label_5 = new JLabel("0/0");
   panelNorth.add(label_5);

   JLabel lblVtg = new JLabel("VTG:");
   panelNorth.add(lblVtg);

   JLabel label_6 = new JLabel("0/0");
   panelNorth.add(label_6);

   JPanel jpCenter = new JPanel();
   contentPane.add(jpCenter, BorderLayout.CENTER);
   jpCenter.setPreferredSize(new Dimension(785, 440));

   JPanel jpSouth = new JPanel();
   FlowLayout flowLayout_1 = (FlowLayout) jpSouth.getLayout();
   flowLayout_1.setAlignment(FlowLayout.TRAILING);
   contentPane.add(jpSouth, BorderLayout.SOUTH);

   this.daneXYSciezki = new XYSeries("Trasa", false);
   wykCenter = new XYSeriesCollection(this.daneXYSciezki);
   final JFreeChart jfcWykCenter = createChart(wykCenter);
   final ChartPanel jfcPanelCenter = new ChartPanel(jfcWykCenter);
   jpCenter.add(jfcPanelCenter,BorderLayout.CENTER);

   this.daneXYSciezki = new XYSeries("Wysokość", false);
   wykSouth = new XYSeriesCollection(this.daneXYSciezki);
   final JFreeChart jfcWykSouth = createChart(wykSouth);
   final ChartPanel jfcPanelSouth = new ChartPanel(jfcWykSouth);
   jpSouth.add(jfcPanelSouth,BorderLayout.CENTER);
   repaint();
   revalidate();
    }

    private JFreeChart createChart(final XYDataset dataset) {
        final JFreeChart result = ChartFactory.createXYLineChart(
                "",
                "Szerokość",
                "Długość",
                dataset);
        final XYPlot plot = result.getXYPlot();
        NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
        yAxis.setAutoRangeIncludesZero(false);
        yAxis.setAutoRange(true);
        customizeChart(result);
        return result;
    }

    private void customizeChart(JFreeChart chart) {
        XYPlot plot = chart.getXYPlot();
        XYLineAndShapeRenderer renderer;
        renderer = new XYLineAndShapeRenderer(true, true);
        renderer.setSeriesShapesVisible(0, true);
        renderer.setSeriesShapesVisible(1, false);
        renderer.setSeriesPaint(0, Color.RED);
        renderer.setSeriesStroke(0, new BasicStroke(1.0f));
        plot.setRenderer(renderer);
        plot.setBackgroundPaint(Color.WHITE);
        plot.setRangeGridlinesVisible(true);
        plot.setRangeGridlinePaint(Color.BLACK);
        plot.setDomainGridlinesVisible(true);
        plot.setDomainGridlinePaint(Color.BLACK);
    }
}

Window I created

After @m.cekiera advices

public class MainWindow extends JFrame{
private JPanel contentPane;

private XYSeries daneXYSciezki;
private XYSeries daneXYWys;

private final XYSeriesCollection wykCenter;
private final XYSeriesCollection wykSouth;

public MainWindow(){
    setTitle("ParserGPS");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 880, 640);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(new BorderLayout(0, 0));

    JPanel panelNorth = new JPanel();
    FlowLayout flowLayout = (FlowLayout) panelNorth.getLayout();
    flowLayout.setAlignment(FlowLayout.LEFT);
    contentPane.add(panelNorth,BorderLayout.NORTH);

    JButton btnWczytajPlik = new JButton("Wczytaj plik");
    panelNorth.add(btnWczytajPlik);

    JLabel lblOdlego = new JLabel("Odległość:");
    panelNorth.add(lblOdlego);

    JLabel lblm = new JLabel("0m");
    panelNorth.add(lblm);

    JLabel lblCzas = new JLabel("Czas:");
    panelNorth.add(lblCzas);

    JLabel label = new JLabel("00:00:00");
    panelNorth.add(label);

    JLabel lblPrdko = new JLabel("Prędkość:");
    panelNorth.add(lblPrdko);

    JLabel lblkmh = new JLabel("0km/h");
    panelNorth.add(lblkmh);

    JLabel lblNrSat = new JLabel("Nr sat:");
    panelNorth.add(lblNrSat);

    JLabel label_1 = new JLabel("0");
    panelNorth.add(label_1);

    JLabel lblGga = new JLabel("GGA:");
    //lblGga.setHorizontalAlignment(SwingConstants.CENTER);
    panelNorth.add(lblGga);

    JLabel label_2 = new JLabel("0/0");
    panelNorth.add(label_2);

    JLabel lblGsa = new JLabel("GSA:");
    panelNorth.add(lblGsa);

    JLabel label_3 = new JLabel("0/0");
    panelNorth.add(label_3);

    JLabel lblNewLabel = new JLabel("RMC:");
    panelNorth.add(lblNewLabel);

    JLabel label_4 = new JLabel("0/0");
    panelNorth.add(label_4);

    JLabel lblGll = new JLabel("GLL:");
    panelNorth.add(lblGll);

    JLabel label_5 = new JLabel("0/0");
    panelNorth.add(label_5);

    JLabel lblVtg = new JLabel("VTG:");
    panelNorth.add(lblVtg);

    JLabel label_6 = new JLabel("0/0");
    panelNorth.add(label_6);

    JPanel jpCenter = new JPanel(new BorderLayout());
    contentPane.add(jpCenter, BorderLayout.CENTER);
    //jpCenter.setPreferredSize(new Dimension(785, 440));

    JPanel jpSouth = new JPanel(new BorderLayout());
    //FlowLayout flowLayout_1 = (FlowLayout) jpSouth.getLayout();
   // flowLayout_1.setAlignment(FlowLayout.TRAILING);
    contentPane.add(jpSouth, BorderLayout.SOUTH);

    this.daneXYSciezki = new XYSeries("Trasa", false);
    wykCenter = new XYSeriesCollection(this.daneXYSciezki);
    final JFreeChart jfcWykCenter = createChart(wykCenter);
    final ChartPanel jfcPanelCenter = new ChartPanel(jfcWykCenter);
    jpCenter.add(jfcPanelCenter,BorderLayout.NORTH);

    this.daneXYSciezki = new XYSeries("Wysokość", false);
    wykSouth = new XYSeriesCollection(this.daneXYSciezki);
    final JFreeChart jfcWykSouth = createChart(wykSouth);
    final ChartPanel jfcPanelSouth = new ChartPanel(jfcWykSouth);
    jpCenter.add(jfcPanelSouth,BorderLayout.SOUTH);
    }

...
}

after 3rd correct

1

There are 1 answers

4
m.cekiera On

As your code is not MCVE I could not run it with changes, but I think your problem is bad layout settings. For example, you create a new JPanel:

JPanel jpSouth = new JPanel();
FlowLayout flowLayout_1 = (FlowLayout) jpSouth.getLayout();
flowLayout_1.setAlignment(FlowLayout.TRAILING);

You are aware that it is using FlowLayout by defaul, by later you use:

jpSouth.add(jfcPanelSouth,BorderLayout.CENTER);

but you never change layout to BorderLayout. You cannot mix layout of one particular container. But this is not what couse you problem directly.

Your BorderLayout.SOUTH area stretch horizontally, to fit components inside, and BorderLayout.CENTER takes all spece left(or not). This is how those parts of BorderLayout works. So you can change where you put particular components, or change layout at all.

I suggest to try with:

JPanel jpCenter = new JPanel(new BorderLayout());
...
jpCenter.add(jfcPanelCenter,BorderLayout.NORTH);
...
jpCenter.add(jfcPanelSouth,BorderLayout.SOUTH); //not to contentPane!

this will change an arrangement. However I am not sure, is it what you want. If not, try with different layout, for example BoxLayout. Also I would rather use pack() and set size of components, than setBounds on a countainer.

You can also use GUI builders, like Netbeans IDE GUI Builder if you want to create GUI but don't care how.