Results 1 to 5 of 5
  1. #1
    alikhan707 is offline Member
    Join Date
    Dec 2008
    Posts
    1
    Rep Power
    0

    Default Help me on this Form please

    Hello,
    i have design this form, i have place 3 values(Aus,Greece,Ireland) in Combo Box. I want to go on next form whenever i click on any one of these values but i unable to this action. For examp, when i click on Greece, it should go on next form which contain information about the Greece. Can somebody help me on this ? please.... here is the code in working condition....


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    /**
    * Summary description for Test
    *
    */
    public class Test extends JFrame
    {
    // Variables declaration
    private JLabel jLabel1;
    private JComboBox jComboBox1;
    private JButton jButton1;
    private JPanel contentPane;
    // End of variables declaration


    public Test()
    {
    super();
    initializeComponent();
    //
    // TODO: Add any constructor code after initializeComponent call
    //

    this.setVisible(true);
    }


    private void initializeComponent()
    {
    jLabel1 = new JLabel();
    jComboBox1 = new JComboBox();
    jButton1 = new JButton();
    contentPane = (JPanel)this.getContentPane();

    //
    // jLabel1
    //
    jLabel1.setText("Country");
    //
    // jComboBox1
    //
    jComboBox1.addItem("Austria");
    jComboBox1.addItem("Greece");
    jComboBox1.addItem("Ireland");
    jComboBox1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    jComboBox1_actionPerformed(e);
    }

    });
    //
    // jButton1
    //
    jButton1.setText("Next");
    jButton1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    jButton1_actionPerformed(e);
    }

    });
    //
    // contentPane
    //
    contentPane.setLayout(null);
    addComponent(contentPane, jLabel1, 45,51,60,18);
    addComponent(contentPane, jComboBox1, 162,51,100,22);
    addComponent(contentPane, jButton1, 164,123,83,28);
    //
    // Test
    //
    this.setTitle("Test - extends JFrame");
    this.setLocation(new Point(131, 70));
    this.setSize(new Dimension(390, 300));
    }

    /** Add Component Without a Layout Manager (Absolute Positioning) */
    private void addComponent(Container container,Component c,int x,int y,int width,int height)
    {
    c.setBounds(x,y,width,height);
    container.add(c);
    }

    //
    // TODO: Add any appropriate code in the following Event Handling Methods
    //
    private void jComboBox1_actionPerformed(ActionEvent e)
    {
    System.out.println("\njComboBox1_actionPerformed(A ctionEvent e) called.");

    Object o = jComboBox1.getSelectedItem();
    System.out.println(">>" + ((o==null)? "null" : o.toString()) + " is selected.");
    // TODO: Add any handling code here for the particular object being selected

    }

    private void jButton1_actionPerformed(ActionEvent e)
    {
    System.out.println("\njButton1_actionPerformed(Act ionEvent e) called.");
    // TODO: Add any handling code here

    }

    //
    // TODO: Add any method code to meet your needs in the following area
    //




    public static void main(String[] args)
    {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JDialog.setDefaultLookAndFeelDecorated(true);
    try
    {
    UIManager.setLookAndFeel("com.sun.java.swing.plaf. windows.WindowsLookAndFeel");
    }
    catch (Exception ex)
    {
    System.out.println("Failed loading L&F: ");
    System.out.println(ex);
    }
    new Test();
    }
    //= End of Testing =


    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    For examp, when i click on Greece, it should go on next form which contain information about the Greece. Can somebody help me on this ?
    What do you mean by "go on next form"? Do you mean open a dialog window of some sort with a display of information? Do you mean have your main JFrame swap displays so that now it is showing another JPanel that displays country information? You need to be a bit more clear here I think.

    Also, when posting code, if you use code tags, your code will be more readable and this will increase the number of folks willing to read your question and think about your code -- something that you want to have happen. So you may want to edit your post and place the tag [code] on top of your code block and the tag [/code] below your code block.

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    I glanced through the code quickly, but I didn't see any event listeners attached to the combo box. I can't remember the exact one, but you want to listen for item selection events. When you get an event, you then display the next form.

    I suggest you find an introductory tutorial on Model-View-Controller programming in Java. Even with little projects, doing it "right" ends up taking a lot less time than stringing code together. It should cover events and listeners. This is a place where Java is powerful but complicated. Take the time to learn how it works, and, again, you'll end up taking a lot less time.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Actually he does have an ActionListener added to the combobox that in its actionPerformed method prints out some debugging information to let him know that an item was selected. Based on the look of his program, I'm assuming that the real action should occur when the button is pressed.

    To the original poster, you would do well to study the Sun Swing Tutorials on how to respond to actions and also the tutorials on how to create and display dialogs. You can find both of them here:

    The Really Big Index

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Here's a quickly thrown together example that uses a DefaultComboBoxModel to hold objects of a class called CountryInfo. These objects hold two strings: the country's name and a url string that points to an online site that holds information on the country. When the selectCountryBtn is pressed, it will get the CountryInfo object out of the model, and use it to place the desired web page into a JEditorPane called infoPane.

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import javax.swing.DefaultComboBoxModel;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JComponent;
    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    
    public class ComboAction
    {
      private static final Dimension INFO_PANE_SIZE = new Dimension(800, 800);
      private JPanel mainPanel = new JPanel();
      private JEditorPane infoPane = new JEditorPane();
      private DefaultComboBoxModel comboModel = new DefaultComboBoxModel();
    
      public ComboAction()
      {
        comboModel.addElement(new CountryInfo("Australia", 
          "https://www.cia.gov/library/publications/the-world-factbook/print/as.html"));
        comboModel.addElement(new CountryInfo("Greece", 
          "https://www.cia.gov/library/publications/the-world-factbook/print/gr.html"));
        comboModel.addElement(new CountryInfo("Ireland", 
          "https://www.cia.gov/library/publications/the-world-factbook/print/ei.html"));
        JComboBox combo = new JComboBox(comboModel);
        
        JButton selectCountryBtn = new JButton("Select");
        selectCountryBtn.addActionListener(new SelectCountryListener());
        
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 25, 5));
        topPanel.add(new JLabel("Choose a Country:"));
        topPanel.add(combo);
        topPanel.add(selectCountryBtn);
        
        infoPane.setPreferredSize(INFO_PANE_SIZE);
        
        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(topPanel, BorderLayout.NORTH);
        mainPanel.add(new JScrollPane(infoPane), BorderLayout.CENTER);
      }
    
      public JComponent getPanel()
      {
        return mainPanel;
      }
      
      private class SelectCountryListener implements ActionListener
      {
        public void actionPerformed(ActionEvent e)
        {
          CountryInfo info = (CountryInfo)comboModel.getSelectedItem();
          try
          {
            URL url = new URL(info.getInfoAddress());
            infoPane.setPage(url);
          }
          catch (MalformedURLException e1)
          {
            e1.printStackTrace();
          }
          catch (IOException e1)
          {
            e1.printStackTrace();
          }
        }
      }
    
      private static void createAndShowGUI()
      {
        JFrame frame = new JFrame("ComboAction Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new ComboAction().getPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowGUI();
          }
        });
      }
    }
    
    class CountryInfo
    {
      private String name;
      private String urlInfoAddress;
      public CountryInfo(String name, String infoAddress)
      {
        this.name = name;
        this.urlInfoAddress = infoAddress;
      }
      public String getInfoAddress()
      {
        return urlInfoAddress;
      }
      public String getName()
      {
        return name;
      }
      @Override
      public String toString()
      {
        return name;
      }
    }

Similar Threads

  1. Replies: 6
    Last Post: 11-28-2008, 07:55 AM
  2. take value other form
    By hungleon88 in forum AWT / Swing
    Replies: 4
    Last Post: 11-18-2008, 10:37 AM
  3. New to this form
    By talasilasumanth in forum Introductions
    Replies: 5
    Last Post: 10-30-2008, 01:34 PM
  4. enctype=multipart/form-data with form data in struts
    By vk_satheesh in forum New To Java
    Replies: 0
    Last Post: 09-19-2008, 01:48 PM
  5. Form Help Pls
    By CoOlbOyCoOl in forum NetBeans
    Replies: 4
    Last Post: 05-27-2007, 09:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •