How do I close the jFrame of my JList? - java

I have a JList problem. I have listeners for this JList (both mouse and keyboard).
I would like, after you double clicked on one of the option of the list (or pressed Enter), that the JFrame closes. I couldn't find that anywhere. Could you please help me with that?
Here's the class that I use (taken from StackOverflow):
import javax.swing.*;
import java.awt.event.*;
import java.util.Vector;
public class ActionJList extends JList {
ActionListener al;
boolean close=false;
public ActionJList(String[] it){
super(it);
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
if (al == null) return;
Object ob[] = getSelectedValues();
if (ob.length > 1) return;
if (me.getClickCount() == 2) {
System.out.println("Sending ACTION_PERFORMED to ActionListener");
al.actionPerformed(new ActionEvent(this,
ActionEvent.ACTION_PERFORMED,
ob[0].toString()));
me.consume();
close=true;
}
}
});
addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent ke) {
if (al == null) return;
Object ob[] = getSelectedValues();
if (ob.length > 1) return;
if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
System.out.println("Sending ACTION_PERFORMED to ActionListener");
al.actionPerformed(new ActionEvent(this,
ActionEvent.ACTION_PERFORMED,
ob[0].toString()));
ke.consume();
}
}
});
this.setSelectedIndex(0);
}
public ActionJList(Vector it){
super(it);
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
if (al == null) return;
Object ob[] = getSelectedValues();
if (ob.length > 1) return;
if (me.getClickCount() == 2) {
System.out.println("Sending ACTION_PERFORMED to ActionListener");
al.actionPerformed(new ActionEvent(this,
ActionEvent.ACTION_PERFORMED,
ob[0].toString()));
me.consume();
}
}
});
addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent ke) {
if (al == null) return;
Object ob[] = getSelectedValues();
if (ob.length > 1) return;
if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
System.out.println("Sending ACTION_PERFORMED to ActionListener");
al.actionPerformed(new ActionEvent(this,
ActionEvent.ACTION_PERFORMED,
ob[0].toString()));
ke.consume();
}
}
});
this.setSelectedIndex(0);
}
public void addActionListener(ActionListener al){
this.al = al;
}
public boolean getClose(){return close;}
}

You can always use the following snippet:
Window window = SwingUtilities.getWindowAncestor(ActionJList.this);
if (window!=null)
window.setVisible(false);
NB: instead of adding a KeyListener/KeyAdapter to your JList, consider using Swing KeyBindings.

For what it is worth check out List Action for a reusable class that adds the MouseListener and Key Bindings for you. Also, the source of the event is the JList so it makes it easy for you to create your Action using Guillaume's suggestion.
There is no need to make a reference to the JFrame available. The SwingUtilities approach is the better way.

Related

ArrayList of ActionListeners cleared when notifyListeners() called

I am trying to write a class that both listens for actions from buttons and notifies another class when one of the buttons is pressed. I have an ArrayList<ActionListener> and methods addActionListener(ActionListener al), removeActionListener(ActionListener al), and notifyActionListeners(ActionEvent ae). I print to a separate window whenever I add a listener, and print the size of actionListeners as well. It works great and prints that I have 1 actionListener, but then when I try to notify the listeners it says that there are 0 objects in actionListeners. I added a println() to the removeActionListener(al) method to see if it is called, and it never is.
Here's the class:
package state;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import driver.GameDriver;
import ui.Button;
public class MainMenu extends Menu {
private static final long serialVersionUID = -7130241947836998525L;
private ArrayList<ActionListener> actionListeners;
private Button play;
private Button scores;
private Button settings;
private Button help;
private Button exit;
public MainMenu() {
super("Main Menu");
actionListeners = new ArrayList<ActionListener>();
}
#Override
protected void addComponents() {
//Irrelevant to Stackexchange
}
#Override
public void actionPerformed(ActionEvent arg0) {
Object src = arg0.getSource();
if (src == play) {
} else if (src == scores) {
} else if (src == settings) {
} else if (src == help) {
} else if (src == exit) {
ActionEvent ae = new ActionEvent(this, ActionEvent.ACTION_FIRST, "exit");
notifyActionListeners(ae);
}
}
public void addActionListener(ActionListener al) {
GameDriver.println("Added Listener:");
actionListeners.add(al);
GameDriver.println(actionListeners.size());
}
public void removeActionListener(ActionListener al) {
GameDriver.println("Removed al for some reason");
actionListeners.remove(al);
}
private void notifyActionListeners(ActionEvent ae) {
GameDriver.println("Sending exit to " + actionListeners.size() + " listeners.");
for(int i = 0; i < actionListeners.size(); i++) {
GameDriver.println("Exit sent");
actionListeners.get(i).actionPerformed(ae);
}
}
}
Here are the methods that actually reference the instance of MainMenu:
1. Initialization
protected GameDriver() {
mainMenu = new MainMenu();
mainMenu.addActionListener(this);
debugger = new Debugger();
println("Size Loader Test...");
SizeLoader.loadSizes();
println(SizeLoader.getCurrentSize());
println("Complete.");
println("Window Test...");
window = new Window("Asteroids");
windowManager = new WindowManager();
// window.addWindowFocusListener(windowManager);
// window.addWindowListener(windowManager);
// window.addWindowStateListener(windowManager);
window.buildWindow(SizeLoader.getCurrentWidth(), SizeLoader.getCurrentHeight());
window.add(new MainMenu());
println("Complete");
println("Menu Test...");
}
And here's the actionPerformed(ae):
#Override
public void actionPerformed(ActionEvent e) {
println("Event happened");
if (e.getSource() == mainMenu) {
if (e.getActionCommand() == "exit") {
println("Exiting FR this time...");
}
}
}
As per my comment: 3) You're creating more than one MainMenu object, one you add a listener to and the other you add to window. This looks to be a serious bug.
Instead create only one instance, set its state as needed (add listeners) and add it to the gui.
So change
window.add(new MainMenu());
To
window.add(mainMenu);
And again as per my prior comment, don't use == for String equality check but rather the .equals method.

Return null when closing JDialog

I have a table that has a JDialog for add new record, When i click to add Button and want to add a new record and JDialog opened, I close JDialog window and it returns null for my all columns of my table rows.
This is my JDialog constructor:
public class AddBookDialog extends JDialog implements ActionListener {
public AddBookDialog(JFrame owner) {
super(owner, "Add New Book", true);
initComponents();
saveBtn.addActionListener(this);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == cancelBtn) dispose();
else if (e.getSource() == saveBtn) saveAction();
}
}
public void saveAction() {
if (nameTf.getText().trim().length() != 0) {
if (!haveDigit(nameTf.getText().trim())) setBookName(nameTf.getText().trim());
else {
JOptionPane.showMessageDialog(null, "Book Name have digit");
return;
}
} else {
JOptionPane.showMessageDialog(null, "Enter Book Name");
return;
}
if (isbnTf.getText().trim().length() != 0) {
if (haveSpace(isbnTf.getText().trim()) || haveLetter(isbnTf.getText().trim())) {
JOptionPane.showMessageDialog(null, "Enter Correct ISBN");
return;
}
setIsbn(isbnTf.getText().trim());
} else {
JOptionPane.showMessageDialog(null, "Enter Book ISBN");
return;
}
setBorrowStatus("No");
setDate(dateGenerate());
dispose();
}
I try to control this problem in my table GUI class:
public class BookPage_Admin extends JFrame implements ActionListener {
...
public void addAction() {
AddBookDialog dialog = new AddBookDialog(this);
if (dialog.getBookName() != null && dialog.getIsbn() != null && dialog.getBorrowStatus() != null &&
dialog.getDate() != null) {
Object[] added = new Object[]{dialog.getBookID(), dialog.getBookName(), dialog.getIsbn(), dialog.getBorrowStatus(), dialog.getDate()};
model.addRow(added);
}
}
}
But still when i close it, it returns null for my row.
How to prevent returning null when close it?
Just in case if no one will answer you:
When you initiate Dialog, aka AddBookDialog dialog = new AddBookDialog(this); you can override ActionListener on the Frame side like:
AddBookDialog dialog = new AddBookDialog(this);
dialog.setModal(true);
dialog.getSaveBtn().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object[] added = new Object[]{dialog.getBookID(), dialog.getBookName(), dialog.getIsbn(), dialog.getBorrowStatus(), dialog.getDate()};
model.addRow(added);
}
});
// importent set visible after ActionListener!!
dialog.setVisible(true);
Hope it will help,

How to use KeyListener

I am currently trying to implement a keylistener in my program so that it does an action when I pressed an arrow key, the object in my program either moves left or right.
Here is the moving method in my program
public void moveDirection(KeyEvent e)
{
int move = 0;
int r = K.getRow();
int c = K.getCol();
if (e.getKeyCode() == 39) move = 1; //KeyEvent.VK_RIGHT
if (e.getKeyCode() == 37) move = 2; //KeyEvent.VK_LEFT
//if (e.getKeyCode() == KeyEvent.VK_DOWN) move = 3;
switch (move)
{
case 1: if (inBound(r, c+1))
K.setLocation(r ,c+1);
if (inBound(r, c-1) && frame2[r][c-1] == K)
frame2[K.getRow()][K.getCol()-1] = null;
break; //move right 39
case 2: K.setLocation(K.getRow(), K.getCol()-1); break; //move left 37
//case 3: b.setLocation(b.getRow()+1, b.getCol()); break; //move down
default: return;
}
processBlockList();
}
I am wondering how the program is supposed to read in (KeyEvent) e. I cannot really type in an arrowkey....
Please help!
edit: I also need to know what I need to add to my code so that my program waits about 700 milliseconds for a keyinput before moving on to another method
http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html
Check this tutorial
If it's a UI based application , then " I also need to know what I need to add to my code so that my program waits about 700 milliseconds for a keyinput before moving on to another method" you can use GlassPane or Timer class to fulfill the requirement.
For key Event:
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
dx = -1;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 1;
}
if (key == KeyEvent.VK_UP) {
dy = -1;
}
if (key == KeyEvent.VK_DOWN) {
dy = 1;
}
}
check this game example http://zetcode.com/tutorials/javagamestutorial/movingsprites/
Here is an SSCCE,
package experiment;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class KeyListenerTester extends JFrame implements KeyListener {
JLabel label;
public KeyListenerTester(String s) {
super(s);
JPanel p = new JPanel();
label = new JLabel("Key Listener!");
p.add(label);
add(p);
addKeyListener(this);
setSize(200, 100);
setVisible(true);
}
#Override
public void keyTyped(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
System.out.println("Right key typed");
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
System.out.println("Left key typed");
}
}
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
System.out.println("Right key pressed");
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
System.out.println("Left key pressed");
}
}
#Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
System.out.println("Right key Released");
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
System.out.println("Left key Released");
}
}
public static void main(String[] args) {
new KeyListenerTester("Key Listener Tester");
}
}
Additionally read upon these links : How to Write a Key Listener and How to Use Key Bindings
The class which implements KeyListener interface becomes our custom key event listener. This listener can not directly listen the key events. It can only listen the key events through intermediate objects such as JFrame. So
Make one Key listener class as
class MyListener implements KeyListener{
// override all the methods of KeyListener interface.
}
Now our class MyKeyListener is ready to listen the key events. But it can not directly do so.
Create any object like JFrame object through which MyListener can listen the key events. for that you need to add MyListener object to the JFrame object.
JFrame f=new JFrame();
f.addKeyListener(new MyKeyListener);
In addition to using KeyListener (as shown by others' answers), sometimes you have to ensure that the JComponent you are using is Focusable. This can be set by adding this to your component(if you are subclassing):
#Override
public void setFocusable(boolean b) {
super.setFocusable(b);
}
And by adding this to your constructor:
setFocusable(true);
Or, if you are calling the function from a parent class/container:
JComponent childComponent = new JComponent();
childComponent.setFocusable(true);
And then doing all the KeyListener stuff mentioned by others.

Main menu for a game

I'm a beginner trying to create a game. The game is empty, I didn't start to write it.
My problem: when I click in the start button I want to start the game in the same frame, but with this code when I'll click on start the program opens another frame with the menu, and in the original frame will delete the panel.
How can I do this simple menu?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import panelimage.Image;
public class Project0 extends JFrame{
private static final long serialVersionUID = 1L;
private Game JPanelGame;
private Image MainMenu = new Image(new File("src/img/menu_background.jpg"));
JButton start = new JButton("START GAME"), exit = new JButton ("LEAVE GAME");
public Project0() {
super();
initFrame();
initMenu();
this.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
JPanelGame.keyPressed(evt);
}
public void keyReleased(KeyEvent evt) {
JPanelGame.keyReleased(evt);
}
});
}
private void initFrame() {
setResizable(false);
setBounds(new Rectangle(400, 400, 600, 400));
MainMenu.setLayout(new BorderLayout());
}
private void initMenu() {
initButtons();
MainMenu.setLayout(null);
MainMenu.add(start);
MainMenu.add(exit);
add(MainMenu);
setContentPane(MainMenu);
setTitle("Project0");
}
private void initButtons() {
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
JPanelGame = new Game();
remove(MainMenu);
add(JPanelGame, BorderLayout.CENTER);
setContentPane(JPanelGame);
invalidate(); validate();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Project0 Game = new Project0();
Game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Game.setVisible(true);
}
});
}
});
start.setLocation(225,100);
start.setSize(150,50);
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
exit.setLocation(225,200);
exit.setSize(150,50);
}
public static void main(String[] args) {
Project0 Game = new Project0();
Game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Game.setVisible(true);
}
}
and this frame will start
import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.*;
import java.io.File;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import highscores.*;
public class Game extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
Thread game;
BufferedImage background;
HighscoreManager scores = new HighscoreManager();
public Game(){
super(true);
try{
background = ImageIO.read(new File(""));
}catch(Exception e) {}
game=new Thread(this);
game.start();
}
public void paintComponent(Graphics graphic2d){
setOpaque(false);
super.paintComponent(graphic2d);
graphic2d.drawImage(background, 0, 0, null);
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
}
if (key == KeyEvent.VK_RIGHT) {
}
if (key == KeyEvent.VK_UP) {
}
if (key == KeyEvent.VK_DOWN) {
}
if (key == KeyEvent.VK_A) {
}
if (key == KeyEvent.VK_D) {
}
if (key == KeyEvent.VK_W) {
}
if (key == KeyEvent.VK_S) {
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
}
if (key == KeyEvent.VK_RIGHT) {
}
if (key == KeyEvent.VK_UP) {
}
if (key == KeyEvent.VK_DOWN) {
}
if (key == KeyEvent.VK_A) {
}
if (key == KeyEvent.VK_D) {
}
if (key == KeyEvent.VK_W) {
}
if (key == KeyEvent.VK_S) {
}
}
public void run(){
try {Thread.sleep(50);}catch(InterruptedException ex){}
}
}
I see two options, depending on your needs. Either you just remove all components from the frame and add the new contents. But by doing so, you loose your 'menu-frame'. The other option (and IMO the best of the two) is to opt for a CardLayout, where one card is your menu and the other your game. The 'start' button would then simply switch from the 'menu-card' to the 'game-card' (and if needed start the game). In your game, you can have a button/key/... which does the inverse (switching from the game card to the menu card)
I was going through your code but I gave up (certainly since #orzechowskid already pointed out what you need to adjust). A few tips in case you are going to post more code on this site:
Read the SSCCE.org website and try to post code as described there
Follow the Java naming conventions (e.g. classes start with an uppercase letter, variables with a lowercase letter)
Do not give your own classes the same names as standard JDK classes. Makes it very hard for someone not familiar with your code (but probably rather familiar with the JDK classes) to read the code. E.g. it took me some time before I realized you have your own panelimage.Image class which has nothing to do with the java.awt.Image class
And then a few Swing related tips based on what I saw of your code:
Learn how to use LayoutManagers and get rid of the null layouts. The Visual guide to layout managers might be a good starting point
Swing is designed to work with KeyBindings and not with KeyListeners. See the tutorial
Be careful with the Thread.sleep usage and running multiple Threads in combination with a Swing UI. Make sure you are aware of the Swing threading rules
when you call SwingUtilities.invokeLater() inside your button's action listener, you're creating a second Project0 object. And whenever you create a Project0, you get a JFrame drawn on screen with a main menu inside of it. Remove the call to SwingUtilities.invokeLater() and you should be left with a single frame.

Remove key listener from panel

Is it possible to clear a listener that I put on my JPanel? When I call a method, I put a KeyListener on the panel but when I quit this method, I want to clear that listener.
Here is my method :
private void stopBall(final Graphics2D g2, int posBallY, String winner) {
move = false;
scorePanel.showPressSpace(true);
setFocusable(true);
requestFocus();
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e){
if (e.getKeyCode() == KeyEvent.VK_SPACE){
setPosX(getPlayPanelWidth()/2);
setPosY(0);
move = true;
scorePanel.showPressSpace(false);
initBall(g2);
}
}});
if (winner == "player1") {
scoreCountPlayer1++;
scorePanel.getLab_Player1().setText("" + scoreCountPlayer1);
} else if (winner == "comp") {
scoreCountComputer++;
scorePanel.getLab_Computer().setText("" + scoreCountComputer);
}
}
You have an unqualified call to addKeyListener(KeyListener), so I presume that you've extended JPanel. If so, then you can call removeKeyListener(KeyListener). In your current code, your key listener is anonymous. You'll need to change it just a bit to hold on to that reference, like so:
// Create a variable holding the listener
KeyAdapter keyAdapter = new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_SPACE)
{
setPosX(getPlayPanelWidth() / 2);
setPosY(0);
move = true;
scorePanel.showPressSpace(false);
initBall(g2);
}
}
};
// Register the listener with this JPanel
addKeyListener(keyAdapter);
// Time passes...
// Remove the listener from this JPanel
removeKeyListener(keyAdapter);
Or the way with the absolute minimal adjustments to your code
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e){
if (e.getKeyCode() == KeyEvent.VK_SPACE){
((JPanel)e.getSource()).removeKeyListener( this );
setPosX(getPlayPanelWidth()/2);
setPosY(0);
move = true;
scorePanel.showPressSpace(false);
initBall(g2);
}
}
});
But as already stated in the comments, you should opt for keybindings instead of keylisteners

Categories