Example usage for java.awt FlowLayout FlowLayout

List of usage examples for java.awt FlowLayout FlowLayout

Introduction

In this page you can find the example usage for java.awt FlowLayout FlowLayout.

Prototype

public FlowLayout() 

Source Link

Document

Constructs a new FlowLayout with a centered alignment and a default 5-unit horizontal and vertical gap.

Usage

From source file:Main.java

public static void main(String args[]) {
    String[] mainData = { "-Select-", "Sel 1", "Sel 2", "Sel 3" };
    String[] subData1 = { "Sub Sel 11", "Sub Sel 12", "Sub Sel 13" };
    String[] subData2 = { "Sub Sel 21", "Sub Sel 22", "Sub Sel 23" };
    String[] subData3 = { "Sub Sel 31", "Sub Sel 32", "Sub Sel 33" };
    DefaultComboBoxModel boxModel = new DefaultComboBoxModel();
    JComboBox box1 = new JComboBox(mainData), box2 = new JComboBox();
    JFrame frame = new JFrame();
    frame.setLayout(new FlowLayout());
    frame.add(box1);/*from ww  w  .  ja  v  a  2s . com*/
    frame.add(box2);
    box2.setVisible(false);
    box2.setModel(boxModel);
    frame.setBounds(200, 200, 500, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    box1.addItemListener(e -> {
        box2.setVisible(true);
        boxModel.removeAllElements();
        if (box1.getSelectedIndex() == 0) {
            box2.setVisible(false);
        } else if (box1.getSelectedIndex() == 1) {
            for (String s : subData1) {
                boxModel.addElement(s);
            }
        } else if (box1.getSelectedIndex() == 2) {
            for (String s : subData2) {
                boxModel.addElement(s);
            }
        } else if (box1.getSelectedIndex() == 3) {
            for (String s : subData3) {
                boxModel.addElement(s);
            }
        }
    });
}

From source file:TestOval.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel label1 = new JLabel(new OvalIcon(20, 50));
    JLabel label2 = new JLabel(new OvalIcon(50, 20));
    JLabel label3 = new JLabel("Round!", new OvalIcon(60, 60), SwingConstants.CENTER);
    label3.setHorizontalTextPosition(SwingConstants.CENTER);

    Container c = f.getContentPane();
    c.setLayout(new FlowLayout());
    c.add(label1);// ww w .  j  ava2  s. c  o m
    c.add(label2);
    c.add(label3);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 400);/* w ww. j a  v  a 2s. c  om*/

    JDialog dialog = new JDialog(frame, "Dialog", true);

    JPanel mainGui = new JPanel(new BorderLayout());
    mainGui.setBorder(new EmptyBorder(20, 20, 20, 20));
    mainGui.add(new JLabel("Contents go here"), BorderLayout.CENTER);

    JPanel buttonPanel = new JPanel(new FlowLayout());
    mainGui.add(buttonPanel, BorderLayout.SOUTH);

    JButton close = new JButton("Close");
    close.addActionListener(e -> dialog.setVisible(false));

    buttonPanel.add(close);

    frame.setVisible(true);

    dialog.setContentPane(mainGui);
    dialog.pack();
    dialog.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {

    JRadioButton button1 = new JRadioButton("Red");
    JRadioButton button2 = new JRadioButton("Green");

    button2.setMnemonic(KeyEvent.VK_G);

    JRadioButton button3 = new JRadioButton("Blue");
    ButtonGroup colorButtonGroup = new ButtonGroup();
    colorButtonGroup.add(button1);/*from   ww  w .ja  v  a  2s . c o  m*/
    colorButtonGroup.add(button2);
    colorButtonGroup.add(button3);
    button1.setSelected(true);

    JFrame frame = new JFrame();
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(new JLabel("Color:"));
    frame.add(button1);
    frame.add(button2);
    frame.add(button3);
    frame.pack();
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    JComboBox c = new JComboBox();
    c.addPopupMenuListener(new PopupMenuListener() {

        @Override/*  w  w w  .  ja v  a  2s .  c  o  m*/
        public void popupMenuCanceled(PopupMenuEvent e) {
            System.out.println(e.getSource());
        }

        @Override
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            System.out.println(e.getSource());
        }

        @Override
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            System.out.println(e.getSource());
        }
    });
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().setLayout(new FlowLayout());
    f.getContentPane().add(c);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String[] items = new String[] { "", "Apple", "Banana", "Carrot" };
    Color bgColor = UIManager.getColor("TextField.background");

    UIManager.put("ComboBox.selectionBackground", new ColorUIResource(bgColor));

    JComboBox<String> combo1 = new JComboBox<>(items);
    combo1.setPrototypeDisplayValue("XXXXXXXXXXXXXXX");
    combo1.setEditable(true);//from  w w w. ja  v  a  2 s . c  o m
    combo1.setSelectedIndex(-1);

    JComboBox<String> combo2 = new JComboBox<>(items);
    combo2.setPrototypeDisplayValue("XXXXXXXXXXXXXXX");
    combo2.setEditable(false);
    combo2.setSelectedIndex(-1);
    combo2.setBackground(bgColor);

    JFrame frame = new JFrame();
    Container c = frame.getContentPane();
    c.setLayout(new FlowLayout());
    c.add(combo1);
    c.add(combo2);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 100);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:FancyButton.java

public static void main(String[] args) {

    FancyButton b1 = new FancyButton(new ImageIcon("1.gif"), new ImageIcon("2.gif"), new ImageIcon("3.gif"));
    FancyButton b2 = new FancyButton(new ImageIcon("4.gif"), new ImageIcon("1.gif"), new ImageIcon("2.gif"));
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container c = f.getContentPane();
    c.setLayout(new FlowLayout());
    c.add(b1);/*from w  ww  .j  a  v a2 s. c  o m*/
    c.add(b2);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    Object[] items = new Object[] { "Dog", "Cat", "Other" };
    DefaultComboBoxModel dcbm = new DefaultComboBoxModel(items);
    JComboBox comboBox = new JComboBox(dcbm);
    comboBox.setPreferredSize(new Dimension(200, 20));
    comboBox.addItemListener(e -> {/*  w  ww.j  av  a 2 s  .co  m*/
        Object selectedItem = comboBox.getSelectedItem();
        boolean editable = selectedItem instanceof String && ((String) selectedItem).equals("Other");
        comboBox.setEditable(editable);
    });
    comboBox.getEditor().addActionListener(e -> {
        Object newItem = comboBox.getEditor().getItem();
        DefaultComboBoxModel d = (DefaultComboBoxModel) comboBox.getModel();
        d.addElement(newItem);
        d.setSelectedItem(newItem);

    });

    JPanel content = new JPanel(new FlowLayout());
    content.add(new JLabel("Test:"));
    content.add(comboBox);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(content);
    frame.pack();
    frame.setVisible(true);
}

From source file:JToggleButtonEvents.java

public static void main(String[] args) {
    JToggleButton jtb = new JToggleButton("Press Me");

    jtb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            System.out.println("ActionEvent!");
        }//from w  w w  . jav  a2 s.c  om
    });
    jtb.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent ev) {
            System.out.println("ItemEvent!");
        }
    });
    jtb.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent ev) {
            System.out.println("ChangeEvent!");
        }
    });
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container c = f.getContentPane();
    c.setLayout(new FlowLayout());
    c.add(jtb);
    f.pack();
    f.setVisible(true);
}

From source file:YAxisAlignX.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Alignment Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container panel1 = layoutComponents("Left", Component.LEFT_ALIGNMENT);
    Container panel2 = layoutComponents("Center", Component.CENTER_ALIGNMENT);
    Container panel3 = layoutComponents("Right", Component.RIGHT_ALIGNMENT);
    frame.setLayout(new FlowLayout());
    frame.add(panel1);//from  w  w  w.j  a  v a2  s  . c  om
    frame.add(panel2);
    frame.add(panel3);

    frame.pack();
    frame.setVisible(true);
}