Example usage for javax.swing JCheckBox setSelected

List of usage examples for javax.swing JCheckBox setSelected

Introduction

In this page you can find the example usage for javax.swing JCheckBox setSelected.

Prototype

public void setSelected(boolean b) 

Source Link

Document

Sets the state of the button.

Usage

From source file:Main.java

/**
 * Makes a new CheckBox with given properties.
 * /*from  w w w  . j a v  a2  s .c o  m*/
 * @param name
 *            The label of the control.
 * @param command
 *            The name the action listener is looking for.
 * @param isSelected
 *            The condition on which this control is selected.
 * @param listener
 *            The action listener.
 * @return A new CheckBox with proper default properties.
 */
public static JCheckBox createCheckBox(String name, String command, boolean isSelected,
        ActionListener listener) {
    JCheckBox button;
    button = new JCheckBox(name);
    button.setActionCommand(command);
    button.setSelected(isSelected);
    button.addActionListener(listener);
    return button;
}

From source file:components.GlassPaneDemo.java

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.//from  w ww  .  j a v  a  2 s.c  o m
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("GlassPaneDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Start creating and adding components.
    JCheckBox changeButton = new JCheckBox("Glass pane \"visible\"");
    changeButton.setSelected(false);

    //Set up the content pane, where the "main GUI" lives.
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new FlowLayout());
    contentPane.add(changeButton);
    contentPane.add(new JButton("Button 1"));
    contentPane.add(new JButton("Button 2"));

    //Set up the menu bar, which appears above the content pane.
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Menu");
    menu.add(new JMenuItem("Do nothing"));
    menuBar.add(menu);
    frame.setJMenuBar(menuBar);

    //Set up the glass pane, which appears over both menu bar
    //and content pane and is an item listener on the change
    //button.
    myGlassPane = new MyGlassPane(changeButton, menuBar, frame.getContentPane());
    changeButton.addItemListener(myGlassPane);
    frame.setGlassPane(myGlassPane);

    //Show the window.
    frame.pack();
    frame.setVisible(true);
}

From source file:GlassPaneDemo.java

/**
 * Create the GUI and show it. For thread safety, this method should be
 * invoked from the event-dispatching thread.
 *//* w  w w  .ja  v  a2 s. c o  m*/
private static void createAndShowGUI() {
    // Create and set up the window.
    JFrame frame = new JFrame("GlassPaneDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Start creating and adding components.
    JCheckBox changeButton = new JCheckBox("Glass pane \"visible\"");
    changeButton.setSelected(false);

    // Set up the content pane, where the "main GUI" lives.
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new FlowLayout());
    contentPane.add(changeButton);
    contentPane.add(new JButton("Button 1"));
    contentPane.add(new JButton("Button 2"));

    // Set up the menu bar, which appears above the content pane.
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Menu");
    menu.add(new JMenuItem("Do nothing"));
    menuBar.add(menu);
    frame.setJMenuBar(menuBar);

    // Set up the glass pane, which appears over both menu bar
    // and content pane and is an item listener on the change
    // button.
    myGlassPane = new MyGlassPane(changeButton, menuBar, frame.getContentPane());
    changeButton.addItemListener(myGlassPane);
    frame.setGlassPane(myGlassPane);

    // Show the window.
    frame.pack();
    frame.setVisible(true);
}

From source file:de.mycrobase.jcloudapp.Main.java

private static Map<String, String> showLoginDialog() {
    String message = ""; //"Welcome to JCloudApp!";

    JTextField usernameField = new JTextField();
    JPasswordField passwordField = new JPasswordField();
    JCheckBox remeberCheck = new JCheckBox("Remember login (on disk)");
    remeberCheck.setSelected(true);
    Object[] content = { message, "Username:", usernameField, "Password:", passwordField, remeberCheck };

    //        int res = JOptionPane.showOptionDialog(
    //            null, content, "JCloudApp - Login",
    //            JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, icon,
    //            null, usernameField);
    int res = JOptionPane.showConfirmDialog(null, content, "JCloudApp - Login", JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE, IconLarge);

    if (res == JOptionPane.OK_OPTION) {
        HashMap<String, String> m = new HashMap<String, String>();
        m.put("username", usernameField.getText());
        m.put("password", new String(passwordField.getPassword()));
        m.put("remember", Boolean.toString(remeberCheck.isSelected()));
        return m;
    } else {//from   w w  w . j  a v a  2  s  .  c o  m
        return null;
    }
}

From source file:CheckBoxState.java

public CheckBoxState() {
    setSize(300, 300);//  w w  w .j  av  a2  s  .  com
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout(FlowLayout.LEFT));

    JCheckBox checkBox = new JCheckBox("Check me!");
    checkBox.setSelected(true);

    boolean selected = checkBox.isSelected();
    if (selected) {
        System.out.println("Check box state is selected.");
    } else {
        System.out.println("Check box state is not selected.");
    }

    getContentPane().add(checkBox);
}

From source file:Main.java

public Main() {
    setSize(300, 300);//from  w  ww  .j a va 2  s .c om
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout(FlowLayout.LEFT));

    JCheckBox checkBox = new JCheckBox("Check me!");
    checkBox.setSelected(true);

    boolean selected = checkBox.isSelected();
    if (selected) {
        System.out.println("Check box state is selected.");
    } else {
        System.out.println("Check box state is not selected.");
    }

    getContentPane().add(checkBox);
}

From source file:JCheckBoxCustomIcon.java

public JCheckBoxCustomIcon() {
    setSize(300, 300);/*from   w  ww  .j  av a 2s. c om*/
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout(FlowLayout.LEFT));

    JCheckBox checkBox = new JCheckBox("Check me!");
    checkBox.setSelected(true);

    // Set default icon for checkbox
    checkBox.setIcon(new ImageIcon("icon.png"));
    // Set selected icon when checkbox state is selected
    checkBox.setSelectedIcon(new ImageIcon("selectedIcon.png"));
    // Set disabled icon for checkbox
    checkBox.setDisabledIcon(new ImageIcon("disabledIcon.png"));
    // Set disabled-selected icon for checkbox
    checkBox.setDisabledSelectedIcon(new ImageIcon("disabledSelectedIcon.png"));
    // Set checkbox icon when checkbox is pressed
    checkBox.setPressedIcon(new ImageIcon("pressedIcon.png"));
    // Set icon when a mouse is over the checkbox
    checkBox.setRolloverIcon(new ImageIcon("rolloverIcon.png"));
    // Set icon when a mouse is over a selected checkbox
    checkBox.setRolloverSelectedIcon(new ImageIcon("rolloverSelectedIcon.png"));

    getContentPane().add(checkBox);
}

From source file:Main.java

public Main() {
    setSize(300, 300);//from ww w  .j ava2  s.  c  om
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout(FlowLayout.LEFT));

    JCheckBox checkBox = new JCheckBox("Check me!");
    checkBox.setSelected(true);

    // Set default icon for checkbox
    checkBox.setIcon(new ImageIcon("icon.png"));
    // Set selected icon when checkbox state is selected
    checkBox.setSelectedIcon(new ImageIcon("selectedIcon.png"));
    // Set disabled icon for checkbox
    checkBox.setDisabledIcon(new ImageIcon("disabledIcon.png"));
    // Set disabled-selected icon for checkbox
    checkBox.setDisabledSelectedIcon(new ImageIcon("disabledSelectedIcon.png"));
    // Set checkbox icon when checkbox is pressed
    checkBox.setPressedIcon(new ImageIcon("pressedIcon.png"));
    // Set icon when a mouse is over the checkbox
    checkBox.setRolloverIcon(new ImageIcon("rolloverIcon.png"));
    // Set icon when a mouse is over a selected checkbox
    checkBox.setRolloverSelectedIcon(new ImageIcon("rolloverSelectedIcon.png"));

    getContentPane().add(checkBox);
}

From source file:com.diversityarrays.kdxplore.vistool.VisToolbarFactory.java

static public VisToolToolBar create(final String title, final JComponent comp, final Closure<File> snapshotter,
        final VisToolDataProvider visToolDataProvider, boolean floatable, final String[] imageSuffixes) {
    Window window = GuiUtil.getOwnerWindow(comp);

    boolean anyButtons = false;

    final JCheckBox keepOnTop;

    if (window == null) {
        keepOnTop = null;/*from  w  ww. j a  va  2 s. c o  m*/
    } else {
        anyButtons = true;
        keepOnTop = new JCheckBox(Msg.OPTION_KEEP_ON_TOP(), true);

        keepOnTop.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                window.setAlwaysOnTop(keepOnTop.isSelected());
            }
        });
        window.setAlwaysOnTop(keepOnTop.isSelected());

        //         buttons.add(keepOnTop);

        final PropertyChangeListener alwaysOnTopListener = new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                keepOnTop.setSelected(window.isAlwaysOnTop());
            }
        };
        window.addPropertyChangeListener(PROPERTY_ALWAYS_ON_TOP, alwaysOnTopListener);

        window.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                window.removeWindowListener(this);
                window.removePropertyChangeListener(PROPERTY_ALWAYS_ON_TOP, alwaysOnTopListener);
            }
        });
    }

    final JButton cameraButton;
    if (snapshotter == null) {
        cameraButton = null;
    } else {
        Action cameraAction = new AbstractAction(Msg.ACTION_SNAPSHOT()) {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (chooser == null) {
                    chooser = new JFileChooser();
                    chooser.setFileFilter(new FileFilter() {
                        @Override
                        public boolean accept(File f) {
                            if (!f.isFile()) {
                                return true;
                            }
                            String loname = f.getName().toLowerCase();
                            for (String sfx : imageSuffixes) {
                                if (loname.endsWith(sfx)) {
                                    return true;
                                }
                            }
                            return false;
                        }

                        @Override
                        public String getDescription() {
                            return Msg.DESC_IMAGE_FILE();
                        }
                    });
                    chooser.setMultiSelectionEnabled(false);
                    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                }

                if (JFileChooser.APPROVE_OPTION == chooser.showSaveDialog(comp)) {
                    File file = chooser.getSelectedFile();
                    snapshotter.execute(file);
                }
            }
        };

        ImageIcon icon = loadIcon("camera-24.png"); //$NON-NLS-1$
        if (icon != null) {
            cameraAction.putValue(Action.SMALL_ICON, icon);
            cameraAction.putValue(Action.NAME, null);
        }

        anyButtons = true;
        cameraButton = new JButton(cameraAction);
    }

    final JButton refreshButton;
    if (visToolDataProvider == null) {
        refreshButton = null;
    } else {
        anyButtons = true;

        refreshButton = new JButton(Msg.ACTION_REFRESH());

        ImageIcon icon = loadIcon("refresh-24.png"); //$NON-NLS-1$
        if (icon != null) {
            refreshButton.setIcon(icon);
            // don't remove the name
        }

        refreshButton.setForeground(Color.RED);
        refreshButton.setEnabled(false);

        refreshButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (visToolDataProvider.refreshData()) {
                    refreshButton.setEnabled(false);
                }
            }
        });

        visToolDataProvider.addVisToolDataChangedListener(new VisToolDataChangedListener() {
            @Override
            public void visToolDataChanged(Object source) {
                refreshButton.setEnabled(true);
            }
        });
    }

    VisToolToolBar toolBar = null;

    if (anyButtons) {
        toolBar = new VisToolToolBar(keepOnTop, cameraButton, refreshButton);
        toolBar.setFloatable(floatable);
    }
    return toolBar;

}

From source file:layout.BoxLayoutDemo2.java

public void populateContentPane(Container contentPane) {
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));

    //Create the rectangles.
    int shortSideSize = 15;
    for (int i = 0; i < NUM_COMPONENTS; i++) {
        if (sizeIsRandom) {
            shortSideSize = (int) (30.0 * Math.random()) + 30;
        } else {//ww  w.  j  av  a  2  s  . com
            shortSideSize += 10;
        }
        bldComponent[i] = new BLDComponent(xAlignment[i], hue[i], shortSideSize, restrictSize, sizeIsRandom,
                String.valueOf(i));
        panel.add(bldComponent[i]);
    }

    //Create the instructions.
    JLabel label = new JLabel("Click a rectangle to " + "change its X alignment.");
    JCheckBox cb = new JCheckBox("Restrict maximum rectangle size.");
    cb.setSelected(restrictSize);
    cb.addItemListener(this);

    panel.setBorder(BorderFactory.createLineBorder(Color.red));

    Box box = Box.createVerticalBox();
    box.add(label);
    box.add(cb);

    contentPane.add(panel, BorderLayout.CENTER);
    contentPane.add(box, BorderLayout.PAGE_END);
}