public class ApplicationWindow extends javax.swing.JFrame {/** Creates new form ApplicationWindow */public ApplicationWindow() {initComponents();listPanel.setVisible(false);}/** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */@SuppressWarnings("unchecked")// private void initComponents() {comboPanel = new javax.swing.JPanel();comboLbl = new javax.swing.JLabel();fruits = new javax.swing.JComboBox();listPanel = new javax.swing.JPanel();listLbl = new javax.swing.JLabel();jScrollPane1 = new javax.swing.JScrollPane();vegs = new javax.swing.JList();vegFruitBut = new javax.swing.JButton();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);setPreferredSize(new java.awt.Dimension(300, 250));setSize(new java.awt.Dimension(300, 250));comboLbl.setText("Fruits");fruits.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Apple", "Apricot", "Banana", "Cherry", "Date", "Kiwi", "Orange", "Pear", "Strawberry", " ", " " }));org.jdesktop.layout.GroupLayout comboPanelLayout = new org.jdesktop.layout.GroupLayout(comboPanel);comboPanel.setLayout(comboPanelLayout);comboPanelLayout.setHorizontalGroup(comboPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(comboPanelLayout.createSequentialGroup().add(31, 31, 31).add(comboLbl).add(39, 39, 39).add(fruits, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).addContainerGap(83, Short.MAX_VALUE)));comboPanelLayout.setVerticalGroup(comboPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(comboPanelLayout.createSequentialGroup().addContainerGap().add(comboPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(comboLbl).add(fruits, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)).addContainerGap(64, Short.MAX_VALUE)));getContentPane().add(comboPanel, java.awt.BorderLayout.PAGE_START);listLbl.setText("Vegetables:");vegs.setModel(new javax.swing.AbstractListModel() {String[] strings = { "Asparagus", "Beans", "Broccoli", "Cabbage", "Carrot", "Celery", "Cucumber", "Leek", "Mushroom", "Pepper", "Radish", "Shallot", "Spinach", "Swede", "Turnip" };public int getSize() { return strings.length; }public Object getElementAt(int i) { return strings[i]; }});vegs.setLayoutOrientation(javax.swing.JList.HORIZONTAL_WRAP);jScrollPane1.setViewportView(vegs);org.jdesktop.layout.GroupLayout listPanelLayout = new org.jdesktop.layout.GroupLayout(listPanel);listPanel.setLayout(listPanelLayout);listPanelLayout.setHorizontalGroup(listPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(listPanelLayout.createSequentialGroup().add(37, 37, 37).add(listLbl).add(39, 39, 39).add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).addContainerGap(33, Short.MAX_VALUE)));listPanelLayout.setVerticalGroup(listPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(listPanelLayout.createSequentialGroup().add(listPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(listPanelLayout.createSequentialGroup().addContainerGap().add(listLbl)).add(listPanelLayout.createSequentialGroup().add(11, 11, 11).add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))).addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));getContentPane().add(listPanel, java.awt.BorderLayout.CENTER);vegFruitBut.setText("Fruit or Veg");vegFruitBut.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {vegFruitButActionPerformed(evt);}});getContentPane().add(vegFruitBut, java.awt.BorderLayout.PAGE_END);pack();java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();java.awt.Dimension dialogSize = getSize();setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);}// private void vegFruitButActionPerformed(java.awt.event.ActionEvent evt) {listPanel.setVisible(!listPanel.isVisible());comboPanel.setVisible(!comboPanel.isVisible());}/** * @param args the command line arguments */public static void main(String args[]) {/* Set the Nimbus look and feel *////* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html*/try {for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {if ("Nimbus".equals(info.getName())) {javax.swing.UIManager.setLookAndFeel(info.getClassName());break;}}} catch (ClassNotFoundException ex) {java.util.logging.Logger.getLogger(ApplicationWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);} catch (InstantiationException ex) {java.util.logging.Logger.getLogger(ApplicationWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);} catch (IllegalAccessException ex) {java.util.logging.Logger.getLogger(ApplicationWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);} catch (javax.swing.UnsupportedLookAndFeelException ex) {java.util.logging.Logger.getLogger(ApplicationWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);}///* Create and display the form */java.awt.EventQueue.invokeLater(new Runnable() {public void run() {new ApplicationWindow().setVisible(true);}});}// Variables declaration - do not modifyprivate javax.swing.JLabel comboLbl;private javax.swing.JPanel comboPanel;private javax.swing.JComboBox fruits;private javax.swing.JScrollPane jScrollPane1;private javax.swing.JLabel listLbl;private javax.swing.JPanel listPanel;private javax.swing.JButton vegFruitBut;private javax.swing.JList vegs;// End of variables declaration}
Java Code:
The code shown below is what is created behind the scenes of the NetBeans GUI Builder by following the Coding a Simple GUI Application - Part II. The reason it's here is so that you can compare it to the Java code listing written manually in the Coding a Simple GUI Application - Part I.