Example usage for java.awt BorderLayout NORTH

List of usage examples for java.awt BorderLayout NORTH

Introduction

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

Prototype

String NORTH

To view the source code for java.awt BorderLayout NORTH.

Click Source Link

Document

The north layout constraint (top of container).

Usage

From source file:KeyTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Key Listener");
    Container contentPane = frame.getContentPane();

    KeyListener listener = new KeyListener() {
        public void keyPressed(KeyEvent e) {
            dumpInfo("Pressed", e);
        }// ww w  .  ja va 2 s . c o m

        public void keyReleased(KeyEvent e) {
            dumpInfo("Released", e);
        }

        public void keyTyped(KeyEvent e) {
            dumpInfo("Typed", e);
        }

        private void dumpInfo(String s, KeyEvent e) {
            System.out.println(s);
            int code = e.getKeyCode();
            System.out.println("\tCode: " + KeyEvent.getKeyText(code));
            System.out.println("\tChar: " + e.getKeyChar());
            int mods = e.getModifiersEx();
            System.out.println("\tMods: " + KeyEvent.getModifiersExText(mods));
            System.out.println("\tLocation: " + location(e.getKeyLocation()));
            System.out.println("\tAction? " + e.isActionKey());
        }

        private String location(int location) {
            switch (location) {
            case KeyEvent.KEY_LOCATION_LEFT:
                return "Left";
            case KeyEvent.KEY_LOCATION_RIGHT:
                return "Right";
            case KeyEvent.KEY_LOCATION_NUMPAD:
                return "NumPad";
            case KeyEvent.KEY_LOCATION_STANDARD:
                return "Standard";
            case KeyEvent.KEY_LOCATION_UNKNOWN:
            default:
                return "Unknown";
            }
        }
    };

    JTextField text = new JTextField();
    text.addKeyListener(listener);
    contentPane.add(text, BorderLayout.NORTH);
    frame.pack();
    frame.show();
}

From source file:RegexTable.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Regexing JTable");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Object rows[][] = { { "A", "About", 44.36 }, { "B", "Boy", 44.84 }, { "C", "Cat", 463.63 },
            { "D", "Day", 27.14 }, { "E", "Eat", 44.57 }, { "F", "Fail", 23.15 }, { "G", "Good", 4.40 },
            { "H", "Hot", 24.96 }, { "I", "Ivey", 5.45 }, { "J", "Jack", 49.54 }, { "K", "Kids", 280.00 } };
    String columns[] = { "Symbol", "Name", "Price" };
    TableModel model = new DefaultTableModel(rows, columns) {
        public Class getColumnClass(int column) {
            Class returnValue;//from  w w  w. j av  a  2  s. c o m
            if ((column >= 0) && (column < getColumnCount())) {
                returnValue = getValueAt(0, column).getClass();
            } else {
                returnValue = Object.class;
            }
            return returnValue;
        }
    };

    final JTable table = new JTable(model);
    final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
    table.setRowSorter(sorter);
    JScrollPane pane = new JScrollPane(table);
    frame.add(pane, BorderLayout.CENTER);

    JPanel panel = new JPanel(new BorderLayout());
    JLabel label = new JLabel("Filter");
    panel.add(label, BorderLayout.WEST);
    final JTextField filterText = new JTextField("A");
    panel.add(filterText, BorderLayout.CENTER);
    frame.add(panel, BorderLayout.NORTH);
    JButton button = new JButton("Filter");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String text = filterText.getText();
            if (text.length() == 0) {
                sorter.setRowFilter(null);
            } else {
                sorter.setRowFilter(RowFilter.regexFilter(text));
            }
        }
    });
    frame.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 250);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    final JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();
    JButton b = new JButton("Hide for 5");
    ActionListener action = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            frame.setVisible(false);/*from w w w . java2s . c  o  m*/
            TimerTask task = new TimerTask() {
                public void run() {
                    frame.setVisible(true);
                }
            };
            Timer timer = new Timer();
            timer.schedule(task, 5000);
        }
    };
    b.addActionListener(action);
    AncestorListener ancestor = new AncestorListener() {
        public void ancestorAdded(AncestorEvent e) {
            System.out.println("Added");
            dumpInfo(e);
        }

        public void ancestorMoved(AncestorEvent e) {
            System.out.println("Moved");
            dumpInfo(e);
        }

        public void ancestorRemoved(AncestorEvent e) {
            System.out.println("Removed");
            dumpInfo(e);
        }

        private void dumpInfo(AncestorEvent e) {
            System.out.println("\tAncestor: " + name(e.getAncestor()));
            System.out.println("\tAncestorParent: " + name(e.getAncestorParent()));
            System.out.println("\tComponent: " + name(e.getComponent()));
        }

        private String name(Container c) {
            return (c == null) ? null : c.getName();
        }
    };
    b.addAncestorListener(ancestor);
    contentPane.add(b, BorderLayout.NORTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:PropertyChangeListenerDemo.java

public static void main(String args[]) {
    Runnable runner = new Runnable() {
        public void run() {
            JFrame frame = new JFrame("Button Sample");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            final JButton button1 = new JButton("Select Me");
            final JButton button2 = new JButton("No Select Me");
            final Random random = new Random();
            // Define ActionListener
            ActionListener actionListener = new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    JButton button = (JButton) actionEvent.getSource();
                    int red = random.nextInt(255);
                    int green = random.nextInt(255);
                    int blue = random.nextInt(255);
                    button.setBackground(new Color(red, green, blue));
                }//w  w  w. j  a  v  a2  s. c o m
            };
            // Define PropertyChangeListener
            PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
                    String property = propertyChangeEvent.getPropertyName();
                    if ("background".equals(property)) {
                        button2.setBackground((Color) propertyChangeEvent.getNewValue());
                    }
                }
            };
            button1.addActionListener(actionListener);
            button1.addPropertyChangeListener(propertyChangeListener);
            button2.addActionListener(actionListener);
            frame.add(button1, BorderLayout.NORTH);
            frame.add(button2, BorderLayout.SOUTH);
            frame.setSize(300, 100);
            frame.setVisible(true);
        }
    };
    EventQueue.invokeLater(runner);
}

From source file:ComplexRenderingSample.java

public static void main(String args[]) {

    Object elements[][] = {/*from ww  w  .j  av a 2 s. com*/
            { new Font("Helvetica", Font.PLAIN, 20), Color.red, new DiamondIcon(Color.blue), "Help" },
            { new Font("TimesRoman", Font.BOLD, 14), Color.blue, new DiamondIcon(Color.green), "Me" },
            { new Font("Courier", Font.ITALIC, 18), Color.green, new DiamondIcon(Color.black), "I'm" },
            { new Font("Helvetica", Font.BOLD | Font.ITALIC, 12), Color.gray, new DiamondIcon(Color.magenta),
                    "Trapped" },
            { new Font("TimesRoman", Font.PLAIN, 32), Color.pink, new DiamondIcon(Color.yellow), "Inside" },
            { new Font("Courier", Font.BOLD, 16), Color.yellow, new DiamondIcon(Color.red), "This" },
            { new Font("Helvetica", Font.ITALIC, 8), Color.darkGray, new DiamondIcon(Color.pink),
                    "Computer" } };

    JFrame frame = new JFrame("Complex Renderer");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    JList jlist = new JList(elements);
    ListCellRenderer renderer = new ComplexCellRenderer();
    jlist.setCellRenderer(renderer);
    JScrollPane scrollPane = new JScrollPane(jlist);
    contentPane.add(scrollPane, BorderLayout.CENTER);

    JComboBox comboBox = new JComboBox(elements);
    comboBox.setRenderer(renderer);
    contentPane.add(comboBox, BorderLayout.NORTH);

    frame.setSize(300, 300);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Action Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final Action printAction = new PrintHelloAction();

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("File");
    menuBar.add(menu);/*from  ww w. j  av a  2 s .c  om*/
    menu.add(new JMenuItem(printAction));

    JToolBar toolbar = new JToolBar();
    toolbar.add(new JButton(printAction));

    JButton enableButton = new JButton("Enable");
    ActionListener enableActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            printAction.setEnabled(true);
        }
    };
    enableButton.addActionListener(enableActionListener);

    JButton disableButton = new JButton("Disable");
    ActionListener disableActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            printAction.setEnabled(false);
        }
    };
    disableButton.addActionListener(disableActionListener);

    JButton relabelButton = new JButton("Relabel");
    ActionListener relabelActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            printAction.putValue(Action.NAME, "Hello, World");
        }
    };
    relabelButton.addActionListener(relabelActionListener);

    JPanel buttonPanel = new JPanel();
    buttonPanel.add(enableButton);
    buttonPanel.add(disableButton);
    buttonPanel.add(relabelButton);

    frame.setJMenuBar(menuBar);

    frame.add(toolbar, BorderLayout.SOUTH);
    frame.add(buttonPanel, BorderLayout.NORTH);
    frame.setSize(300, 200);
    frame.setVisible(true);

}

From source file:Bouncer.java

public static void main(String[] args) {
    final Bouncer bouncer = new Bouncer();
    Frame f = new AnimationFrame(bouncer);
    f.setFont(new Font("Serif", Font.PLAIN, 12));
    f.setSize(200, 200);//  w ww  .j a  v  a  2s  .  c  o  m
    Panel controls = new Panel();
    controls.add(bouncer.createCheckbox("Anti.", Bouncer.ANTIALIASING));
    controls.add(bouncer.createCheckbox("Trans.", Bouncer.TRANSFORM));
    controls.add(bouncer.createCheckbox("Gradient", Bouncer.GRADIENT));
    controls.add(bouncer.createCheckbox("Outline", Bouncer.OUTLINE));
    controls.add(bouncer.createCheckbox("Dotted", Bouncer.DOTTED));
    controls.add(bouncer.createCheckbox("Axes", Bouncer.AXES));
    controls.add(bouncer.createCheckbox("Clip", Bouncer.CLIP));
    f.add(controls, BorderLayout.NORTH);

    f.setVisible(true);
}

From source file:ResizeTable.java

public static void main(String args[]) {

    Object rowData[][] = { { "1", "one", "ichi - \u4E00", "un", "I" },
            { "2", "two", "ni - \u4E8C", "deux", "II" }, { "3", "three", "san - \u4E09", "trois", "III" },
            { "4", "four", "shi - \u56DB", "quatre", "IV" }, { "5", "five", "go - \u4E94", "cinq", "V" },
            { "6", "six", "roku - \u516D", "treiza", "VI" }, { "7", "seven", "shichi - \u4E03", "sept", "VII" },
            { "8", "eight", "hachi - \u516B", "huit", "VIII" }, { "9", "nine", "kyu - \u4E5D", "neur", "IX" },
            { "10", "ten", "ju - \u5341", "dix", "X" } };

    String columnNames[] = { "#", "English", "Japanese", "French", "Roman" };

    final JTable table = new JTable(rowData, columnNames);
    JScrollPane scrollPane = new JScrollPane(table);

    String modes[] = { "Resize All Columns", "Resize Last Column", "Resize Next Column", "Resize Off",
            "Resize Subsequent Columns" };
    final int modeKey[] = { JTable.AUTO_RESIZE_ALL_COLUMNS, JTable.AUTO_RESIZE_LAST_COLUMN,
            JTable.AUTO_RESIZE_NEXT_COLUMN, JTable.AUTO_RESIZE_OFF, JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS };
    JComboBox resizeModeComboBox = new JComboBox(modes);
    int defaultMode = 4;
    table.setAutoResizeMode(modeKey[defaultMode]);
    resizeModeComboBox.setSelectedIndex(defaultMode);
    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            JComboBox source = (JComboBox) e.getSource();
            int index = source.getSelectedIndex();
            table.setAutoResizeMode(modeKey[index]);
        }/*from   w w  w.  jav a2  s.  co  m*/
    };
    resizeModeComboBox.addItemListener(itemListener);

    JFrame frame = new JFrame("Resizing Table");
    Container contentPane = frame.getContentPane();

    contentPane.add(resizeModeComboBox, BorderLayout.NORTH);
    contentPane.add(scrollPane, BorderLayout.CENTER);

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

From source file:MainClass.java

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

    JToggleButton toggleButton = new JToggleButton("Selected");

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
            boolean selected = abstractButton.getModel().isSelected();
            System.out.println("Action - selected=" + selected + "\n");
        }//from ww w .j  a  va2 s.co  m
    };

    ChangeListener changeListener = new ChangeListener() {
        public void stateChanged(ChangeEvent changeEvent) {
            AbstractButton abstractButton = (AbstractButton) changeEvent.getSource();
            ButtonModel buttonModel = abstractButton.getModel();
            boolean armed = buttonModel.isArmed();
            boolean pressed = buttonModel.isPressed();
            boolean selected = buttonModel.isSelected();
            System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
        }
    };

    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent itemEvent) {
            int state = itemEvent.getStateChange();
            if (state == ItemEvent.SELECTED) {
                System.out.println("Selected");
            } else {
                System.out.println("Deselected");
            }
        }
    };

    toggleButton.addActionListener(actionListener);
    toggleButton.addChangeListener(changeListener);
    toggleButton.addItemListener(itemListener);
    frame.add(toggleButton, BorderLayout.NORTH);

    JToggleButton toggleButton2 = new JToggleButton("Focused");
    frame.add(toggleButton2, BorderLayout.CENTER);

    JToggleButton toggleButton3 = new JToggleButton("Not Selected");
    frame.add(toggleButton3, BorderLayout.SOUTH);

    frame.setSize(300, 125);
    frame.setVisible(true);
}

From source file:Main.java

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

    JPanel gui = new JPanel(new BorderLayout(5, 5));

    JPanel plafComponents = new JPanel(new FlowLayout(FlowLayout.RIGHT, 3, 3));
    plafComponents.setBorder(new TitledBorder("FlowLayout(FlowLayout.RIGHT, 3,3)"));

    UIManager.LookAndFeelInfo[] plafInfos = UIManager.getInstalledLookAndFeels();
    String[] plafNames = new String[plafInfos.length];
    for (int ii = 0; ii < plafInfos.length; ii++) {
        plafNames[ii] = plafInfos[ii].getName();
    }/*from   w  w  w .  java 2  s.c o  m*/
    JComboBox plafChooser = new JComboBox(plafNames);
    plafComponents.add(plafChooser);

    JCheckBox pack = new JCheckBox("Pack on PLAF change", true);
    plafComponents.add(pack);

    plafChooser.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            int index = plafChooser.getSelectedIndex();
            try {
                UIManager.setLookAndFeel(plafInfos[index].getClassName());
                SwingUtilities.updateComponentTreeUI(frame);
                if (pack.isSelected()) {
                    frame.pack();
                    frame.setMinimumSize(frame.getSize());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    gui.add(plafComponents, BorderLayout.NORTH);

    JPanel dynamicLabels = new JPanel(new BorderLayout(4, 4));
    dynamicLabels.setBorder(new TitledBorder("BorderLayout(4,4)"));
    gui.add(dynamicLabels, BorderLayout.WEST);

    final JPanel labels = new JPanel(new GridLayout(0, 2, 3, 3));
    labels.setBorder(new TitledBorder("GridLayout(0,2,3,3)"));

    JButton addNew = new JButton("Add Another Label");
    dynamicLabels.add(addNew, BorderLayout.NORTH);
    addNew.addActionListener(new ActionListener() {

        private int labelCount = 0;

        public void actionPerformed(ActionEvent ae) {
            labels.add(new JLabel("Label " + ++labelCount));
            frame.validate();
        }
    });

    dynamicLabels.add(new JScrollPane(labels), BorderLayout.CENTER);

    String[] header = { "Name", "Value" };
    String[] a = new String[0];
    String[] names = System.getProperties().stringPropertyNames().toArray(a);
    String[][] data = new String[names.length][2];
    for (int ii = 0; ii < names.length; ii++) {
        data[ii][0] = names[ii];
        data[ii][1] = System.getProperty(names[ii]);
    }
    DefaultTableModel model = new DefaultTableModel(data, header);
    JTable table = new JTable(model);

    JScrollPane tableScroll = new JScrollPane(table);
    Dimension tablePreferred = tableScroll.getPreferredSize();
    tableScroll.setPreferredSize(new Dimension(tablePreferred.width, tablePreferred.height / 3));

    JPanel imagePanel = new JPanel(new GridBagLayout());
    JLabel imageLabel = new JLabel("test");
    imagePanel.add(imageLabel, null);

    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, tableScroll, new JScrollPane(imagePanel));
    gui.add(splitPane, BorderLayout.CENTER);

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