Example usage for javax.swing JFrame pack

List of usage examples for javax.swing JFrame pack

Introduction

In this page you can find the example usage for javax.swing JFrame pack.

Prototype

@SuppressWarnings("deprecation")
public void pack() 

Source Link

Document

Causes this Window to be sized to fit the preferred size and layouts of its subcomponents.

Usage

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame("Fixed size content");
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    Container c = f.getContentPane();
    c.setBackground(Color.YELLOW);

    Dimension d = new Dimension(400, 40);
    c.setPreferredSize(d);/*w w  w. ja v a2  s. c  o m*/
    f.pack();
    f.setResizable(false);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    SpinnerNumberModel model = new SpinnerNumberModel(0.0, -1000.0, 1000.0, 0.1);
    JSpinner s = new JSpinner(model);
    JSpinner.NumberEditor editor = new JSpinner.NumberEditor(s);
    s.setEditor(editor);//from w  w w.j  av a2 s. c  om
    JTextField stepText = new JTextField(10);
    JButton bStepSet = new JButton("Set Step");
    bStepSet.addActionListener(e -> {
        Double val = Double.parseDouble(stepText.getText().trim());
        model.setStepSize(val);
    });
    JFrame f = new JFrame();
    Container c = f.getContentPane();
    c.add(s);
    JPanel southPanel = new JPanel();
    southPanel.add(stepText);
    southPanel.add(bStepSet);
    c.add(southPanel, BorderLayout.SOUTH);
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:CloseAction.java

public static void main(String[] args) {
    JFrame frame = new JFrame("JFrame");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    JButton closeButton = new JButton(new CloseAction());
    contentPane.add(closeButton);/*from   w  ww  .  ja v a 2s  . c o  m*/

    frame.pack();
    frame.setVisible(true);
}

From source file:com.bpd.jfreechart.Chart.java

/**
 * Test program that will display a JFreeChart showing interpolated data points.
 * /* ww  w.ja v  a2  s  .  com*/
 * @param args <b>"1"</b> to display Series 1. <b>"2"</b> to display Series 2. <b>"0"</b> to
 * display both series.
 */
public static void main(String... args) {
    if (args.length != 1) {
        System.err.println("Usage: java Chart [0|1|2]\n\n  -- 0: Display Series 1.");
        System.err.println("  -- 1: Display Series 2.\n  -- 2: Display both series.");
        return;
    }

    String option = args[0];
    if (!"0".equals(option) && !"1".equals(option) && !"2".equals(option)) {
        System.err.println("Invalid argument: " + option);
        return;
    }

    List<Point<Number, Number>> list1 = new ArrayList<Point<Number, Number>>();
    list1.add(new Point<Number, Number>(100, 100));
    list1.add(new Point<Number, Number>(200, 200));
    list1.add(new Point<Number, Number>(300, 400));
    list1.add(new Point<Number, Number>(400, 200));
    list1.add(new Point<Number, Number>(500, 100));

    List<Point<Number, Number>> list2 = new ArrayList<Point<Number, Number>>();
    list2.add(new Point<Number, Number>(50, 100.0));
    list2.add(new Point<Number, Number>(150, 200.0));
    list2.add(new Point<Number, Number>(250, 400.0));
    list2.add(new Point<Number, Number>(350, 600.0));
    list2.add(new Point<Number, Number>(450, 400.0));
    list2.add(new Point<Number, Number>(550, 200.0));

    List<List<Point<Number, Number>>> lists = new ArrayList<List<Point<Number, Number>>>();
    List<Point<Number, Number>> extrapolatedList1 = new ArrayList<Point<Number, Number>>();
    List<Point<Number, Number>> extrapolatedList2 = new ArrayList<Point<Number, Number>>();
    if ("1".equals(option)) {
        lists.add(list1);
        extrapolatedList1 = Interpolator.interpolate(list1, lists);
    } else if ("2".equals(option)) {
        lists.add(list2);
        extrapolatedList2 = Interpolator.interpolate(list2, lists);
    } else if ("0".equals(option)) {
        lists.add(list1);
        lists.add(list2);
        extrapolatedList1 = Interpolator.interpolate(list1, lists);
        extrapolatedList2 = Interpolator.interpolate(list2, lists);
    }

    TimeSeries series1 = new TimeSeries("Series 1", FixedMillisecond.class);
    for (Point<Number, Number> dataPoint : extrapolatedList1) {
        if ("1".equals(option) || "0".equals(option)) {
            series1.add(new FixedMillisecond(dataPoint.getX().longValue()), dataPoint.getY());
        }
        System.err.println(dataPoint.getX() + "\t" + dataPoint.getY());
    }

    System.err.println();

    TimeSeries series2 = new TimeSeries("Series 2", FixedMillisecond.class);
    for (Point<Number, Number> dataPoint : extrapolatedList2) {
        if ("2".equals(option) || "0".equals(option)) {
            series2.add(new FixedMillisecond(dataPoint.getX().longValue()), dataPoint.getY());
        }
        System.err.println(dataPoint.getX() + "\t" + dataPoint.getY());
    }

    TimeSeriesCollection dataset = new TimeSeriesCollection();
    if ("1".equals(option)) {
        dataset.addSeries(series1);
    } else if ("2".equals(option)) {
        dataset.addSeries(series2);
    } else if ("0".equals(option)) {
        dataset.addSeries(series1);
        dataset.addSeries(series2);
    }

    JFreeChart chart = ChartFactory.createXYLineChart("Test", null, null, dataset, PlotOrientation.VERTICAL,
            false, true, false);

    XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) chart.getXYPlot().getRenderer();
    renderer.setSeriesShapesVisible(0, true);
    renderer.setSeriesShapesVisible(1, true);

    ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));

    JFrame frame = new JFrame();
    frame.getContentPane().add(chartPanel);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override/*w  w w.  ja v a  2s.  co m*/
        public void run() {
            JFrame frame = new JFrame("Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            frame.add(new JScrollPane(new MyTextArea()));
            frame.pack();
            frame.setVisible(true);
        }
    });
}

From source file:Main.java

public static void main(String[] a) {
    JFrame frame = new JFrame("JFileChooser Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JFileChooser fileChooser = new JFileChooser(".");

    fileChooser.rescanCurrentDirectory();
    frame.add(fileChooser, BorderLayout.CENTER);

    frame.pack();
    frame.setVisible(true);//from w  w w .ja  v  a2s  .c o m
}

From source file:Main.java

public static void main(String[] args) throws InterruptedException {
    JFrame f = new JFrame();

    DefaultTableModel m = new DefaultTableModel();
    JTable t = new JTable(m);

    f.add(new JScrollPane(t), BorderLayout.CENTER);
    f.setVisible(true);/* ww w . j a  va2s  .c o  m*/
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    m.addColumn("A");
    m.addRow(new String[] { "A1" });

    Thread.sleep(2000);

    m.addColumn("B");
    m.addRow(new String[] { "A2", "B2" });
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JButton button = new JButton("Click me");
    button.addActionListener(e -> JOptionPane.showMessageDialog(frame, "Hello World!"));
    frame.getContentPane().add(button);//from w  w  w .j  a  v a2s.  c  om
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] a) {
    JFrame frame = new JFrame("JFileChooser Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JFileChooser fileChooser = new JFileChooser(".");
    int type = fileChooser.getDialogType();

    frame.add(fileChooser, BorderLayout.CENTER);

    frame.pack();
    frame.setVisible(true);/*from   w  ww .j a  v a2  s  . c o m*/
}

From source file:MyActionListener.java

public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JDialog.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("ActionListener Test 1");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Register");
    button.addActionListener(new MyActionListener());
    frame.getContentPane().add(button);/*from w  w w  .  j  a va  2s  .c  o m*/
    frame.pack();
    frame.setVisible(true);
}