I am writing a Pokedex in Java Swing and I plan to have each Pokemon represented by a String in a JList Object. Upon the user clicking a Pokemon, I want it to open a new tabbed Panel within the main frame that holds all the information for that specific Pokemon sorted with Categories. But I can't figure out how to get the String to implement an action. I am willing to change the type of list if that fixes the problem. My current code is below:
//this is an intermediate Pokedex application for windows. the code for the application is below.
//import the needed jlibraries for the program
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.Color;
import java.awt.event.*;
import javax.swing.UIManager.*;
import javax.swing.border.*;
import javax.imageio.*;
import java.io.*;
import java.lang.*;
import javax.swing.JRadioButtonMenuItem.*;
import java.util.*;
import javax.swing.JOptionPane;
//everything below is the pokedex class
public class Pokedex extends JFrame
{
String[] gen1 = {"Bulbasaur", "Ivysaur"};
JList<String> gen1List = new JList<String>(gen1);
JScrollPane browsePane = new JScrollPane(gen1List);
JPanel topIndexPanel = new JPanel();
int intX;
public static void main(String[] args)
{
Toolkit.getDefaultToolkit().setDynamicLayout(true);
System.setProperty("sun.awt.noerasebackground", "true");
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
try
{
UIManager.setLookAndFeel("com.jtattoo.plaf.noire.NoireLookAndFeel");
}
catch (ClassNotFoundException ex)
{
java.util.logging.Logger.getLogger(Pokedex.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
catch (InstantiationException ex)
{
java.util.logging.Logger.getLogger(Pokedex.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
catch (IllegalAccessException ex)
{
java.util.logging.Logger.getLogger(Pokedex.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
catch (javax.swing.UnsupportedLookAndFeelException ex)
{
java.util.logging.Logger.getLogger(Pokedex.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
Pokedex f = new Pokedex();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
f.setBounds(0,0,screenSize.width, screenSize.height);
f.setVisible(true);
f.setTitle("Pokedex");
}
public Pokedex()
{
add(browsePane, BorderLayout.WEST);
gen1List.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
gen1List.setBackground(Color.BLACK);
gen1List.setForeground(Color.WHITE);
gen1List.setFont(new Font("Arial", Font.PLAIN, 18));
addWindowListener(
new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
String selected = gen1List.getSelectedValue().toString();
System.out.println(selected);
}
}
EDIT
I was able to create a work Around with the Below code and with a couple of modifications it will do what i need.
gameRedList.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent arg0)
{
if (!arg0.getValueIsAdjusting())
{
actionString = gameRedList.getSelectedValue().toString();
if (actionString=="Bulbasaur")
{
JFrame testFrame = new JFrame();
testFrame.setBounds(0,0,500,500);
testFrame.setBackground(Color.BLACK);
testFrame.setForeground(Color.WHITE);
testFrame.setTitle("Success");
testFrame.setVisible(true);
}
}
}
});
You can't assume the user will click on a Pokémon. The user could use the mouse or the keyboard. To be notified when an item in a JList is selected you would use a
ListSelectionListener
. Read the section from the Swing tutorial on Selecting Items in a List for more information and a working examplesYou probably don't want to open a new tabbed panel. Instead you create GUI with your component. Then as the selection is changed you just update the data displayed. That is, when designing a GUI you avoid adding/removing components.
Also you don't need the WindowListener. That is old AWT code. In Swing you can just use: