1 Reply Latest reply: Jun 2, 2013 12:39 PM by TPD-Opitz RSS

    Create new JButton by pressing on existing JButton

    1012327
      Hi all,

      I am trying to create a code which allows the users to create JButtons to a an existing frame.
      So what I want is that users can create buttons wihch opens a URL. The user need to be able to click on an Existing JButton called "Add Favorite", insert the name of the favorite and URL and the button is added with the functionality to open the URL in Internet Explorer.

      I already have a beginning but have no clue how to add that functionaly to create JButton by pressing on button "Add Favorites" with the needed functionality
      The functionality needes to be added to Button4 ("add Favorite" ). As you can see, Button4 ("add Favorite") opens two inputShowDialog which allows the users to insert the name of the favorite and URL.

      Thanks in advance.

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      import javax.swing.JButton.*;
      import java.awt.Dimension.*;
      import java.lang.RuntimeException.*;
      import javax.swing.JOptionPane;

      public class Favorites extends JFrame implements ActionListener{

      //private JPanel panel1 = new Jpanel ("Add Favorite");
      private JButton button1 = new JButton ("Google");
      private JButton button2 = new JButton ("Tweakers");
      private JButton button3 = new JButton ("Hardware.info");
      private JButton button4 = new JButton ("Add Favorite");


      public Favorites (){createGUI();}public static void main (String[] args) {
      Favorites frame = new Favorites ();
      frame.setVisible(true);
      frame.setSize(150,300);
      frame.setLocation(200,0);
      frame.setLocation(1750,300);



      }


      private void createGUI () {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Container window = getContentPane();
      window.setLayout (new FlowLayout ());
      button1 = new JButton ("Google");
      add(button1);button1.addActionListener(this);

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      window.setLayout (new FlowLayout ());
      button2 = new JButton ("Tweakers");
      add(button2);button2.addActionListener(this);

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      window.setLayout (new FlowLayout ());
      button3 = new JButton ("Hardware.info");
      add(button3);button3.addActionListener(this);

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      window.setLayout (new FlowLayout ());
      button4 = new JButton ("Add Favorite");
      add(button4);button4.addActionListener(this);

      }

      public void actionPerformed(java.awt.event.ActionEvent event) {

      if (event.getSource() == button1) { 
      try
      {Runtime r = Runtime.getRuntime();
               r.exec("cmd /c start iexplore.exe www.google.nl");
               }
      catch(Exception e) {}
      }

      if (event.getSource() == button2) { 
      try
      {Runtime r = Runtime.getRuntime();
               r.exec("cmd /c start iexplore.exe www.tweakers.net");
               }
      catch(Exception e) {}
      }
      if (event.getSource() == button3) { 
      try
      {Runtime r = Runtime.getRuntime();
               r.exec("cmd /c start iexplore.exe www.hardware.info");
               }
      catch(Exception e) {}
      }


      if (event.getSource() == button4) { 
      try
      {
      int Favorite;
      String FavoriteString;
      FavoriteString = JOptionPane.showInputDialog ("Give name Favorite");
      int Link;
      String LinkString;
      LinkString = JOptionPane.showInputDialog ("Insert link");




      }
      catch(Exception e) {}
      }

      }

      }
        • 1. Re: Create new JButton by pressing on existing JButton
          TPD-Opitz
          Welcome to the forum.

          please enclose you code examples in
          -tags for better readability.
          
          
          1009324 wrote:
          The functionality needes to be added to Button4 ("add Favorite" ).
          So why do you name it <tt>Button4</tt> and not <tt>addFavorite</tt>?
          As you can see, Button4 ("add Favorite") opens two inputShowDialog which allows the users to insert the name of the favorite and URL.
          Do you see that the code for createing an URL-button looks exactly same accept the actual URL? 1. Change the code so that the URL is assigned to a variable first. (You IDE meight have a refactoring "exctact local variable". 2. Move this variable assignment before the complete button creation code. 3. Select all the lines excluding the new variable assignment and the line that adds the button to the Container. 4. use the "extract method" refactoring of your IDE. now you have a method you can call when ever you need to create a new URL-button. bye TPD