Example usage for javax.swing JPanel getComponent

List of usage examples for javax.swing JPanel getComponent

Introduction

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

Prototype

public Component getComponent(int n) 

Source Link

Document

Gets the nth component in this container.

Usage

From source file:Main.java

public static void setJPanelOpaque(JPanel p, boolean opaque) {
    p.setOpaque(opaque);/*from w ww  .  j a  va2 s .  co 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
 *//*  w  ww .  j  a  v  a2s  .co 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  w  w .j  a  v  a 2s. c om*/
 */
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   ww  w.ja  va2s  .co  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:Main.java

void addTab() {
    JEditorPane ep = new JEditorPane();
    ep.setEditable(false);/* w  w  w .  jav  a  2  s. c  om*/
    tp.addTab(null, new JScrollPane(ep));

    JButton tabCloseButton = new JButton("Close");
    tabCloseButton.setActionCommand("" + tabCounter);

    ActionListener al;
    al = new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JButton btn = (JButton) ae.getSource();
            String s1 = btn.getActionCommand();
            for (int i = 1; i < tp.getTabCount(); i++) {
                JPanel pnl = (JPanel) tp.getTabComponentAt(i);
                btn = (JButton) pnl.getComponent(0);
                String s2 = btn.getActionCommand();
                if (s1.equals(s2)) {
                    tp.removeTabAt(i);
                    break;
                }
            }
        }
    };
    tabCloseButton.addActionListener(al);

    if (tabCounter != 0) {
        JPanel pnl = new JPanel();
        pnl.setOpaque(false);
        pnl.add(tabCloseButton);
        tp.setTabComponentAt(tp.getTabCount() - 1, pnl);
        tp.setSelectedIndex(tp.getTabCount() - 1);
    }

    tabCounter++;
}

From source file:com.streamhub.StreamHubLicenseGenerator.java

private JPanel createActionsRow() {
    JPanel actionsRow = new JPanel(new FlowLayout());
    File baseFolder = new File(DEFAULT_CUSTOMERS_DIRECTORY);
    JButton generate = new JButton("Generate");
    generate.addActionListener(new ActionListener() {
        @Override//  ww w . j  a  v  a 2s. com
        public void actionPerformed(ActionEvent e) {
            for (JPanel row : macAddressPanels) {
                try {
                    String macAddress = ((JTextField) row.getComponent(1)).getText();
                    if (macAddress.length() > 0) {
                        String startDate = ((JTextField) row.getComponent(3)).getText();
                        String expiryDate = ((JTextField) row.getComponent(5)).getText();
                        String edition = ((JComboBox) row.getComponent(6)).getSelectedItem().toString();
                        macAddress = macAddress.replaceAll("-", ":").trim();
                        String name = ((JTextField) row.getComponent(8)).getText().trim();
                        String numUsers = ((JTextField) row.getComponent(10)).getText();
                        String licenseString = startDate + "-" + expiryDate + "-" + numUsers + "-" + macAddress
                                + "-" + edition + ":" + name;
                        String hashInput = licenseString + USELESS_KEY;
                        MessageDigest m = MessageDigest.getInstance("SHA-512");
                        m.update(hashInput.getBytes(), 0, hashInput.length());
                        String hash = "==" + new BigInteger(1, m.digest()).toString(16) + "==";
                        StringBuilder licenseText = new StringBuilder();
                        licenseText.append("--").append(licenseString).append("--");
                        licenseText.append(CRLF);
                        licenseText.append(hash);
                        licenseText.append(CRLF);
                        System.out.println(name + ":");
                        System.out.println();
                        System.out.println(licenseText);

                        File licenseDir = new File(folderChooser.getSelectedFile(), name);
                        if (!licenseDir.isDirectory() && !licenseDir.exists()) {
                            licenseDir.mkdir();
                        }
                        File licenseFile = new File(licenseDir, "license.txt");
                        System.out.println("writing to " + licenseFile.getAbsolutePath());
                        FileUtils.writeStringToFile(licenseFile, licenseText.toString());
                    }
                } catch (Exception exception) {
                    System.out.println("Could not generate license");
                    exception.printStackTrace();
                }
            }
        }
    });
    final JButton chooseFolder = new JButton("Choose Folder");
    final JTextField folderDisplay = new JTextField(baseFolder.getAbsolutePath());
    folderChooser = new JFileChooser(baseFolder);
    folderChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    folderChooser.setSelectedFile(baseFolder);
    chooseFolder.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == chooseFolder) {
                int returnVal = folderChooser.showOpenDialog(StreamHubLicenseGenerator.this);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File folder = folderChooser.getSelectedFile();
                    folderDisplay.setText(folder.getAbsolutePath());
                }
            }
        }
    });

    actionsRow.add(folderDisplay);
    actionsRow.add(chooseFolder);
    actionsRow.add(generate);
    return actionsRow;
}

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 ww. j  a  va2s  . c o m*/
}

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

public void setTabTitleFavicon() {
    String title = doc.title();/*  ww  w. j a v  a  2s. c o m*/
    title = Str.shortenString(title, 12);
    int tabIndex = tabView.getCurrentTabIndex();
    JPanel tabPanel = (JPanel) tabView.getCurrentFrame().getTabPane().getTabComponentAt(tabIndex);
    JLabel titleLabel = (JLabel) tabPanel.getComponent(0);
    titleLabel.setText(title);

    String baseUrl = pageUrl.getProtocol() + "://" + pageUrl.getHost();
    Element element = doc.head().select("link[href~=.*\\.(ico|png)]").first();
    String favicoUrlStr = "";
    if (element != null) {
        favicoUrlStr = element.attr("abs:href");
    } else {
        element = doc.head().select("meta[itemprop=image]").first();
        if (element != null) {
            favicoUrlStr = baseUrl + element.attr("content");
        }
    }

    ImageIcon icon = null;
    try {
        if (!favicoUrlStr.isEmpty()) {
            if (favicoUrlStr.endsWith(".ico")) {
                java.util.List<BufferedImage> imgs = ICODecoder.read(new URL(favicoUrlStr).openStream());
                icon = new ImageIcon(imgs.get(0));
            } else {
                icon = new ImageIcon(new URL(favicoUrlStr));
            }
            icon = new ImageIcon(icon.getImage().getScaledInstance(16, 16, Image.SCALE_DEFAULT));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    titleLabel.setIcon(icon);

    HistoryEntry historyEntry = new HistoryEntry(title, pageUrl.toString(), Instant.now().getEpochSecond(),
            icon, -1L);
    globalHistoryDAO.create(historyEntry);
}

From source file:com.game.ui.views.ItemPanel.java

public void persistTreasure(String name, JPanel panel) {
    String value = ((JTextField) panel.getComponent(4)).getText();
    if (StringUtils.isNotBlank(value) && StringUtils.isNotBlank(name)) {
        int position = GameUtils.getPositionOfTreasureItem(name);
        if (position != -1) {
            GameBean.treasureDetails.remove(position);
        }/*from www. j  a  v  a  2s .co m*/
        if (GameBean.treasureDetails == null) {
            GameBean.treasureDetails = new ArrayList<>();
        }
        Treasure treasure = new Treasure();
        treasure.setName(name);
        treasure.setValue(Integer.parseInt(value));
        GameBean.treasureDetails.add(treasure);
        try {
            GameUtils.writeItemsToXML(GameBean.treasureDetails, Configuration.PATH_FOR_TREASURES);
            validationMess.setText("Saved Successfully..");
            validationMess.setVisible(true);
            if (position == -1) {
                comboBox.removeActionListener(this);
                comboBox.addItem(name);
                comboBox.setSelectedItem(name);
                comboBox.addActionListener(this);
            }
            TileInformation tileInfo = GameBean.mapInfo.getPathMap().get(location);
            if (tileInfo == null) {
                tileInfo = new TileInformation();
            }
            tileInfo.setTreasure(treasure);
            GameBean.mapInfo.getPathMap().put(location, tileInfo);
            chkBox.setSelected(true);
        } catch (Exception e) {
            validationMess.setText("Some error occured...");
            e.printStackTrace();
            validationMess.setVisible(true);
        }
    } else {
        validationMess.setText("Pls enter all the fields or pls choose a weapon from the drop down");
        validationMess.setVisible(true);
    }
}

From source file:com.game.ui.views.ItemPanel.java

public void persistPotionData(String name, JPanel panel) {
    String potionPts = ((JTextField) panel.getComponent(4)).getText();
    if (StringUtils.isNotBlank(potionPts) && StringUtils.isNotBlank(name)) {
        int position = GameUtils.getPositionOfPotionItem(name);
        if (position != -1) {
            GameBean.potionDetails.remove(position);
        }//from  w  w  w .  j a v a  2  s .  c o m
        if (GameBean.potionDetails == null) {
            GameBean.potionDetails = new ArrayList<>();
        }
        Potion potion = new Potion();
        potion.setName(name);
        potion.setPotionPts(Integer.parseInt(potionPts));
        GameBean.potionDetails.add(potion);
        try {
            GameUtils.writeItemsToXML(GameBean.potionDetails, Configuration.PATH_FOR_POTIONS);
            validationMess.setText("Saved Successfully..");
            validationMess.setVisible(true);
            if (position == -1) {
                comboBox.removeActionListener(this);
                comboBox.addItem(name);
                comboBox.setSelectedItem(name);
                comboBox.addActionListener(this);
            }
            TileInformation tileInfo = GameBean.mapInfo.getPathMap().get(location);
            if (tileInfo == null) {
                tileInfo = new TileInformation();
            }
            tileInfo.setPotion(potion);
            GameBean.mapInfo.getPathMap().put(location, tileInfo);
            chkBox.setSelected(true);
        } catch (Exception e) {
            validationMess.setText("Some error occured...");
            e.printStackTrace();
            validationMess.setVisible(true);
        }
    } else {
        validationMess.setText("Pls enter all the fields or pls choose a weapon from the drop down");
        validationMess.setVisible(true);
    }
}