Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 784869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:46:53+00:00 2026-05-14T20:46:53+00:00

here is my code, i basically just did a tester for the most common

  • 0

here is my code, i basically just did a tester for the most common listeners, which i might later use in future projects, the main problem is in the keylistener at the bottom, i am trying to re-show the frame but i think it just cant be done that way, please help
ps: no idea why the imports dont show up right.

package newpackage;

import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JSeparator;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class NewClass1 extends JFrame {


private JLabel item1,infomouse,infoclicks,infoKeys,writehere;
private JButton button1,button2,button3;
private JTextArea text1,status,KeyStatus;
private JTextField text2,text3,mouse,clicks,test;
private JSeparator sep1;
private int clicknumber;

public NewClass1() {
    super("Listener Tests");
    setLayout(null);
    sep1 = new JSeparator();

    button1 = new JButton("Button1");
    button2 = new JButton("Button2");
    button3 = new JButton("Button3");
    item1 = new JLabel("Button Status :");
    infomouse = new JLabel("Mouse Status :");
    infoclicks = new JLabel("Nº of clicks :");
    infoKeys = new JLabel("Keyboard status:");
    writehere = new JLabel("Write here: ");


    text1 = new JTextArea();
    text2 = new JTextField(20);
    text3 = new JTextField(20);
    status = new JTextArea();
    mouse  = new JTextField(20);
    clicks = new JTextField(4);
    KeyStatus = new JTextArea();
    test = new JTextField(3);
    clicks.setText(String.valueOf(clicknumber));

    text1.setEditable(true);
    text2.setEditable(false);
    text3.setEditable(false);
    status.setEditable(false);
    mouse.setEditable(false);
    clicks.setEditable(false);
    KeyStatus.setEditable(false);

    text1.setBounds(135, 310, 150, 20);
    text2.setBounds(135, 330, 150, 20);
    text3.setBounds(135, 350, 150, 20);
    status.setBounds(15, 20, 240, 20);
    infomouse.setBounds(5,45,120,20);
    infoKeys.setBounds(5,90,120,20);
    KeyStatus.setBounds(15,115,240,85);
    test.setBounds(15,225,240,20);
    mouse.setBounds(15,70,100,20);
    infoclicks.setBounds(195, 45, 140, 20);
    clicks.setBounds(195, 70, 60, 20);

    item1.setBounds(5, 0, 120, 20);
    button1.setBounds(10, 310, 115, 20);
    button2.setBounds(10, 330, 115, 20);
    button3.setBounds(10, 350, 115, 20);
    sep1.setBounds(5, 305, 285, 10);

    sep1.setBackground(Color.BLACK);
    status.setBackground(Color.LIGHT_GRAY);

    button1.addActionListener(new button1list());
    button2.addActionListener(new button1list());
    button3.addActionListener(new button1list());

    button1.addMouseListener(new MouseList());
    button2.addMouseListener(new MouseList());
    button3.addMouseListener(new MouseList());
    getContentPane().addMouseListener(new MouseList());
    test.addKeyListener(new KeyList());
    this.addKeyListener(new KeyList());
    test.requestFocus();

    add(item1);
    add(button1);
    add(button2);
    add(button3);
    add(text1);
    add(text2);
    add(text3);
    add(status);
    add(infomouse);
    add(mouse);
    add(infoclicks);
    add(clicks);
    add(infoKeys);
    add(KeyStatus);
    add(test);
    add(sep1);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    try{
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }catch (Exception e){System.out.println("Error");}
    SwingUtilities.updateComponentTreeUI(this);
    setSize(300, 400);
    setResizable(false);
    setVisible(true);
    test.setFocusable(true);
    test.setFocusTraversalKeysEnabled(false);
    setLocationRelativeTo(null);
}

public class button1list implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        String buttonpressed = e.getActionCommand();
        if (buttonpressed.equals("Button1")) {
            text1.setText("just");
        } else if (buttonpressed.equals("Button2")) {
            text2.setText(text2.getText()+"testing ");
        } else if (buttonpressed.equals("Button3")) {
            text3.setText("this");
        } 
    }
}
    public class MouseList implements MouseListener{
        public void mouseEntered(MouseEvent e){
        if(e.getSource()==button1){
                status.setText("button 1 hovered");
            }
        else if(e.getSource()==button2){
                status.setText("button 2 hovered");
            }
        else if(e.getSource()==button3){
                status.setText("button 3 hovered");
            }
        }
        public void mouseExited(MouseEvent e){
                status.setText("");
        }
        public void mouseReleased(MouseEvent e){
            if(!status.getText().equals("")){
                status.replaceRange("", 0, 22);
            }
        }
        public void mousePressed(MouseEvent e){
            if(e.getSource()==button1){
                status.setText("button 1 being pressed");
            }
        else if(e.getSource()==button2){
                status.setText("button 2 being pressed");
            }
        else if(e.getSource()==button3){
                status.setText("button 3 being pressed");

            }
        }
        public void mouseClicked(MouseEvent e){
            clicknumber++;
            mouse.setText("mouse working");
            clicks.setText(String.valueOf(clicknumber));
      }
 }
    public class KeyList implements KeyListener{
        public void keyReleased(KeyEvent e){}
        public void keyPressed(KeyEvent e){
            KeyStatus.setText("");
            test.setText("");
            String full = e.paramString();
            String [] temp = null;
            temp = full.split(",");
            for(int i=0; i<7 ;i++){
            KeyStatus.append(temp[i] + "\n");
            }
            if(e.getKeyChar()=='h'){setVisible(false);
                test.requestFocus();
            }
            if(e.getKeyChar()=='s'){setVisible(true);}
        }
        public void keyTyped(KeyEvent e){}
    }

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:46:54+00:00Added an answer on May 14, 2026 at 8:46 pm

    Visible components can have the keyboard focus, and recieve keyboard and other input events, but invisible components cannot have the focus, nor recieve input events. When you make the frame invisible, it stops recieving input events, as does all it’s children, including the test component you are adding a KeyListener to.

    To make this work, you will have to have a visible component, and forward keyboard events from that to your invisible frame. Or better, call invisibelFrame.setVisible(true) from your already visible component.

    Alternatively, you may be able to find some other way to trigger showing the frame. For example, a system tray component, with a context menu is a common pattern for hiding and showing application frames (and for some users, causing much chagrin in the process!)

    See

    • Sun Tutorial, how to use the System Tray
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 465k
  • Answers 465k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is a fbml tag for that: fb:prompt-permission Example: <fb:prompt-permission… May 16, 2026 at 1:27 am
  • Editorial Team
    Editorial Team added an answer $('select[name="sb[]"]').change(function(){ var $this = $(this); $.get('file.php', { action: 'dothis'}, function(html)… May 16, 2026 at 1:27 am
  • Editorial Team
    Editorial Team added an answer I've been using ugettext instead of ugettext_lazy. Remember to use… May 16, 2026 at 1:27 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.