Example usage for javax.swing JPanel getComponentCount

List of usage examples for javax.swing JPanel getComponentCount

Introduction

In this page you can find the example usage for javax.swing JPanel getComponentCount.

Prototype

public int getComponentCount() 

Source Link

Document

Gets the number of components in this panel.

Usage

From source file:Main.java

public static void main(String[] args) {
    JPanel panel = new JPanel(new GridBagLayout());
    for (int i = 0; i < 25; i++) {
        JTextField field = new JTextField("Field " + i, 20);
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridy = i;//from   ww  w  .j av  a  2 s.c o m
        panel.add(field, constraints);
    }
    JScrollPane scrollPane = new JScrollPane(panel);
    JButton removeButton = new JButton("Remove Field");
    removeButton.addActionListener(e -> {
        if (panel.getComponentCount() >= 1) {
            panel.remove(panel.getComponentCount() - 1);
            scrollPane.revalidate();
            scrollPane.repaint();
        }
    });
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(640, 480);
    f.setLocation(200, 200);
    f.getContentPane().add(scrollPane);
    f.getContentPane().add(removeButton, BorderLayout.SOUTH);
    f.setVisible(true);
}

From source file:Main.java

public static void setJPanelOpaque(JPanel p, boolean opaque) {
    p.setOpaque(opaque);/*from  ww w .  ja v a  2s .  c o  m*/
    for (int iComp = 0; iComp < p.getComponentCount(); iComp++) {
        if (p.getComponent(iComp) instanceof JPanel) {
            setJPanelOpaque((JPanel) p.getComponent(iComp), opaque);
        }
    }
}

From source file:Main.java

/**
 * Sets the JButtons inside a JPanelto be the same size.
 * This is done dynamically by setting each button's preferred and maximum
 * sizes after the buttons are created. This way, the layout automatically
 * adjusts to the locale-specific strings.
 *
 * @param jPanelButtons JPanel containing buttons
 *///from ww  w .  j  a  v a  2  s .  c  o m
public static void equalizeButtonSizes(JPanel jPanelButtons) {
    ArrayList<JButton> lbuttons = new ArrayList<JButton>();
    for (int i = 0; i < jPanelButtons.getComponentCount(); i++) {
        Component c = jPanelButtons.getComponent(i);
        if (c instanceof JButton) {
            lbuttons.add((JButton) c);
        }
    }

    // Get the largest width and height
    Dimension maxSize = new Dimension(0, 0);
    for (JButton lbutton : lbuttons) {
        Dimension d = lbutton.getPreferredSize();
        maxSize.width = Math.max(maxSize.width, d.width);
        maxSize.height = Math.max(maxSize.height, d.height);
    }

    for (JButton btn : lbuttons) {
        btn.setPreferredSize(maxSize);
        btn.setMinimumSize(maxSize);
        btn.setMaximumSize(maxSize);
    }
}

From source file:Main.java

/**
 * Adds a one pixel border of random color to this and all panels contained in this panel's
 * child hierarchy./* w  ww. j  a  va  2  s  .c o m*/
 */
public static void addDebugBorders(JPanel panel) {
    Color bcolor = new Color(_rando.nextInt(256), _rando.nextInt(256), _rando.nextInt(256));
    panel.setBorder(BorderFactory.createLineBorder(bcolor));

    for (int ii = 0; ii < panel.getComponentCount(); ii++) {
        Object child = panel.getComponent(ii);
        if (child instanceof JPanel) {
            addDebugBorders((JPanel) child);
        }
    }
}

From source file:tourma.utils.web.WebStatistics.java

/**
 * //from w  ww . j  ava  2 s . c o m
 */
public static String getHTML() {
    StringBuffer stats = new StringBuffer("");

    JPNStatistics jpn = new JPNStatistics();
    jpn.setSize(640, 480);
    JTabbedPane jtp = jpn.getTabbedPane();
    for (int i = 0; i < jtp.getTabCount(); i++) {
        Component comp = jtp.getComponent(i);

        if (comp instanceof ChartPanel) {
            ChartPanel panel = (ChartPanel) comp;
            panel.setSize(640, 480);
            BufferedImage buf = new BufferedImage(640, 480, BufferedImage.TYPE_INT_ARGB);
            Graphics g = buf.createGraphics();
            panel.print(g);
            g.dispose();

            //BufferedImage buf = toBufferedImage(img, 640, 480);
            String img_str = WebPicture.getPictureAsHTML(buf, 640, 480, true);
            stats.append(img_str);
        }

        if (comp instanceof JPanel) {
            // Find JList, Select All then Find ChartPanel
            JPanel pane = (JPanel) comp;
            ChartPanel panel = null;
            JList list = null;
            for (int j = 0; j < pane.getComponentCount(); j++) {
                Component c = pane.getComponent(j);
                if (c instanceof JScrollPane) {
                    for (int k = 0; k < ((JScrollPane) c).getViewport().getComponentCount(); k++) {
                        Component c2 = ((JScrollPane) c).getViewport().getComponent(k);
                        if (c2 instanceof JList) {
                            list = (JList) c2;
                        }

                    }
                }
            }

            if (list != null) {

                int start = 0;
                int end = list.getModel().getSize() - 1;
                if (end >= 0) {
                    list.setSelectionInterval(start, end);
                }

                jpn.updatePositions();
            }
            for (int j = 0; j < pane.getComponentCount(); j++) {
                Component c = pane.getComponent(j);
                if (c instanceof ChartPanel) {
                    panel = (ChartPanel) c;
                }
            }

            if (panel != null) {
                panel.setSize(640, 480);
                BufferedImage buf = new BufferedImage(640, 480, BufferedImage.TYPE_INT_ARGB);
                Graphics g = buf.createGraphics();
                panel.print(g);
                g.dispose();

                //BufferedImage buf = toBufferedImage(img, 640, 480);
                String img_str = WebPicture.getPictureAsHTML(buf, 640, 480, true);
                stats.append(img_str);
            }
        }
    }

    return stats.toString();
}

From source file:fr.isen.browser5.Util.UrlLoader.java

public void movePanel() {
    JPanel panel = elementVisitor.getPanel();
    int count = panel.getComponentCount();
    for (int i = 0; i < count; i++) {
        tabView.add(panel.getComponent(0));
    }/*from w w w. ja  va2  s .  c  om*/
}

From source file:de.tud.kom.p2psim.impl.skynet.visualization.SkyNetVisualization.java

private ChartPanel getChartPanel(int i, int j) {
    JPanel chartContainer = (JPanel) ((JPanel) graphix.getComponent(i)).getComponent(j);
    int count = 0;
    while (count < chartContainer.getComponentCount()) {
        if (chartContainer.getComponent(count) instanceof ChartPanel) {
            return (ChartPanel) chartContainer.getComponent(count);
        } else {//from   www . j av a  2s . c  om
            count++;
        }
    }
    return null;
}

From source file:jamel.gui.JamelWindow.java

/**
 * Exports a rapport.//from  w  w w . j a v  a 2s .  c om
 */
public void exportRapport() {
    final int max = tabbedPane.getTabCount();
    final ProgressMonitor progressMonitor = new ProgressMonitor(this, "Exporting", "", 0, max);
    progressMonitor.setMillisToDecideToPopup(0);
    final String rc = System.getProperty("line.separator");
    final File outputDirectory = new File("exports/" + this.getTitle() + "-" + (new Date()).getTime());
    outputDirectory.mkdir();
    try {
        final FileWriter writer = new FileWriter(new File(outputDirectory, "Rapport.html"));
        writer.write("<HTML>" + rc);
        writer.write("<HEAD>");
        writer.write("<TITLE>" + this.getTitle() + "</TITLE>" + rc);
        writer.write("</HEAD>" + rc);
        writer.write("<BODY>" + rc);
        writer.write("<H1>" + this.getTitle() + "</H1>" + rc);
        writer.write("<HR>" + rc);
        final Date start = viewManager.getStart().getDate();
        final Date end = viewManager.getEnd().getDate();
        for (int tabIndex = 0; tabIndex < max; tabIndex++) {
            try {
                final JPanel currentTab = (JPanel) tabbedPane.getComponentAt(tabIndex);
                final String tabTitle = tabbedPane.getTitleAt(tabIndex);
                writer.write("<H2>" + tabTitle + "</H2>" + rc);
                writer.write("<TABLE>" + rc);
                final int chartPanelCount = currentTab.getComponentCount();
                for (int chartIndex = 0; chartIndex < chartPanelCount; chartIndex++) {
                    if ((chartIndex == 3) | (chartIndex == 6))
                        writer.write("<TR>" + rc);
                    final ChartPanel aChartPanel = (ChartPanel) currentTab.getComponent(chartIndex);
                    final JamelChart chart = (JamelChart) aChartPanel.getChart();
                    if (chart != null) {
                        final String chartTitle = chart.getTitle().getText();
                        if (!chartTitle.equals("Empty")) {
                            try {
                                chart.setTitle("");
                                chart.setTimeRange(start, end);
                                String imageName = (tabTitle + "-" + chartIndex + "-" + chartTitle + ".png")
                                        .replace(" ", "_").replace("&", "and");
                                ChartUtilities.saveChartAsPNG(new File(outputDirectory, imageName), chart,
                                        aChartPanel.getWidth(), aChartPanel.getHeight());
                                writer.write("<TD><IMG src=\"" + imageName + "\" title=\"" + chartTitle + "\">"
                                        + rc);
                                chart.setTitle(chartTitle);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
                writer.write("</TABLE>" + rc);
                writer.write("<HR>" + rc);
            } catch (ClassCastException e) {
            }
            progressMonitor.setProgress(tabIndex);
        }
        writer.write("<H2>Scenario</H2>" + rc);
        writer.write(this.consoleText.toString());
        writer.write("</BODY>" + rc);
        writer.write("</HTML>" + rc);
        writer.close();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    progressMonitor.close();
}

From source file:game.Clue.ClueGameUI.java

public void packageGameState() {
    System.out.println("Number of pieces on gameboard: " + gameBoard.getComponentCount());
    for (int i = 1; i < gameBoard.getComponentCount(); i++) {
        JPanel room = (JPanel) gameBoard.getComponent(i);
        if (room.getComponentCount() != 0) {
            System.out.println(room.getComponent(0));
        }//from   w ww . j  av  a2s  . com

    }

}

From source file:ffx.ui.ModelingPanel.java

/**
 * Create a string representing the modeling command to execute.
 *
 * @return the modeling command string.//from   w  w w .  jav a 2  s .  c o  m
 */
private String createCommandInput() {
    StringBuilder commandLineParams = new StringBuilder(activeCommand + " ");
    // Now append command line input to a TextArea, one option per line.
    // This TextArea gets dumped to an input file.
    commandTextArea.setText("");
    int numparams = optionsTabbedPane.getTabCount();
    for (int i = 0; i < numparams; i++) {
        // A few cases require that a newLine not be generated between
        // options.
        boolean newLine = true;
        // The optionString will collect the parameters for this Option,
        // then append them to the CommandTextArea.
        StringBuilder optionString = new StringBuilder();
        JPanel optionPanel = (JPanel) optionsTabbedPane.getComponentAt(i);
        int numOptions = optionPanel.getComponentCount();
        String title = optionsTabbedPane.getTitleAt(i);
        if (title.equalsIgnoreCase("Sequence")) {
            for (int k = 0; k < acidComboBox.getItemCount(); k++) {
                if (k != 0) {
                    optionString.append("\n");
                }
                String s = (String) acidComboBox.getItemAt(k);
                s = s.substring(s.indexOf(" "), s.length()).trim();
                optionString.append(s);
            }
            // Need an extra newline for Nucleic
            if (activeCommand.equalsIgnoreCase("NUCLEIC")) {
                optionString.append("\n");
            }
        } else {
            JPanel valuePanel = (JPanel) optionPanel.getComponent(numOptions - 1);
            int numValues = valuePanel.getComponentCount();
            for (int j = 0; j < numValues; j++) {
                Component value = valuePanel.getComponent(j);
                if (value instanceof JCheckBox) {
                    JCheckBox jcbox = (JCheckBox) value;
                    if (jcbox.isSelected()) {
                        optionString.append("-");
                        optionString.append(jcbox.getName());
                        optionString.append(" ");
                        optionString.append(jcbox.getText());
                    }
                } else if (value instanceof JTextField) {
                    JTextField jtfield = (JTextField) value;
                    optionString.append("-");
                    optionString.append(jtfield.getName());
                    optionString.append(" ");
                    optionString.append(jtfield.getText());
                } else if (value instanceof JComboBox) {
                    JComboBox jcb = (JComboBox) value;
                    Object object = jcb.getSelectedItem();
                    if (object instanceof FFXSystem) {
                        FFXSystem system = (FFXSystem) object;
                        File file = system.getFile();
                        if (file != null) {
                            String absolutePath = file.getAbsolutePath();
                            if (absolutePath.endsWith("xyz")) {
                                absolutePath = absolutePath + "_1";
                            }
                            optionString.append(absolutePath);
                        }
                    }
                } else if (value instanceof JRadioButton) {
                    JRadioButton jrbutton = (JRadioButton) value;
                    if (jrbutton.isSelected()) {
                        if (!jrbutton.getText().equalsIgnoreCase("NONE")) {
                            optionString.append("-");
                            optionString.append(jrbutton.getName());
                            optionString.append(" ");
                            optionString.append(jrbutton.getText());
                        }
                        if (title.equalsIgnoreCase("C-CAP")) {
                            optionString.append("\n");
                        }
                    }
                }
            }
            // Handle Conditional Options
            if (optionPanel.getComponentCount() == 3) {
                valuePanel = (JPanel) optionPanel.getComponent(1);
                // JLabel conditionalLabel = (JLabel)
                // valuePanel.getComponent(0);
                JTextField jtf = (JTextField) valuePanel.getComponent(1);
                if (jtf.isEnabled()) {
                    String conditionalInput = jtf.getText();
                    // Post-Process the Input into Atom Pairs
                    String postProcess = jtf.getName();
                    if (postProcess != null && postProcess.equalsIgnoreCase("ATOMPAIRS")) {
                        String tokens[] = conditionalInput.split(" +");
                        StringBuilder atomPairs = new StringBuilder();
                        int atomNumber = 0;
                        for (String token : tokens) {
                            atomPairs.append(token);
                            if (atomNumber++ % 2 == 0) {
                                atomPairs.append(" ");
                            } else {
                                atomPairs.append("\n");
                            }
                        }
                        conditionalInput = atomPairs.toString();
                    }
                    // Append a newline to "enter" the option string.
                    // Append "conditional" input.
                    optionString.append("\n").append(conditionalInput);
                }
            }
        }
        if (optionString.length() > 0) {
            commandTextArea.append(optionString.toString());
            if (newLine) {
                commandTextArea.append("\n");
            }
        }
    }
    String commandInput = commandTextArea.getText();
    if (commandInput != null && !commandInput.trim().equalsIgnoreCase("")) {
        commandLineParams.append(commandInput);
    }

    // The final token on the command line is the structure file name, except
    // for protein and nucleic.
    if (!activeCommand.equalsIgnoreCase("Protein") && !activeCommand.equalsIgnoreCase("Nucleic")) {
        File file = activeSystem.getFile();
        if (file != null) {
            String name = file.getName();
            commandLineParams.append(name);
            commandLineParams.append(" ");
        } else {
            return null;
        }
    }

    return commandLineParams.toString();
}