logo
down
shadow

How do you open a program by using ActionListener in Java?


How do you open a program by using ActionListener in Java?

By : mwe
Date : November 25 2020, 07:06 PM
should help you out I am busy creating a GUI in Java with buttons such that if I press a button it must open another program like Excel or Word and also a folder. Is there anyway that this is possible? , You would do something like this...
code :


Share : facebook icon twitter icon
Error on basic Java program - cannot find symbol ActionListener()

Error on basic Java program - cannot find symbol ActionListener()


By : user2571906
Date : March 29 2020, 07:55 AM
I hope this helps you . The following code is working, Here is what I did :
add import java.awt.event.*; remove unnecessary import java.util.*; replaced guiFrame.close() with guiFrame.dispose(); as There is no Frame.close() method.
code :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyTest {

    public static void main(String args[]) throws Exception {

        JFrame guiFrame = new JFrame("Test Frame");
        guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        guiFrame.setTitle("Example GUI");
        guiFrame.setSize(300, 250);
        guiFrame.setLocationRelativeTo(null);

        JButton btn = new JButton("Hit Me");

        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                guiFrame.dispose();
            }
        });
        guiFrame.add(btn, BorderLayout.SOUTH);
        guiFrame.setVisible(true);

    }
}
ActionListener Issue: Program to see if a number is prime or not? (Java GUI)

ActionListener Issue: Program to see if a number is prime or not? (Java GUI)


By : Mirza Zaheer Baba
Date : March 29 2020, 07:55 AM
seems to work fine ActionListener for JTextField is triggered when you press enter in the text field.
Edit: If you want to do the prime check whenever the value is changed in the text field. As suggested by @MadProgrammer, you can add a DocumentListener to the document of the text field and implement the insertUpdate (called when you add a character) and removeUpdate (called when you remove a character) methods.
code :
primeTestField.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void insertUpdate(DocumentEvent e) 
            check();
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            check();
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
        }
    });
private void check() {
    int n;
    try {
        System.out.println(primeTestField.getText());
        n = Integer.parseInt(primeTestField.getText());
    } catch (Exception ex) {
        return;
    }
    if (n == 2) {
        trueFalseLabel.setText("Yes");
    } else {
        boolean isPrime = true;
        for (int i = 2; 2 * i < n; i++) {
            if (n % i == 0) {
                isPrime = false;
                break;
            }
        }
        if (isPrime) {
            trueFalseLabel.setText("Yes");
        } else {
            trueFalseLabel.setText("No");
        }
    }
}
Java - Unable to open a new frame from ActionListener

Java - Unable to open a new frame from ActionListener


By : Rinat Mahmutov
Date : March 29 2020, 07:55 AM
Hope this helps This is your login class. I put the JFrame frame in the global scope, so you can manipulate it from the ButtonListener method. I also created a SomeFrame class, just to demonstrate the new JFrame that would be created when you click the button. When an action is performed(the button is clicked) a new object of SomeFrame is created. Since SomeFrame extends JFrame we can use the method setVisible() to a SomeFrame object. The SomeFrame frame appears and the LoginView is no longer visible.
code :
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginView {

 public static JFrame frame = new JFrame("Bus Tour Booking System");

    public static void main(String[] args) {

        frame.setSize(300, 200);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        frame.add(panel);
        placeComponents(panel);

        frame.setVisible(true);
    }

    private static void placeComponents(JPanel panel) {

        panel.setLayout(null);

        JLabel titleLabel = new JLabel("Bus Tour Booking System");
        titleLabel.setBounds(70,15,150,25);
        panel.add(titleLabel);

        JLabel userLabel = new JLabel("Username: ");
        userLabel.setBounds(30, 50, 80, 25);
        panel.add(userLabel);

        JTextField userText = new JTextField(20);
        userText.setBounds(120, 50, 130, 25);
        panel.add(userText);

        JLabel passwordLabel = new JLabel("Password: ");
        passwordLabel.setBounds(30, 80, 80, 25);
        panel.add(passwordLabel);

        JPasswordField passwordText = new JPasswordField(20);
        passwordText.setBounds(120, 80, 130, 25);
        panel.add(passwordText);

        JButton loginButton = new JButton("login");
        loginButton.setBounds(100, 125, 80, 25);
        panel.add(loginButton);

        ActionListener myButtonListener = new MyButtonListener();
        loginButton.addActionListener(myButtonListener);

    }

    private static class MyButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
           SomeFrame newFrame = new SomeFrame();
           newFrame.setVisible(true);
           frame.setVisible(false);
        }

      }
 }
import javax.swing.JFrame;
import javax.swing.JPanel;

public class SomeFrame extends JFrame {

    public SomeFrame(){
         super("something");
            this.setSize(300, 200);
            this.setResizable(false);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JPanel panel = new JPanel();
            this.add(panel);

            this.setVisible(true);
    }

}
Java actionListener in another class won't open the window

Java actionListener in another class won't open the window


By : user2256121
Date : March 29 2020, 07:55 AM
I hope this helps . The current problem with the code is down to the fact that the action listener has no reference to the original window, and instead creates an entirely separate instance that is never set visible. (As detailed by D.G).
But the action listener is not needed. The effect can be achieved using a ButtonGroup, like this:
code :
import javax.swing.*;
import java.awt.*;

public class RadioButtonWindow extends JFrame{
    public JRadioButton radio1= new JRadioButton("Salam1");
    public JRadioButton radio2= new JRadioButton("Salam2");
    public JRadioButton radio3= new JRadioButton("Salam3");

    public RadioButtonWindow(){
        super("Title");
        setLayout(new FlowLayout());

        add(radio1);
        add(radio2);
        add(radio3);

        // Only one button in this group can be selected at a time!
        ButtonGroup bg = new ButtonGroup();
        bg.add(radio1);
        bg.add(radio2);
        bg.add(radio3);
    }

   public static void main(String args[]) {
        RadioButtonWindow  salam = new RadioButtonWindow ();
        salam.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Wrong way to size a GUI!
        //salam.setSize(500,150);
        salam.setResizable(false);
        // Correct way to size a GUI
        salam.pack();
        salam.setVisible(true);
    }
}
Java ActionListener Error , Buttons Color Changing Program using popups

Java ActionListener Error , Buttons Color Changing Program using popups


By : Nara Arun Kumar
Date : March 29 2020, 07:55 AM
hop of those help? I'm trying to make a program that changes different colors in frame to the color I choose using a popup menu. I'm getting an error in the ActionListener and without it I can't have an action performed for each colored button. , Take a look at this section...
Related Posts Related Posts :
  • Why is it mandatory for ternary operations to return a value?
  • How can I make my listview rows different heights
  • Array declaration trick : Is it bad to use it?
  • Calling different classes with if else statements
  • Free API for speech recognition system
  • Java: Objects in JFrame are messed up
  • Is there a similar of MessageBox of ExtJS in ADF Faces
  • Passing HttpServletResponse in Spring Boot controller method
  • Failed to load libraries: [netty_tcnative_linux_arm_32, netty_tcnative_linux_arm_32_fedora, netty_tcnative_arm_32, netty
  • How to build a small documents storage?
  • Getting pixel position of a text in an image
  • How do I get a list of Methods called from a Class in Eclipse IDE?
  • Convert string to day of week (not exact date)
  • Unable to understand how rounding is working for decimal digits (Java)
  • How to avoid java.lang.NoSuchMethodError: org.apache.poi.util.IOUtils.copy(Ljava/io/InputStream;Ljava/io/OutputStream;)
  • JTable Renderer not working with RowSorter?
  • From java.util.concurrent.Future<T> to play.libs.F.Promise<T>: How to do that in Java?
  • jUnit tests work in Eclipse, but fail in ant - java.lang.RuntimeException: Stub
  • How do i add a tooltip to existing gwt Textbox?
  • actor replying to non-actor
  • Google Cloud Messaging - server connection
  • Which classes are unmodifiable and cannot be intrumented and is there a way to bypass it?
  • Java & Jersey 1.17: Custom default response headers
  • Java rounding error
  • Setting custom RejectedExecutionHandler for ThreadPoolExecutor after executing some tasks?
  • Arquillian test class variables lose value
  • java.util.ConcurrentModificationException while inserting in ArrayList
  • How to write generic Java API without method Overloading
  • Java Serialization / Deserialization of an ArrayList works only on first program execution
  • Conway's Game of Life's rules dont apply properly
  • call a java method when Click on a html button without using javascript
  • Why does SSLSocketFactory.createSocket returns a Socket instead of SSLSocket?
  • How to schedule a task in Tomcat
  • compare two pdf files (approach) using java
  • What is the use of allowCoreThreadTimeout( ) in ThreadPoolExecutor?
  • Describe the Strategy Design Pattern in Java?
  • MongoDB too many open connections
  • Issue with generate JAXB classes with value constructors
  • Moving from one frame to another in java swing
  • Get a thread by Id
  • how to open front camera and back camera at same time?
  • Messed up with Java Declaration
  • Extracting chart in EXcel as image using java
  • How to get latitude and longitude in Android phone?
  • Do GC pauses and kill -STOP produce the same behavior?
  • Android HTML.ImageGetter Images resized
  • The way to instantiate map<String, List<String>> in Java
  • Java Making objects move while buttons held
  • Androidx Navigation View - `setNavigationItemSelectedListener` Doesn't Work
  • after compiling code JFrame does not appear
  • Netbeans project, JFrames ran from the main JFrame close all JFrames in project
  • Square Retrofit Client: How to enable/disable followRedirects? How to intercept redirect URL?
  • Java Circular Reference - Can’t Compile
  • Mouse click on specific time JAVA
  • android 4 onCreateDialog return type incompatible when overriding
  • Java Implementation of Oracle's DBMS_UTILITY.GET_HASH_VALUE Function
  • javac cannot be run, and furthermore does not seem installed
  • Function to obtain a thread given its ID
  • Java File path to Executable has spaces
  • Minimum Oracle Java VM heap memory setting?
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org