Example usage for javax.swing JPanel getComponents

List of usage examples for javax.swing JPanel getComponents

Introduction

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

Prototype

public Component[] getComponents() 

Source Link

Document

Gets all the components in this container.

Usage

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();

    JPanel entreePanel = new JPanel();
    final ButtonGroup entreeGroup = new ButtonGroup();
    JRadioButton radioButton;//from   w ww .jav a2s  .  c  om
    entreePanel.add(radioButton = new JRadioButton("A"));
    radioButton.setActionCommand("A");
    entreeGroup.add(radioButton);
    entreePanel.add(radioButton = new JRadioButton("B"));
    radioButton.setActionCommand("B");
    entreeGroup.add(radioButton);
    entreePanel.add(radioButton = new JRadioButton("C", true));
    radioButton.setActionCommand("C");
    entreeGroup.add(radioButton);

    final JPanel condimentsPanel = new JPanel();
    condimentsPanel.add(new JCheckBox("Ketchup"));
    condimentsPanel.add(new JCheckBox("Mustard"));
    condimentsPanel.add(new JCheckBox("Pickles"));

    JPanel orderPanel = new JPanel();
    JButton orderButton = new JButton("Place Order");
    orderPanel.add(orderButton);

    frame.setLayout(new GridLayout(3, 1));
    frame.add(entreePanel);
    frame.add(condimentsPanel);
    frame.add(orderPanel);

    orderButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            String entree = entreeGroup.getSelection().getActionCommand();
            System.out.println(entree + " sandwich");
            Component[] components = condimentsPanel.getComponents();
            for (Component c : components) {
                JCheckBox cb = (JCheckBox) c;
                if (cb.isSelected())
                    System.out.println("With " + cb.getText());
            }
        }
    });

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:Main.java

public static void disableChildren(JPanel panel, Class<?>... classesToIgnore) {
    for (Component component : panel.getComponents()) {
        if (component instanceof JPanel) {
            disableChildren((JPanel) component, classesToIgnore);
        } else {/*from  w w w .j  a va  2 s .co m*/
            if (!ignoreClasses(component, classesToIgnore)) {
                component.setEnabled(false);
            }
        }
    }
}

From source file:Main.java

public static void enableChildren(JPanel panel, Class<?>... classesToIgnore) {
    for (Component component : panel.getComponents()) {
        if (component instanceof JPanel) {
            enableChildren((JPanel) component, classesToIgnore);
        } else {/*from  w  ww.ja va 2s  .c  om*/
            if (!ignoreClasses(component, classesToIgnore)) {
                component.setEnabled(true);
            }
        }
    }
}

From source file:Main.java

public static void roChildren(JPanel panel, Class<?>... classesToIgnore) {
    for (Component component : panel.getComponents()) {
        if (component instanceof JPanel) {
            roChildren((JPanel) component, classesToIgnore);
        } else {// w w w. j av a  2 s  . c  o  m
            if (!ignoreClasses(component, classesToIgnore)) {
                Method m = discover(component, "setEditable", boolean.class);
                invoke(m, component, new Object[] { Boolean.FALSE });
            }
        }
    }
}

From source file:Main.java

public static void rwChildren(JPanel panel, Class<?>... classesToIgnore) {
    for (Component component : panel.getComponents()) {
        if (component instanceof JPanel) {
            rwChildren((JPanel) component, classesToIgnore);
        } else {/*from  www  . jav  a2 s. co  m*/
            if (!ignoreClasses(component, classesToIgnore)) {
                Method m = discover(component, "setEditable", boolean.class);
                invoke(m, component, new Object[] { Boolean.TRUE });
            }
        }
    }
}

From source file:Main.java

public Main() {
    JPanel panel = new JPanel();
    BoxLayout boxLayout = new BoxLayout(panel, BoxLayout.PAGE_AXIS);
    panel.setLayout(boxLayout);//from w w  w  . j a  va  2 s.  c o  m
    for (int i = 0; i < 40; i++) {
        panel.add(new JButton("Button " + i));
    }
    buttons = panel.getComponents();
    activeComponent = buttons[index];
    final JScrollPane scroll = new JScrollPane(panel);

    Timer timer = new Timer(500, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ((JButton) activeComponent).setForeground(Color.BLACK);
            if (index >= buttons.length - 1) {
                index = 0;
            } else {
                index++;
            }
            activeComponent = buttons[index];
            ((JButton) activeComponent).setForeground(Color.red);
            setView(scroll, activeComponent);
            System.out.println(((JButton) activeComponent).getActionCommand());
        }
    });
    timer.start();

    scroll.setPreferredSize(new Dimension(200, 300));
    JFrame frame = new JFrame();
    frame.add(scroll);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

From source file:com.sldeditor.test.unit.tool.mapbox.MapBoxToolTest.java

/**
 * Test method for//from   ww w  .j  av  a  2  s  .co m
 * {@link com.sldeditor.tool.mapbox.MapBoxTool#setSelectedItems(java.util.List, java.util.List)}.
 */
@Test
public void testSetSelectedItems() {
    MapBoxTool tool = new MapBoxTool();

    JPanel panel = tool.getPanel();

    ToolButton toSLD = null;

    for (Component c : panel.getComponents()) {
        if (c instanceof ToolButton) {
            ToolButton button = (ToolButton) c;
            String toolTipText = button.getToolTipText();
            if (toolTipText
                    .compareTo(Localisation.getString(MapBoxTool.class, "MapBoxTool.exportToSLD")) == 0) {
                toSLD = button;
            }
        }
    }

    File testFile1 = null;
    File testFile3 = null;
    try {
        testFile1 = File.createTempFile("invalid", ".tst");
        testFile3 = File.createTempFile("valid", ".json");
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    // Should both be disabled
    assertFalse(toSLD.isEnabled());

    tool.setSelectedItems(null, null);

    // Invalid file
    List<SLDDataInterface> sldDataList = new ArrayList<SLDDataInterface>();
    SLDData sldData1 = new SLDData(null, null);
    sldData1.setSLDFile(testFile1);
    sldDataList.add(sldData1);
    tool.setSelectedItems(null, sldDataList);

    // Should both be disabled
    assertFalse(toSLD.isEnabled());

    /*
     * // Try with valid mapbox file sldDataList = new ArrayList<SLDDataInterface>(); SLDData
     * sldData3 = getSLDDataFile("/point/mapbox/circleStyleTest.json");
     * sldDataList.add(sldData3); tool.setSelectedItems(null, sldDataList);
     * 
     * // SLD should be enabled assertTrue(toSLD.isEnabled()); toSLD.doClick();
     * 
     * // Try with valid sld files sldDataList = new ArrayList<SLDDataInterface>();
     * sldDataList.add(sldData3); tool.setSelectedItems(null, sldDataList);
     * 
     * // SLD should be enabled assertTrue(toSLD.isEnabled());
     */

    testFile1.delete();
    testFile3.delete();

    // tidyUpTempFiles(sldData3.getSLDFile());
}

From source file:com.floreantpos.config.ui.TerminalConfigurationView.java

public void restartPOS() {
    JOptionPane optionPane = new JOptionPane(Messages.getString("TerminalConfigurationView.26"), //$NON-NLS-1$
            JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, Application.getApplicationIcon(),
            new String[] { /*Messages.getString("TerminalConfigurationView.28"),*/Messages
                    .getString("TerminalConfigurationView.30") }); //$NON-NLS-1$ //$NON-NLS-2$

    Object[] optionValues = optionPane.getComponents();
    for (Object object : optionValues) {
        if (object instanceof JPanel) {
            JPanel panel = (JPanel) object;
            Component[] components = panel.getComponents();

            for (Component component : components) {
                if (component instanceof JButton) {
                    component.setPreferredSize(new Dimension(100, 80));
                    JButton button = (JButton) component;
                    button.setPreferredSize(PosUIManager.getSize(100, 50));
                }/* w w  w .j av a 2s.  c o  m*/
            }
        }
    }
    JDialog dialog = optionPane.createDialog(Application.getPosWindow(),
            Messages.getString("TerminalConfigurationView.31")); //$NON-NLS-1$
    dialog.setIconImage(Application.getApplicationIcon().getImage());
    dialog.setLocationRelativeTo(Application.getPosWindow());
    dialog.setVisible(true);
    Object selectedValue = (String) optionPane.getValue();
    if (selectedValue != null) {

        if (selectedValue.equals(Messages.getString("TerminalConfigurationView.28"))) { //$NON-NLS-1$
            try {
                Main.restart();
            } catch (IOException | InterruptedException | URISyntaxException e) {
            }
        } else {
        }
    }

}

From source file:com.aw.swing.mvp.view.IPView.java

/**
 * Get the components of the viewSrc//from ww w . ja  va  2 s  .  c o  m
 *
 * @return
 */
public Component[] getJComponents() {
    if (viewSrc instanceof JDialog) {
        JDialog dlg = (JDialog) viewSrc;
        return dlg.getContentPane().getComponents();
    } else {
        JPanel pnl = (JPanel) AttributeAccessor.get(viewSrc, "pnlMain");
        return pnl.getComponents();
    }
}

From source file:com.sldeditor.test.unit.tool.ysld.YSLDToolTest.java

/**
 * Test sld file./*ww  w. j  a  v a2s  .  c om*/
 */
@Test
public void testSetSelectedItems() {
    YSLDTool tool = new YSLDTool();

    JPanel panel = tool.getPanel();

    ToolButton toSLD = null;
    ToolButton toYSLD = null;

    for (Component c : panel.getComponents()) {
        if (c instanceof ToolButton) {
            ToolButton button = (ToolButton) c;
            String toolTipText = button.getToolTipText();
            if (toolTipText.compareTo(Localisation.getString(YSLDTool.class, "YSLDTool.exportToSLD")) == 0) {
                toSLD = button;
            } else if (toolTipText
                    .compareTo(Localisation.getString(YSLDTool.class, "YSLDTool.exportToYSLD")) == 0) {
                toYSLD = button;
            }
        }
    }

    File testFile1 = null;
    File testFile3 = null;
    try {
        testFile1 = File.createTempFile("invalid", ".tst");
        testFile1.deleteOnExit();
        testFile3 = File.createTempFile("valid", ".ysld");
        testFile3.deleteOnExit();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    // Should both be disabled
    assertFalse(toSLD.isEnabled());
    assertFalse(toYSLD.isEnabled());

    tool.setSelectedItems(null, null);

    // Invalid file
    List<SLDDataInterface> sldDataList = new ArrayList<SLDDataInterface>();
    SLDData sldData1 = new SLDData(null, null);
    sldData1.setSLDFile(testFile1);
    sldDataList.add(sldData1);
    tool.setSelectedItems(null, sldDataList);

    // Should both be disabled
    assertFalse(toSLD.isEnabled());
    assertFalse(toYSLD.isEnabled());

    // Try with valid sld file
    sldDataList = new ArrayList<SLDDataInterface>();
    SLDData sldData2 = getSLDDataFile("/point/sld/point_simplepoint.sld");
    sldDataList.add(sldData2);
    tool.setSelectedItems(null, sldDataList);

    // YSLD should be enabled
    assertTrue(toYSLD.isEnabled());
    assertFalse(toSLD.isEnabled());

    toYSLD.doClick();

    // Try with valid ysld file
    sldDataList = new ArrayList<SLDDataInterface>();
    SLDData sldData3 = getSLDDataFile("/point/ysld/point_simplepoint.ysld");
    sldDataList.add(sldData3);
    tool.setSelectedItems(null, sldDataList);

    // SLD should be enabled
    assertTrue(toSLD.isEnabled());
    assertFalse(toYSLD.isEnabled());
    toSLD.doClick();

    // Try with valid sld and ysld files
    sldDataList = new ArrayList<SLDDataInterface>();
    sldDataList.add(sldData2);
    sldDataList.add(sldData3);
    tool.setSelectedItems(null, sldDataList);

    // SLD and YSLD should be enabled
    assertTrue(toSLD.isEnabled());
    assertTrue(toYSLD.isEnabled());
}