Example usage for javax.swing JFrame validate

List of usage examples for javax.swing JFrame validate

Introduction

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

Prototype

public void validate() 

Source Link

Document

Validates this container and all of its subcomponents.

Usage

From source file:PictureScaler.java

private static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setLayout(new BorderLayout());
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    PictureScaler test = new PictureScaler();
    //f.setSize(scaleW + (4 * PADDING), scaleH + (4 * PADDING));
    f.add(test);//from www  .  ja v a2 s. co m
    f.validate();
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

/**
 * Centers the frame on the screen and sets its bounds. THis method should be
 * used after you call frame.pack() or frame.setSize().
 * @param frame/*from   w  ww. j a  v  a2  s  .  c  o  m*/
 */
public static void centerFrame(JFrame frame) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Point center = ge.getCenterPoint();
    Rectangle bounds = ge.getMaximumWindowBounds();
    int w = Math.max(bounds.width / 2, Math.min(frame.getWidth(), bounds.width));
    int h = Math.max(bounds.height / 2, Math.min(frame.getHeight(), bounds.height));
    int x = center.x - w / 2, y = center.y - h / 2;

    frame.setBounds(x, y, w, h);

    if ((w == bounds.width) && (h == bounds.height)) {
        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
    }

    frame.validate();
}

From source file:ca.nengo.plot.impl.DefaultPlotter.java

public void doPlotMSE(NEFEnsemble ensemble, DecodedOrigin origin, String name) {
    float[] error = new float[origin.getDimensions()];
    float mseAvg; //MSE for all of the dimensions of the origin together

    JPanel panel = new JPanel();
    JFrame frame = createFrame();
    frame.setVisible(true);//ww w  . j  av  a2s.  c  o m
    long time = System.currentTimeMillis() - 21;

    //plot MSE on continuously updating graph as more samples are used in the calculation
    for (int i = 1; i == 1 || frame.isVisible(); i++) { //will crash if runtime exceeds 4.1 years
        //synchronized(ensemble){
        error = MU.sum(MU.prod(error, ((i - 1f) / i)), MU.prod(origin.getError(1), 1f / i));
        //}
        mseAvg = MU.mean(error);
        if ((System.currentTimeMillis() - time) > 20l) { //frame limiter
            panel = getBarChart(error, "MSE per Dimension for Origin: " + origin.getName());
            frame.getContentPane().removeAll();
            frame.getContentPane().add(panel, BorderLayout.CENTER);
            frame.setTitle("Origin MSE Plot (Overall MSE=" + mseAvg + ")");
            frame.validate();

            time = System.currentTimeMillis();
        }
        if (i == 1) {
            frame.pack();
            frame.setVisible(true);
        }
    }
}

From source file:SoundManagerTest.java

/**
 * Creates the UI, which is a row of buttons.
 *///from   ww w.j a v  a 2s  . c  o  m
public void initUI() {
    // make sure Swing components don't paint themselves
    NullRepaintManager.install();

    JFrame frame = super.screen.getFullScreenWindow();
    Container contentPane = frame.getContentPane();

    contentPane.setLayout(new FlowLayout());
    contentPane.add(createButton(PAUSE, true));
    contentPane.add(createButton(PLAY_MUSIC, true));
    contentPane.add(createButton(MUSIC_DRUMS, false));
    contentPane.add(createButton(PLAY_SOUND, false));
    contentPane.add(createButton(PLAY_ECHO_SOUND, false));
    contentPane.add(createButton(PLAY_LOOPING_SOUND, true));
    contentPane.add(createButton(PLAY_MANY_SOUNDS, false));
    contentPane.add(createButton(EXIT, false));

    // explicitly layout components (needed on some systems)
    frame.validate();
}

From source file:modnlp.capte.AlignmentInterfaceWS.java

public void actionPerformed(ActionEvent e) {

    //Handle open button action.
    if (e.getSource() == openButton) {
        int returnVal = fc.showOpenDialog(AlignmentInterfaceWS.this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            //This is where a real application would open the file.
            sourceFile = file.getAbsolutePath();
            log.append("Source File: " + sourceFile + "." + newline);
        } else {//from  w  ww .ja  v  a2  s  .  c o  m
            log.append("Open command cancelled by user." + newline);
        }
        log.setCaretPosition(log.getDocument().getLength());

    }
    //deleteSegment
    else if (e.getSource() == deleteSegment) {
        Object[] o;
        String ivalue;
        int[] selected = table.getSelectedRows();
        ExampleTableModel em = (ExampleTableModel) table.getModel();

        if (selected.length < 1) {

            System.out.println("Please select at least one row");
            JOptionPane.showMessageDialog(edit, "Please select at least one row");
        } else {
            System.out.println("Deleting rows...");
            for (int i = selected.length - 1; i > -1; i--) {
                em.removeRow(selected[i]);
                //update numbers

                System.out.println("Removed row " + selected[i]);
            }
        }
        //Table should update itself automatically
    } else if (e.getSource() == moveUp) {
        int[] selected = table.getSelectedRows();
        ExampleTableModel em = (ExampleTableModel) table.getModel();
        if (selected.length > 1) {
            System.out.println("You can only move one segment at a time!");
            JOptionPane.showMessageDialog(edit, "You can only move one segment at a time!");
        } else if (selected[0] == 0) {
            System.out.println("Can't move up anymore!");
            JOptionPane.showMessageDialog(edit, "Can't move up anymore!");
        } else {
            System.out.println("Moving " + selected[0]);
            em.moveSegmentUp(selected[0]);
            //Table should repaint
        }
    } else if (e.getSource() == moveDown) {

        int[] selected = table.getSelectedRows();
        ExampleTableModel em = (ExampleTableModel) table.getModel();
        if (selected.length > 1) {

            System.out.println("You can only move one segment at a time!");
            JOptionPane.showMessageDialog(edit, "You can only move one segment at a time!");
        } else if (selected[0] == em.getRowCount() - 1) {

            System.out.println("Can't move down anymore!");
            JOptionPane.showMessageDialog(edit, "Can't move down anymore");
        } else {
            System.out.println("Moving " + selected[0]);
            em.moveSegmentDown(selected[0]);
            //Table should repaint
        }
    } else if (e.getSource() == mergeSource) {
        int[] selected = table.getSelectedRows();
        ExampleTableModel em = (ExampleTableModel) table.getModel();

        if (selected.length < 2 || selected.length > 2) {

            System.out.println("Please select two rows to merge ");
            JOptionPane.showMessageDialog(edit, "Please select two rows to merge");
        } else if (selected[0] - selected[1] > 1 || selected[0] - selected[1] < -1) {

            System.out.println("Can only merge adjacent rows");
            JOptionPane.showMessageDialog(edit, "Can only merge adjacent rows");
        } else {
            System.out.println("Merging source in rows " + selected[0] + " " + selected[1]);
            em.mergeSource(selected[0], selected[1]);
        }
    } else if (e.getSource() == mergeTarget) {
        int[] selected = table.getSelectedRows();
        ExampleTableModel em = (ExampleTableModel) table.getModel();

        if (selected.length < 2 || selected.length > 2) {

            System.out.println("Please select two rows to merge ");
            JOptionPane.showMessageDialog(edit, "Please select two rows to merge");
        } else if (selected[0] - selected[1] > 1 || selected[0] - selected[1] < -1) {

            System.out.println("Can only merge adjacent rows");
            JOptionPane.showMessageDialog(edit, "Can only merge adjacent rows");
        } else {
            System.out.println("Merging target in " + selected[0] + " " + selected[1]);
            em.mergeTarget(selected[0], selected[1]);
        }

    } else if (e.getSource() == newSegment) {
        int[] selected = table.getSelectedRows();
        ExampleTableModel em = (ExampleTableModel) table.getModel();

        if (selected.length < 1) {

            System.out.println("Please select a position to insert at:");
            JOptionPane.showMessageDialog(edit, "Please select a position to insert at");
        } else {
            System.out.println("Inserting new segment at " + (selected[0] + 1));
            //insert empty string array
            Object[] sa = new Object[6];
            sa[0] = "";
            sa[1] = "";
            sa[2] = "0.0";
            sa[3] = new Boolean(false);
            sa[4] = "0";
            sa[5] = em.getValueAt((selected[0]), 5) + "(+)";

            em.insertRow(sa, (selected[0] + 1));

        }
    } else if (e.getSource() == lockSelected) {
        int[] selected = table.getSelectedRows();
        ExampleTableModel em = (ExampleTableModel) table.getModel();

        if (selected.length < 1) {

            System.out.println("Please select some rows to lock:");
            JOptionPane.showMessageDialog(edit, "Please select some rows to lock:");
        } else {

            //lock selected rows

            for (int i = 0; i < selected.length; i++) {
                em.lockRow(selected[i]);
                System.out.println("Locking row " + selected[i]);

            }

        }
    } else if (e.getSource() == unlockSelected) {
        int[] selected = table.getSelectedRows();
        ExampleTableModel em = (ExampleTableModel) table.getModel();

        if (selected.length < 1) {

            System.out.println("Please select some rows to unlock:");
            JOptionPane.showMessageDialog(edit, "Please select some rows to unlock:");
        } else {

            //lock selected rows
            for (int i = 0; i < selected.length; i++) {
                em.unlockRow(selected[i]);
                System.out.println("Unlocking row " + selected[i]);
            }

        }
    } else if (e.getSource() == reAlign) {
        //  if(true){
        // JOptionPane.showMessageDialog(edit,"This feature is currently disabled");
        // }else{
        reNumber++;
        ExampleTableModel em = (ExampleTableModel) table.getModel();

        // Get list of locked segments
        // Find lowest locked segment
        // Realign from lowest locked segment
        // Join the realigned bit back up with the locked bit
        // refresh the table
        int lowestlock = 0;
        Vector<Object[]> slice = new Vector<Object[]>();
        Vector<Object[]> result = new Vector<Object[]>();
        Vector<Object[]> locked = new Vector<Object[]>();
        Boolean b = new Boolean(true);
        for (int i = 0; i < em.getRowCount(); i++) {
            b = (Boolean) em.getValueAt(i, 3);
            if (b.booleanValue() == true) {
                lowestlock = i;
            }
        }

        //get slice of table for realignment
        System.out.println("The lowest lock point is " + (lowestlock));
        System.out.println("Realigning from row:" + (lowestlock + 1) + " to : " + em.getRowCount());
        System.out.println("Total size of realign array =:" + (em.getRowCount() - (lowestlock)));
        //Get locked bits
        for (int h = 0; h < lowestlock + 1; h++) {

            locked.add(em.getRow(h));
        }
        //Get bits to realign
        for (int j = lowestlock + 1; j < em.getRowCount(); j++) {

            slice.add(em.getRow(j));

        }
        //flush 
        em.flush();
        for (int z = 0; z < locked.size(); z++) {
            em.insertRow(locked.get(z), z);
        }
        // System.out.println("Total size of array after bits removed  = " + (em.getRowCount()));
        //get the directory where the source files came from
        File parent = new File(sourceFile).getParentFile();
        String dir = parent.getAbsolutePath();
        //create files
        File sf = null;
        File tf = null;
        try {
            sf = File.createTempFile("source", "tmp");
            tf = File.createTempFile("target", "tmp");
        } catch (IOException ef) {
            ef.printStackTrace();
        }
        System.out.println("Writing temp file:" + sf.getName());
        System.out.println("Writing temp file:" + tf.getName());
        // File mf = new File("merged.tmp");
        //get absolute paths
        String sourceF = sf.getAbsolutePath();
        String targetF = tf.getAbsolutePath();
        //String alignF = mf.getAbsolutePath();
        //write out source and target to files
        //NEW write files to server and return string
        String alignment = "";
        AlignerUtils.reWriteAlignment(targetF, sourceF, slice);
        try {

            alignment = AlignerUtils.MultiPartFileUpload(targetF, sourceF);

        } catch (IOException es) {
            es.printStackTrace();
        }
        //convert the String to a Vector form
        result = AlignerUtils.StringToData(alignment, true, reNumber);
        // append the resultant file to the table
        //System.out.println("Total size of array before bits inserted  = " + (em.getRowCount()));
        for (int y = 0, z = em.getRowCount(); y < result.size(); y++, z++) {

            em.insertRow(result.get(y), z);
            System.out.println("Inserting at position: " + z);
            System.out.println("Inserting from position: " + y);

        }
        //  System.out.println("Total size of array after bits inserted  = " + (em.getRowCount()));
        // }
    } else if (e.getSource() == saveButton) {
        int returnVal = fc.showSaveDialog(AlignmentInterfaceWS.this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            //This is where a real application would save the file.
            targetFile = file.getAbsolutePath();
            log.append("Target File " + targetFile + "." + newline);
        } else {
            log.append("Save command cancelled by user." + newline);
        }
        log.setCaretPosition(log.getDocument().getLength());
    } else if (e.getSource() == export) {
        int returnVal = fc.showSaveDialog(AlignmentInterfaceWS.this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            outputFile = file.getAbsolutePath();
            //aserver.writeAlignment(targetFile,data);
            AlignerUtils.writeAlignment(outputFile, data);
            log.append(newline + "Saving " + outputFile + "." + newline);
        } else {
            log.append("Save command cancelled by user." + newline);
        }
        log.setCaretPosition(log.getDocument().getLength());
    } else if (e.getSource() == alignButton) {

        if (sl.getText().length() >= 2 && tl.getText().length() >= 2) {

            log.append("Attempting to align texts");
            sourcel = sl.getText();
            targetl = tl.getText();
            String aligned = "";
            try {
                aligned = AlignerUtils.MultiPartFileUpload(sourceFile, targetFile);
            } catch (IOException ed) {

                ed.printStackTrace();
            }
            //Convert string to alignment format
            data = AlignerUtils.StringToData(aligned, false, 0);

            int i = 0;

            //
            // AlreadyRun = true;
            if (i == 0) {
                //log.setCaretPosition(log.getDocument().getLength());
                log.append("\nAutomatic alignment successful!");
                log.append("\nOpening display window......");
                //Set up the editor window
                JFrame edit = new JFrame("Alignment Editor");
                cols = new Vector<String>();
                cols.add("Source");
                cols.add("Target");
                cols.add("Score");
                cols.add("Lock");
                cols.add("Index");
                cols.add("Orig");

                System.out.println("Size of data array " + data.size());
                System.out.println(data.get(0)[0]);
                ex = new ExampleTableModel(cols, data);
                //ex.addTableModelListener(this);
                table = new JTable(ex);
                table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

                TableColumnModel cmodel = table.getColumnModel();

                cmodel.getColumn(0).setCellRenderer(new TextAreaRenderer());
                cmodel.getColumn(1).setCellRenderer(new TextAreaRenderer());
                cmodel.getColumn(2).setCellRenderer(new TextAreaRenderer());
                cmodel.getColumn(4).setCellRenderer(new TextAreaRenderer());
                cmodel.getColumn(5).setCellRenderer(new TextAreaRenderer());
                TextAreaEditor textEditor = new TextAreaEditor();
                textEditor.addCellEditorListener(this);
                cmodel.getColumn(0).setCellEditor(textEditor);
                cmodel.getColumn(1).setCellEditor(textEditor);
                cmodel.getColumn(2).setCellEditor(textEditor);
                cmodel.getColumn(4).setCellEditor(textEditor);
                cmodel.getColumn(5).setCellEditor(textEditor);
                mergeTarget = new JButton("Merge target");
                mergeSource = new JButton("Merge source");
                export = new JButton("Export to File");
                newSegment = new JButton("Create New Segment");
                deleteSegment = new JButton("Delete Selected");
                moveUp = new JButton("Move Segment Up");
                moveDown = new JButton("Move Segment Down");
                lockSelected = new JButton("Lock Selected");
                unlockSelected = new JButton("Unlock Selected");
                reAlign = new JButton("Realign");
                reAlign.addActionListener(this);
                lockSelected.addActionListener(this);
                unlockSelected.addActionListener(this);
                mergeSource.addActionListener(this);
                mergeTarget.addActionListener(this);
                export.addActionListener(this);
                newSegment.addActionListener(this);
                deleteSegment.addActionListener(this);
                moveUp.addActionListener(this);
                moveDown.addActionListener(this);
                JPanel control = new JPanel();
                JPanel manipulate = new JPanel();
                control.add(moveUp);
                control.add(moveDown);
                control.add(mergeTarget);
                control.add(mergeSource);
                //control.add(export);
                control.add(newSegment);
                control.add(deleteSegment);
                manipulate.add(reAlign);
                manipulate.add(lockSelected);
                manipulate.add(unlockSelected);
                manipulate.add(export);
                edit.add(control, BorderLayout.PAGE_START);
                edit.add(manipulate, BorderLayout.PAGE_END);
                JScrollPane scr = new JScrollPane(table);
                //   JTable rowTable = new FirstRowNumberTable(table);

                //scr.add(table);
                //scr.setRowHeaderView(rowTable);
                //scr.setCorner(JScrollPane.UPPER_LEFT_CORNER, rowTable.getTableHeader()); 
                scr.repaint();
                edit.add(scr, BorderLayout.CENTER);
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                edit.setSize(screenSize.width - 4, screenSize.height - 50);
                int totwidth = screenSize.width - 50;
                table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
                cmodel.getColumn(0).setPreferredWidth((totwidth / 36) * 15);
                cmodel.getColumn(1).setPreferredWidth((totwidth / 36) * 15);
                cmodel.getColumn(2).setPreferredWidth(totwidth / 36 * 2);
                cmodel.getColumn(3).setPreferredWidth(totwidth / 36 * 2);
                cmodel.getColumn(4).setPreferredWidth(totwidth / 36 * 2);
                cmodel.getColumn(4).setPreferredWidth(totwidth / 36 * 2);
                edit.validate(); // Make sure layout is ok

                //edit.setSize(1024,700);
                edit.setVisible(true);

            } else {
                //log.setCaretPosition(log.getDocument().getLength());
                log.append("\nAutomatic alignment unsuccessful..check error logs");
            }
        } else {
            log.append("Please enter valid two letter language codes");
        }
        log.setCaretPosition(log.getDocument().getLength());

    }
}

From source file:org.dishevelled.brainstorm.BrainStorm.java

/** {@inheritDoc} */
public void run() {
    JFrame f = new JFrame("Brain storm");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add("Center", this);
    f.setResizable(false);/*from   ww  w.  j  a  v a 2  s  . co  m*/
    f.setUndecorated(true);
    // hide cursor on linux and windows platforms
    f.setCursor(hiddenCursor);
    GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(f);
    f.validate();

    SwingUtilities.invokeLater(new Runnable() {
        /** {@inheritDoc} */
        public void run() {
            calculatePlaceholderSize();
            calculateTextAreaSize();
            validate();
            textArea.scrollRectToVisible(
                    new Rectangle(0, textArea.getHeight() - 1, textArea.getWidth(), textArea.getHeight()));
        }
    });

    // save every five minutes
    Timer t = new Timer(5 * 60 * 1000, new ActionListener() {
        /** {@inheritDoc} */
        public void actionPerformed(final ActionEvent event) {
            save();
        }
    });
    t.setRepeats(true);
    t.start();
}