List of usage examples for java.awt.event ItemListener ItemListener
ItemListener
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 ww w . ja va 2 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:Main.java
public static void main(String args[]) { String ITEMS[] = { "Black", "Blue", "Green", "Orange", "Purple", "Red", "White" }; JList jList = new JList(ITEMS); jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); JScrollPane scroll = new JScrollPane(jList); scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); JCheckBox chkEnable = new JCheckBox("Enable", true); chkEnable.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { jList.setEnabled(chkEnable.isSelected()); }//from w w w . j a va 2 s . c o m }); JFrame f = new JFrame("Colors"); Container contentPane = f.getContentPane(); contentPane.setLayout(new BorderLayout()); contentPane.add(scroll, BorderLayout.CENTER); contentPane.add(chkEnable, BorderLayout.NORTH); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(180, 220); f.setLocationRelativeTo(null); f.setVisible(true); }
From source file:ResizeTable.java
public static void main(String args[]) { final Object rowData[][] = { { "1", "one", "I" }, { "2", "two", "II" }, { "3", "three", "III" } }; final String columnNames[] = { "#", "English", "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); ItemListener itemListener = new ItemListener() { public void itemStateChanged(ItemEvent e) { JComboBox source = (JComboBox) e.getSource(); int index = source.getSelectedIndex(); table.setAutoResizeMode(modeKey[index]); }//w w w . j a v a 2 s. c om }; resizeModeComboBox.addItemListener(itemListener); JFrame frame = new JFrame("Resizing Table"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(resizeModeComboBox, BorderLayout.NORTH); frame.add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); }
From source file:Main.java
public static void main(final String args[]) { JFrame frame = new JFrame("MenuSample Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMenuBar menuBar = new JMenuBar(); // File Menu, F - Mnemonic JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic(KeyEvent.VK_F); menuBar.add(fileMenu);//from w ww . j a v a2 s . com // File->New, N - Mnemonic JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N); fileMenu.add(newMenuItem); JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Case Sensitive"); caseMenuItem.setMnemonic(KeyEvent.VK_C); fileMenu.add(caseMenuItem); ItemListener iListener = new ItemListener() { public void itemStateChanged(ItemEvent event) { Icon girlIcon = new ImageIcon("g.jpg"); Icon boyIcon = new ImageIcon("b.jpg"); AbstractButton aButton = (AbstractButton) event.getSource(); int state = event.getStateChange(); String newLabel; Icon newIcon; if (state == ItemEvent.SELECTED) { newLabel = "Girl"; newIcon = girlIcon; } else { newLabel = "Boy"; newIcon = boyIcon; } aButton.setText(newLabel); aButton.setIcon(newIcon); } }; caseMenuItem.addItemListener(iListener); frame.setJMenuBar(menuBar); frame.setSize(350, 250); frame.setVisible(true); }
From source file:JCheckBoxMenuItemActionListener.java
public static void main(final String args[]) { JFrame frame = new JFrame("MenuSample Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMenuBar menuBar = new JMenuBar(); // File Menu, F - Mnemonic JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic(KeyEvent.VK_F); menuBar.add(fileMenu);/*w ww . j ava 2s . c o m*/ // File->New, N - Mnemonic JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N); fileMenu.add(newMenuItem); JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Case Sensitive"); caseMenuItem.setMnemonic(KeyEvent.VK_C); fileMenu.add(caseMenuItem); ItemListener iListener = new ItemListener() { public void itemStateChanged(ItemEvent event) { Icon girlIcon = new ImageIcon("girl-r.jpg"); Icon boyIcon = new ImageIcon("boy-r.jpg"); AbstractButton aButton = (AbstractButton) event.getSource(); int state = event.getStateChange(); String newLabel; Icon newIcon; if (state == ItemEvent.SELECTED) { newLabel = "Girl"; newIcon = girlIcon; } else { newLabel = "Boy"; newIcon = boyIcon; } aButton.setText(newLabel); aButton.setIcon(newIcon); } }; caseMenuItem.addItemListener(iListener); frame.setJMenuBar(menuBar); frame.setSize(350, 250); frame.setVisible(true); }
From source file:MainClass.java
public static void main(String args[]) { String rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } }; String headers[] = { "Upper", "Lower" }; 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 }; final JTable table = new JTable(rows, headers); JScrollPane scrollPane = new JScrollPane(table); String modes[] = { "Resize All Columns", "Resize Last Column", "Resize Next Column", "Resize Off", "Resize Subsequent Columns" }; final JComboBox resizeModeComboBox = new JComboBox(modes); int defaultMode = 4; table.setAutoResizeMode(modeKey[defaultMode]); resizeModeComboBox.setSelectedIndex(defaultMode); ItemListener itemListener = new ItemListener() { public void itemStateChanged(ItemEvent e) { int index = resizeModeComboBox.getSelectedIndex(); table.setAutoResizeMode(modeKey[index]); }//from w w w .ja v a2 s . c om }; resizeModeComboBox.addItemListener(itemListener); JFrame frame = new JFrame("Resizing Table"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(resizeModeComboBox, BorderLayout.NORTH); frame.add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); }
From source file:RadioButtonSample.java
public static void main(String args[]) { ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { AbstractButton aButton = (AbstractButton) actionEvent.getSource(); boolean selected = aButton.getModel().isSelected(); System.out.println(actionEvent.getActionCommand() + " - selected? " + selected); }/*www .ja va2 s.c om*/ }; ItemListener itemListener = new ItemListener() { public void itemStateChanged(ItemEvent itemEvent) { AbstractButton aButton = (AbstractButton) itemEvent.getSource(); int state = itemEvent.getStateChange(); String selected = ((state == ItemEvent.SELECTED) ? "selected" : "not selected"); System.out.println(aButton.getText() + " - selected? " + selected); } }; JFrame frame = new JFrame("Radio Menu Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("Menu"); ButtonGroup buttonGroup = new ButtonGroup(); menu.setMnemonic(KeyEvent.VK_M); JRadioButtonMenuItem emptyMenuItem = new JRadioButtonMenuItem(); emptyMenuItem.setActionCommand("Empty"); emptyMenuItem.addActionListener(actionListener); buttonGroup.add(emptyMenuItem); menu.add(emptyMenuItem); JRadioButtonMenuItem oneMenuItem = new JRadioButtonMenuItem("Partridge"); oneMenuItem.addActionListener(actionListener); buttonGroup.add(oneMenuItem); menu.add(oneMenuItem); JRadioButtonMenuItem twoMenuItem = new JRadioButtonMenuItem("Turtle Doves", true); twoMenuItem.addActionListener(actionListener); buttonGroup.add(twoMenuItem); menu.add(twoMenuItem); JRadioButtonMenuItem threeMenuItem = new JRadioButtonMenuItem("French Hens", threeIcon); threeMenuItem.addItemListener(itemListener); buttonGroup.add(threeMenuItem); menu.add(threeMenuItem); JRadioButtonMenuItem fourMenuItem = new JRadioButtonMenuItem("Calling Birds", fourIcon, true); fourMenuItem.addActionListener(actionListener); buttonGroup.add(fourMenuItem); menu.add(fourMenuItem); JRadioButtonMenuItem fiveMenuItem = new JRadioButtonMenuItem(fiveIcon); fiveMenuItem.addActionListener(actionListener); fiveMenuItem.setActionCommand("Rings"); buttonGroup.add(fiveMenuItem); menu.add(fiveMenuItem); JRadioButtonMenuItem sixMenuItem = new JRadioButtonMenuItem(sixIcon, true); sixMenuItem.addActionListener(actionListener); sixMenuItem.setActionCommand("Geese"); buttonGroup.add(sixMenuItem); menu.add(sixMenuItem); menuBar.add(menu); frame.setJMenuBar(menuBar); frame.setSize(350, 250); frame.setVisible(true); }
From source file:SelectingComboSample.java
public static void main(String args[]) { String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "J", "I" }; JFrame frame = new JFrame("Selecting JComboBox"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); JComboBox comboBox = new JComboBox(labels); contentPane.add(comboBox, BorderLayout.SOUTH); final JTextArea textArea = new JTextArea(); textArea.setEditable(false);//from w ww . ja v a2 s . c o m JScrollPane sp = new JScrollPane(textArea); contentPane.add(sp, BorderLayout.CENTER); ItemListener itemListener = new ItemListener() { public void itemStateChanged(ItemEvent itemEvent) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); int state = itemEvent.getStateChange(); String stateString = ((state == ItemEvent.SELECTED) ? "Selected" : "Deselected"); pw.print("Item: " + itemEvent.getItem()); pw.print(", State: " + stateString); ItemSelectable is = itemEvent.getItemSelectable(); pw.print(", Selected: " + selectedString(is)); pw.println(); textArea.append(sw.toString()); } }; comboBox.addItemListener(itemListener); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); pw.print("Command: " + actionEvent.getActionCommand()); ItemSelectable is = (ItemSelectable) actionEvent.getSource(); pw.print(", Selected: " + selectedString(is)); pw.println(); textArea.append(sw.toString()); } }; comboBox.addActionListener(actionListener); frame.setSize(400, 200); frame.setVisible(true); }
From source file:CheckBoxSample.java
public static void main(String args[]) { ActionListener aListener = new ActionListener() { public void actionPerformed(ActionEvent event) { AbstractButton aButton = (AbstractButton) event.getSource(); boolean selected = aButton.getModel().isSelected(); String newLabel;/*from ww w . jav a 2 s . c o m*/ Icon newIcon; if (selected) { newLabel = "Girl"; newIcon = girlIcon; } else { newLabel = "Boy"; newIcon = boyIcon; } aButton.setText(newLabel); aButton.setIcon(newIcon); } }; ItemListener iListener = new ItemListener() { public void itemStateChanged(ItemEvent event) { AbstractButton aButton = (AbstractButton) event.getSource(); int state = event.getStateChange(); String newLabel; Icon newIcon; if (state == ItemEvent.SELECTED) { newLabel = "Girl"; newIcon = girlIcon; } else { newLabel = "Boy"; newIcon = boyIcon; } aButton.setText(newLabel); aButton.setIcon(newIcon); } }; JFrame frame = new JFrame("CheckBox Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMenuBar bar = new JMenuBar(); JMenu menu = new JMenu("Menu"); menu.setMnemonic(KeyEvent.VK_M); JCheckBoxMenuItem one = new JCheckBoxMenuItem(); menu.add(one); JCheckBoxMenuItem two = new JCheckBoxMenuItem("Boy"); menu.add(two); JCheckBoxMenuItem three = new JCheckBoxMenuItem(boyIcon); menu.add(three); JCheckBoxMenuItem four = new JCheckBoxMenuItem("Girl", true); menu.add(four); JCheckBoxMenuItem five = new JCheckBoxMenuItem("Boy", boyIcon); five.addItemListener(iListener); menu.add(five); Icon stateIcon = new DiamondAbstractButtonStateIcon(Color.black); UIManager.put("CheckBoxMenuItem.checkIcon", stateIcon); JCheckBoxMenuItem six = new JCheckBoxMenuItem("Girl", girlIcon, true); six.addActionListener(aListener); menu.add(six); bar.add(menu); frame.setJMenuBar(bar); frame.setSize(350, 250); 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 www . ja v a2 s.c o 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); }