I'm trying to add a Jpanel to a JscrollBAr but when i run java it's not working. It's a RadioButton with a question. Can you help me please ?
this is my class jpanel :
public class yesnoPanel extends JPanel {
private JPanel quest;
private JPanel answ;
public yesnoPanel(String q){
JPanel pan = new JPanel();
JRadioButton yes = new JRadioButton("yes");
JRadioButton no = new JRadioButton("no");
ButtonGroup bg = new ButtonGroup();
JTextArea question = new JTextArea(1,20);
question.setText(q);
JPanel pan2 = new JPanel();
pan2.add(question);
bg.add(yes);
bg.add(no);
pan.add(yes);
pan.add(no);
this.quest=pan2;
this.answ=pan;
}
I can show them when i use JFrame but not with Jscrollbar
This is my main with the jscrollbar :
public static void main(String[] args) {
JFrame fram = new JFrame();
yesnoPanel question = new yesnoPanel("are you ok");
JPanel q = question.getQuestion();
JPanel a = question.getAnsw();
JPanel all = new JPanel();
all.add(q);
all.add(a);
yesnoPanel question2 = new yesnoPanel("sure ?");
JPanel q2 = question2.getQuestion();
q2.setSize(200,500);
JPanel a2 = question2.getAnsw();
JPanel all2 = new JPanel();
all2.add(q2,BorderLayout.WEST);
all2.add(a2);
JScrollPane scroll = new JScrollPane();
scroll.setPreferredSize(new Dimension(100,100));
scroll.setViewportView(all);
scroll.setVisible(true);
}
thanks to all
Replace your last few lines in main method with this to make it work.
JPanel master = new JPanel();
master.add(all);
master.add(all2);
JScrollPane scroll = new JScrollPane(master);
scroll.setPreferredSize(new Dimension(100,100));
scroll.setViewportView(all);
scroll.setVisible(true);
fram.add(scroll);
fram.pack();
fram.setVisible(true);
fram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Related
As stated in the title i need to move the label for the text box to be above the box and not to the side. attached i have a picutres of what i mean. what i have vs what i want i have tried searching for it but i cannot seem to find the answer im looking for/not exactly sure what to look up. I have tried using JFrame but it made a separate window unless i need to make the entire GUI a JFrame for me to get the result i want?
Also the actionPerformed method has things but it is irrelevant to the question but displays correctly still.
import java.awt.event.\*;
import javax.swing.\*;
import java.text.DecimalFormat;
public class Project4 extends JFrame implements ActionListener {
private JTextArea taArea = new JTextArea("", 30, 20);
ButtonGroup group = new ButtonGroup();
JTextField name = new JTextField(20);
boolean ch = false;
boolean pep = false;
boolean sup = false;
boolean veg = false;
DecimalFormat df = new DecimalFormat("##.00");
double cost = 0.0;
public Project4() {
initUI();
}
public final void initUI() {
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
getContentPane().add(panel1, "North");
getContentPane().add(panel2, "West");
getContentPane().add(panel3, "Center");
getContentPane().add(panel4, "East");
panel4.setLayout(new BoxLayout(panel4, BoxLayout.Y_AXIS));
getContentPane().add(panel5, "South");
JButton button = new JButton("Place Order");
button.addActionListener(this);
panel5.add(button);
JButton button2 = new JButton("Clear");
button2.addActionListener(this);
panel5.add(button2);
panel3.add(taArea);
JCheckBox checkBox1 = new JCheckBox("Cheese Pizza") ;
checkBox1.addActionListener(this);
panel4.add(checkBox1);
JCheckBox checkBox2 = new JCheckBox("Pepperoni Pizza");
checkBox2.addActionListener(this);
panel4.add(checkBox2);
JCheckBox checkBox3 = new JCheckBox("Supreme Pizza");
checkBox3.addActionListener(this);
panel4.add(checkBox3);
JCheckBox checkBox4 = new JCheckBox("Vegetarian Pizza");
checkBox4.addActionListener(this);
panel4.add(checkBox4);
JRadioButton radioButton1 = new JRadioButton("Pick Up");
group.add(radioButton1);
radioButton1.addActionListener(this);
panel1.add(radioButton1);
JRadioButton radioButton2 = new JRadioButton("Delivery");
group.add(radioButton2);
radioButton2.addActionListener(this);
panel1.add(radioButton2);
JLabel name_label = new JLabel("Name on Order");
name.addActionListener(this);
panel5.add(name_label);
panel5.add(name);
setSize(600, 300);
setTitle("Pizza to Order");
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent action) {
}
public static void main(String[] args) {
Project4 ex = new Project4();
ex.setVisible(true);
}
}
You can use a nested JPanel with another layout in order to achieve that. I would go with BorderLayout here. You can also other layouts that allow vertical orientation. Visiting the visual guide to Layout Managers will help you spot them.
JLabel name_label = new JLabel("Name on Order");
name.addActionListener(this);
JPanel verticalNestedPanel = new JPanel(new BorderLayout());
verticalNestedPanel.add(name_label, BorderLayout.PAGE_START);
verticalNestedPanel.add(name, BorderLayout.PAGE_END);
panel5.add(verticalNestedPanel);
I'm a third grade computer engineer student and I'm trying to do a game project. I added a background image to my JFrame. And I tried to make the other panels transparent which I added to frame. I use alpha value for this, Ex: new Color(0,0,0,125). I olso use cardLayout in my program and for every calling a new segment or new page at the center panel; alphavalue takes the whole panels transparency and implement it to the selected panel and it creates a problem. Example : I have 7 buttons at left panel and when I click crimes button, crime panel comes to the center panel and left panel comes inside of center panel with buttons again(transparently).
I have 16 classes, so I only added main class.
Sorry for bad grammar. I hope you can understand me and help me.
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class TheMafia {
public static ImageIcon scale(ImageIcon i,int x,int y) {
Image img = i.getImage();
Image newimg = img.getScaledInstance(x,y,Image.SCALE_SMOOTH);
i = new ImageIcon(newimg);
return i;
}
public static void setButton (JButton b,int x,int y) {
b.setPreferredSize(new Dimension(x,y));
b.setBackground(Color.gray);
b.setForeground(Color.white);
b.setBorder(new LineBorder(Color.black,1));
b.setFont(new Font("Serif",Font.BOLD,18));
}
public static void main(String[] args) {
ImageIcon home2 = new ImageIcon("home.jpg");
home2 = scale(home2,1366,768);
JFrame theMafia = new JFrame();
theMafia.setTitle("The Mafia Game - Best game in the world!");
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
theMafia.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theMafia.setContentPane(new JLabel(home2));
theMafia.setLayout(new BorderLayout());
//theMafia.setLayout(new FlowLayout());
//theMafia.setExtendedState(JFrame.MAXIMIZED_BOTH);
theMafia.setSize(800,700);
theMafia.setLocationRelativeTo(null);
theMafia.setVisible(true);
p1.setBackground(new Color(0,0,0,35));
p2.setBackground(new Color(0,0,0,65));
p1.setPreferredSize(new Dimension(250,150));
p2.setPreferredSize(new Dimension(250,150));
//theMafia.add(p1);
//theMafia.add(p2);
// kullanıcı oluşturuldu
User u1 = new User();
// suçlar oluşturuldu
Crime c1 = new Crime();
c1.setName("Yaşlı Kadın");
c1.setDifficulty(5);
c1.setStrength(1);
c1.setMoney(11);
Crime c2 = new Crime();
c2.setName("Dükkan Hırsızlığı");
c2.setDifficulty(10);
c2.setStrength(3);
c2.setMoney(67);
Crime c3 = new Crime();
c3.setName("Araba Hırsızlığı");
c3.setDifficulty(20);
c3.setStrength(6);
c3.setMoney(133);
// suçun seçilmesi
final JPanel crimes = new JPanel(new CardLayout());
//crimes.setBackground(new Color(0,0,0,65));
ImageIcon suçişle = new ImageIcon("suçişle.jpg");
suçişle = scale(suçişle,50,50);
JButton yap = new JButton("Suçu işle!",suçişle);
setButton(yap,100,65);
JPanel crime1 = new JPanel(new GridLayout(2,1));
crime1.setBackground(new Color(0,0,0,35));
crime1.setForeground(Color.white);
JLabel crime1Info = new JLabel("Suç : "+c1.getName()+"\n Para : "+c1.getMoney()+"\n Yapabilme ihtimali : "+c1.getCapable()+"\n Güç : "+c1.getStrength());
crime1Info.setFont(new Font("Serif",Font.BOLD,15));
crime1.add(crime1Info);
crime1.add(yap);
JPanel crime2 = new JPanel(new GridLayout(2,1));
crime2.setBackground(new Color(0,0,0,35));
crime2.setForeground(Color.white);
JLabel crime2Info = new JLabel("Suç : "+c2.getName()+"\n Para : "+c2.getMoney()+"\n Yapabilme ihtimali : "+c2.getCapable()+"\n Güç : "+c2.getStrength());
crime2Info.setFont(new Font("Serif",Font.BOLD,15));
crime2.add(crime2Info);
crime2.add(yap);
JPanel crime3 = new JPanel();
crime3.setBackground(new Color(0,0,0,35));
crime3.setForeground(Color.white);
JLabel crime3Info = new JLabel("Suç : "+c3.getName()+"\n Para : "+c3.getMoney()+"\n Yapabilme ihtimali : "+c3.getCapable()+"\n Güç : "+c3.getStrength());
crime2Info.setFont(new Font("Serif",Font.BOLD,15));
crime3.add(crime3Info);
crime3.add(yap);
crimes.add(crime1,c1.getName());
crimes.add(crime2,c2.getName());
crimes.add(crime3,c3.getName());
String crimesNames [] = {c1.getName(),c2.getName(),c3.getName()};
JComboBox crimesbox = new JComboBox(crimesNames);
crimesbox.setEditable(false);
crimesbox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent evt) {
CardLayout cl = (CardLayout) (crimes.getLayout());
cl.show(crimes,(String)evt.getItem());
}
});
// menu
final JPanel menus = new JPanel(new CardLayout());
//menus.setBackground(new Color(0,0,0,35));
// crime
JPanel crime = new JPanel(new BorderLayout());
crime.setBackground(new Color(0,0,0,35));
crime.add(crimesbox,BorderLayout.PAGE_START);
crime.add(crimes,BorderLayout.SOUTH);
ImageIcon crimeimage = new ImageIcon("thief.png");
crimeimage = scale(crimeimage,50,50);
final JButton crimeButton = new JButton("Suçlar",crimeimage);
setButton(crimeButton,178,76);
crimeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
CardLayout cl = (CardLayout) (menus.getLayout());
if (e.getSource() == crimeButton) {
cl.show(menus,"suç");
}
}
});
// weapon shop
JPanel weaponShop = new JPanel();
//weaponShop.setBackground(new Color(0,0,0,125));
final JButton weaponShopButton = new JButton("Silah Dükkanı");
setButton(weaponShopButton,178,76);
// building
JPanel buildingPanel = new JPanel();
//buildingPanel.setBackground(new Color(0,0,0,125));
final JButton buildingButton = new JButton("Binalar");
setButton(buildingButton,178,76);
// nightlife
JPanel nightLife = new JPanel();
//nightLife.setBackground(new Color(0,0,0,35));
final JButton nightLifeButton = new JButton("Gece Hayatı");
setButton(nightLifeButton,178,76);
// treatment center
JPanel treatmentCenter = new JPanel();
//treatmentCenter.setBackground(new Color(0,0,0,35));
final JButton treatmentCenterButton = new JButton("Tedavi Merkezi");
setButton(treatmentCenterButton,178,76);
// casino
JPanel casinoPanel = new JPanel();
//casinoPanel.setBackground(new Color(0,0,0,35));
final JButton casinoButton = new JButton("Gazino");
setButton(casinoButton,178,76);
// home page
JPanel home = new JPanel();
home.setBackground(new Color(0,0,0,35));
ImageIcon homeimage = new ImageIcon("home.jpg");
homeimage = scale(homeimage,1200,800);
JLabel homelabel= new JLabel();
home.add(homelabel);
ImageIcon homeicon = new ImageIcon("home_icon.png");
homeicon = scale(homeicon,50,50);
final JButton homeButton = new JButton("Home",homeicon);
setButton(homeButton,178,76);
homeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
CardLayout cl = (CardLayout) (menus.getLayout());
if (e.getSource() == homeButton) {
cl.show(menus,"home");
}
}
});
menus.add(home,"home");
menus.add(crime,"suç");
menus.add(weaponShop,"silahDükkanı");
menus.add(buildingPanel,"bina");
menus.add(nightLife,"geceHayatı");
menus.add(treatmentCenter,"TedaviMerkezi");
menus.add(casinoPanel,"gazino");
Color grisi=new Color(13,13,13);
JPanel menusButton = new JPanel(new GridLayout(10,1));
//menusButton.setBackground(grisi);
menusButton.add(homeButton);
menusButton.add(crimeButton);
menusButton.add(weaponShopButton);
menusButton.add(buildingButton);
menusButton.add(nightLifeButton);
menusButton.add(treatmentCenterButton);
menusButton.add(casinoButton);
menusButton.setOpaque(false);
theMafia.add(menusButton,BorderLayout.WEST);
theMafia.add(menus,BorderLayout.CENTER);
}
}
Swing does not handle transparent background properly. Swing expects components to be either opaque or non-opaque and the transparency causes a problem because the component is neither.
Check out Background With Transparency for more information and a couple of solutions to solve the problem.
UPDATE UPDATE UPDATE
thank you :))) I did What u told me
I put frame.add(FirstScreen) first
they appeared .....
but now the events are not working , why???????
Can u help me again???
I'm sorry ........
..................
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class InterFace extends JFrame implements ActionListener,ItemListener
{
JFrame frame = new JFrame("Al-murshed Dictionary");
JPanel FirstScreen = new JPanel();
JPanel SecondScreen = new JPanel();
JPanel ThirdScreen = new JPanel();
JPanel ForthScreen = new JPanel();
JButton Translate = new JButton ("Translate");
JButton About = new JButton ("About");
JButton Help= new JButton ("Help");
JButton Quit= new JButton ("Quit");
JButton Quit1= new JButton ("Quit");
JButton Quit2= new JButton ("Quit");
JButton Back= new JButton ("Back");
JButton Back1= new JButton ("Back");
JTextField WordField = new JTextField("Write Your Word Here",50);
JTextArea ArbField = new JTextArea(40,40);
JTextArea EngField = new JTextArea(40,40);
CardLayout c1 = new CardLayout ();
public InterFace()
{
FirstScreen.setLayout(c1);
SecondScreen.add(WordField);
SecondScreen.add(Translate);
ThirdScreen.add(Back);
ForthScreen.add(Back1);
ThirdScreen.add(Quit1);
ForthScreen.add(Quit2);
FirstScreen.add(SecondScreen,"1");
FirstScreen.add(ThirdScreen,"2");
FirstScreen.add(ForthScreen,"3");
JPanel controlButtons = new JPanel();
controlButtons.add(Help);
controlButtons.add(About);
controlButtons.add(Quit);
JPanel wordTranslate = new JPanel();
wordTranslate.add(WordField);
wordTranslate.add(Translate);
JPanel controlTextArea = new JPanel();
controlTextArea.add(EngField);
controlTextArea.add(ArbField);
c1.show(FirstScreen,"1");
About.addActionListener(this);
Back.addActionListener(this);
Help.addActionListener(this);
Back1.addActionListener(this);
Quit.addActionListener(this);
Quit1.addActionListener(this);
Quit2.addActionListener(this);
frame.add(FirstScreen);
Container pane = frame.getContentPane();
pane.add(wordTranslate, BorderLayout.NORTH);
pane.add(controlTextArea, BorderLayout.CENTER);
pane.add(controlButtons, BorderLayout.PAGE_END);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
//EventHandler
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==About)
c1.show(FirstScreen,"2");
if(e.getSource()==Help)
c1.show(FirstScreen,"3");
if(e.getSource()==Quit)
System.exit(0);
if(e.getSource()==Quit1)
System.exit(0);
if(e.getSource()==Quit2)
System.exit(0);
if(e.getSource()==Back)
c1.show(FirstScreen,"1");
if(e.getSource()==Back1)
c1.show(FirstScreen,"1");
}
public static void main (String args[])
{
InterFace d = new InterFace ();
}
}
pane.add(controlTextArea, BorderLayout.CENTER);
...
frame.add(FirstScreen);
First you add the text area panel to the content pane.
Then you add the "FirstScreen" to the frame.
The problem is that when you add the "FirstScreen" to the frame you are really adding it to the content pane of the frame. So basically you are replacing the text area panel with the first screen.
Also, follow Java naming conventions. Variable names should NOT start with an upper case character.
I have some problem , i want my jpanel to look like this
but after codING i had this view and i didnt find the solution ,
i need some helps please.
i put the radio button inside jScrollPan
//listmateriel is a list (vector) of radio button ----------
//imagebox is jpanel contains jradio buttons and the image ---------
//gridlayoutPanel2 is Jpanel contains the two text fields and jlabel and a JComboBox
package tpjava;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionEvent;
i.......
public class Frame extends JFrame implements ActionListener {
private static final int Haut = 550;
private static final int Larg = 720;
private final JPanel utilisateur;
private final JButton recherche;
private final JRadioButton radioButton,radioButton2;
private Vector listmateriel;
private final JLabel label;
public Frame()
{
super("Gestion des materiels");
setSize(Larg,Haut);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane menu = new JTabbedPane();
JPanel emprunt = new JPanel();
emprunt.setPreferredSize(new Dimension(Larg, Haut ));
emprunt.setLayout(new BoxLayout(emprunt, BoxLayout.Y_AXIS));
emprunt.setBorder(BorderFactory.createEmptyBorder(20,20, 20, 20));
JPanel emp1 = new JPanel();
emp1.setPreferredSize(new Dimension(Larg-40, 200 ));
emp1.setLayout(new BorderLayout());
JLabel title2 = new JLabel("Nouvel emprunt:");
title2.setBorder(BorderFactory.createEmptyBorder(20,20,0,0));
JPanel box2 = new JPanel();
box2.setPreferredSize(new Dimension(Larg, 40 ));
box2.setLayout(new BoxLayout(box2, BoxLayout.X_AXIS));
box2.add(title2);
box2.add(Box.createHorizontalStrut(650));
emprunt.add(box2);
JLabel label3 = new JLabel("test");
JButton dispo = new JButton("Disponibilité");
dispo.addActionListener((ActionListener) this);
dispo.setPreferredSize(new Dimension(200, 30));
JPanel box1 = new JPanel();
box1.setLayout(new BoxLayout(box1, BoxLayout.X_AXIS));
box1.add(dispo);
box1.add(Box.createHorizontalStrut(450));
box1.add(label3);
emp1.add(box1,BorderLayout.SOUTH);
listmateriel = new Vector();
for(int k =0 ;k <10 ; k++)
listmateriel .add(new JRadioButton(res.getString("nom"+k)));
......
ImageIcon newIcon = new ImageIcon(bi);
JLabel picture = new JLabel(newIcon);
picture.setBorder(BorderFactory.createEmptyBorder(0,450,0,0));
JPanel jplRadio = new JPanel();
jplRadio.setLayout(new GridLayout(0, 1));
for(int i = 0 ; i < listmateriel.size() ;i++)
{
jplRadio.add((Component) listmateriel.elementAt(i));
}
JPanel imagebox = new JPanel();
imagebox.setLayout(new BorderLayout());
JScrollPane scroll3 = new JScrollPane(jplRadio);
scroll3.setPreferredSize(new Dimension(200, 30));
imagebox.add(scroll3, BorderLayout.WEST);
imagebox.add(picture, BorderLayout.CENTER);
emp1.add(imagebox,BorderLayout.CENTER);
JPanel box3 = new JPanel();
box3.setLayout(new BoxLayout(box3, BoxLayout.X_AXIS));
JLabel title3 = new JLabel("Remplir les champs suivants:");
box3.add(title3);
box3.add(Box.createHorizontalStrut(650));
JPanel gridlayoutPanel2 = new JPanel();
........
JPanel util5 = new JPanel();
util5.setLayout(new GridLayout(0,1));
util5.setBorder(BorderFactory.createLineBorder(Color.black));
JLabel resul2 = new JLabel("bsfgbsdg");
JScrollPane scroll2 = new JScrollPane();
util5.add(resul2);
emprunt.add(emp1);
emprunt.add(box3);
emprunt.add(gridlayoutPanel2);
emprunt.add(util5);
menu.add("emprunts",emprunt);
//---------------------
getContentPane().add(menu);
pack();
setVisible(true);
}
}
GridLayout(0, 1) instead of BoxLayout is the solution thank you #trashgod
I'm currently working on a program which I've asked questions involving previously.
I've made a good amount of progress since then, and now I'm ready to begin on the infoPanel portion of the client program. I've read through the documentation and other questions, but I'm a still a bit confused. How do I setup the panel to, y'know, work? How do I link it with the JList(If that's even possible to use a JList instead of a combo box), and then how do I establish which selection references which card?
Thank you so much for any assistance!
Source:
import javax.swing.*;
import java.util.*;
import java.awt.*;
public class ClientApp extends JFrame
{
public static void main(String[] args)
{
new ClientApp();
}
public ClientApp()
{
this.setSize(320,380);
this.setTitle("Honeydukes Candy Order");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
JPanel infoPanel = new JPanel(new CardLayout());
JPanel invntryPanel = new JPanel();
String[] candy = {"Acid Pops", "Bat's Blood Soup",
"Bertie Bott's Every Flavour Beans",
"Blood-flavoured Lollipops",
"Cauldron Cakes", "Charm Choc",
"Chocoballs", "Chocolate Cauldrons",
"Chocolate Frogs", "Chocolate Skeletons",
"Chocolate Wands", "Choco-Loco", "Cockroach Clusters",
"Nougat", "Crystallised Pineapple",
"Drooble's Best Blowing Gum", "Exploding Bonbons",
"Toffees", "Fizzing Whizzbees",
"Fudge Flies", "Ice Mice",
"Jelly Slugs", "Liquourice Wands",
"Pepper Imps", "Peppermint Toads",
"Pink Coconut Ice", "Pixie Puffs",
"Pumpkin Fizz", "Salt Water Taffy",
"Shock-o-Choc", "Skeletal Sweets",
"Splindle's Lick'O'Rish Spiders",
"Sugar Quills", "Sugared Butterfly Wings",
"Toothflossing Stringmints", "Tooth-Splintering Strongmints",
"Treacle Fudge", "Chocolates", "Wizochoc"};
JList candyList = new JList(candy);
candyList.setVisibleRowCount(18);
candyList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scroll = new JScrollPane(candyList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
invntryPanel.add(scroll);
JPanel startCard = new JPanel();
JPanel acidPopsCard = new JPanel();
JPanel batsBloodSoupCard = new JPanel();
JPanel bertieBottsCard = new JPanel();
JPanel bloodPopsCard = new JPanel();
JPanel cauldronCakesCard = new JPanel();
JPanel charmChocCard = new JPanel();
JPanel chocoballsCard = new JPanel();
JPanel chocCauldronsCard = new JPanel();
JPanel chocFrogsCard = new JPanel();
JPanel chocSkeleCard = new JPanel();
JPanel chocWands = new JPanel();
JPanel chocolocoCard = new JPanel();
JPanel roachClustersCard = new JPanel();
JPanel nougatCard = new JPanel();
JPanel crystalPineappleCard = new JPanel();
JPanel droobleGumCard = new JPanel();
JPanel explodeBonbonsCard = new JPanel();
JPanel toffeesCard = new JPanel();
JPanel fizzWhizCard = new JPanel();
JPanel fudgeFliesCard = new JPanel();
JPanel iceMiceCard = new JPanel();
JPanel jellySlugsCard = new JPanel();
JPanel liquorWandsCard = new JPanel();
JPanel pepImpsCard = new JPanel();
JPanel pinkCocoIceCard = new JPanel();
JPanel pixiePuffsCard = new JPanel();
JPanel pumpkFizzCard = new JPanel();
JPanel saltTaffeyCard = new JPanel();
JPanel shockChocCard = new JPanel();
JPanel skeleSweetsCard = new JPanel();
JPanel spindleSpidersCard = new JPanel();
JPanel sugarQuillsCard = new JPanel();
JPanel sugarWingsCard = new JPanel();
JPanel flossMintsCard = new JPanel();
JPanel splintMintsCard = new JPanel();
JPanel treacleFudgeCard = new JPanel();
JPanel chocolatesCard = new JPanel();
JPanel wizochocCard = new JPanel();
infoPanel.add(startCard);
infoPanel.add(acidPopsCard);
infoPanel.add(batsBloodSoupCard);
infoPanel.add(bertieBottsCard);
infoPanel.add(bloodPopsCard);
infoPanel.add(cauldronCakesCard);
infoPanel.add(charmChocCard);
infoPanel.add(chocoballsCard);
infoPanel.add(chocCauldronsCard);
infoPanel.add(chocFrogsCard);
infoPanel.add(chocSkeleCard);
infoPanel.add(chocWands);
infoPanel.add(chocolocoCard);
infoPanel.add(roachClustersCard);
infoPanel.add(nougatCard);
infoPanel.add(crystalPineappleCard);
infoPanel.add(droobleGumCard);
infoPanel.add(explodeBonbonsCard);
infoPanel.add(toffeesCard);
infoPanel.add(fizzWhizCard);
infoPanel.add(fudgeFliesCard);
infoPanel.add(iceMiceCard);
infoPanel.add(jellySlugsCard);
infoPanel.add(liquorWandsCard);
infoPanel.add(pepImpsCard);
infoPanel.add(pinkCocoIceCard);
infoPanel.add(pixiePuffsCard);
infoPanel.add(pumpkFizzCard);
infoPanel.add(saltTaffeyCard);
infoPanel.add(shockChocCard);
infoPanel.add(skeleSweetsCard);
infoPanel.add(spindleSpidersCard);
infoPanel.add(sugarQuillsCard);
infoPanel.add(sugarWingsCard);
infoPanel.add(flossMintsCard);
infoPanel.add(splintMintsCard);
infoPanel.add(treacleFudgeCard);
infoPanel.add(chocolatesCard);
infoPanel.add(wizochocCard);
this.add(invntryPanel, BorderLayout.WEST);
this.add(infoPanel, BorderLayout.EAST);
this.setVisible(true);
}
}
I would add a ListSelectionListener to the JList, and inside of that listener change the card displayed by the CardLayout.
You really need to read the tutorial on how to use CardLayout first though as there you'd see that your add method is wrong. Consider using the Strings held by the JList as the constant that you'd use when adding components to the CardLayout-using panel.