Java Swing App Why Not Centered On Screen?

Frank :

I've downloaded a Java Swing app, although I have this line in the app : demo.setLocationRelativeTo(null);

Why is it not centered on the screen ?

The program looks like this :

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.DefaultXYZDataset;
import org.jfree.data.xy.XYZDataset;

public class Bubble_Chart_AWT extends JFrame
{
  public Bubble_Chart_AWT(String s)
  {
    super(s);
    JPanel jpanel=createDemoPanel();
    jpanel.setPreferredSize(new Dimension(560,370));
    setContentPane(jpanel);
  }

  private JFreeChart createChart(XYZDataset xyzdataset)
  {
    JFreeChart jfreechart=ChartFactory.createBubbleChart("AGE vs WEIGHT vs WORK",
                                                         "Weight",
                                                         "AGE",
                                                         xyzdataset,
                                                         PlotOrientation.HORIZONTAL,
                                                         true,true,false);

    XYPlot xyplot=(XYPlot)jfreechart.getPlot();
    xyplot.setForegroundAlpha(0.65F);
    XYItemRenderer xyitemrenderer=xyplot.getRenderer();
    xyitemrenderer.setSeriesPaint(0,Color.blue);
    NumberAxis numberaxis=(NumberAxis)xyplot.getDomainAxis();
    numberaxis.setLowerMargin(0.2);
    numberaxis.setUpperMargin(0.5);
    NumberAxis numberaxis1=(NumberAxis)xyplot.getRangeAxis();
    numberaxis1.setLowerMargin(0.8);
    numberaxis1.setUpperMargin(0.9);

    return jfreechart;
  }

  public static XYZDataset createDataset()
  {
    DefaultXYZDataset defaultxyzdataset=new DefaultXYZDataset();
    double ad[]={30,40,50,60,70,80};
    double ad1[]={10,20,30,40,50,60};
    double ad2[]={4,5,10,8,9,6};
    double ad3[][]={ad,ad1,ad2};
    defaultxyzdataset.addSeries("Series 1",ad3);

    return defaultxyzdataset;
  }
  public JPanel createDemoPanel()
  {
    JFreeChart jfreechart=createChart(createDataset());
    ChartPanel chartpanel=new ChartPanel(jfreechart);

    chartpanel.setDomainZoomable(true);
    chartpanel.setRangeZoomable(true);

    return chartpanel;
  }

  public static void main(String args[])
  {
    SwingUtilities.invokeLater(() ->
    {
      Bubble_Chart_AWT demo=new Bubble_Chart_AWT("Bubble_Chart_AWT");
      demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      demo.setLocationRelativeTo(null);
      demo.pack();
      demo.setVisible(true);
    });
  }
}

You need this [ jfreechart-1.5.0.jar ] in your project "lib" from http://repo1.maven.org/maven2/org/jfree/jfreechart/1.5.0/ to run it.

What should I do so it auto centers on the screen, usually "demo.setLocationRelativeTo(null)" works, but not in this app.

deHaar :

This might solve your problem, it did on my system:

Call pack() before you call setLocationRelativeTo(null), but call setLocationRelativeTo(null) before setVisible(true), like this:

public static void main(String args[]) {
    SwingUtilities.invokeLater(() -> {
        Bubble_Chart_AWT demo = new Bubble_Chart_AWT("Bubble_Chart_AWT");
        demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        demo.pack();
        demo.setLocationRelativeTo(null);
        demo.setVisible(true);
    });
}

Just switching the lines made the chart be centered on my screen.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=308037&siteId=1