Java - Why i can't see my list in JFrame? - java

I can't see my list under the MenuBar, it should show me a list (playing cards)
What i should do ?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Joc extends JFrame{
JMenuBar mb = new JMenuBar();
JMenu m1 = new JMenu("Optiuni");
JMenuItem mi1 = new JMenuItem("Amesteca");
JMenuItem mi2 = new JMenuItem ("Inchide");
DefaultListModel<Card> model = new DefaultListModel<Card>();
JList<Card> list = new JList<Card>();
JScrollPane jsp = new JScrollPane(list);
Deck d = new Deck ();
public Joc() {
super("Joc");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setJMenuBar(mb);
add(jsp);
mb.add(m1);
m1.add(mi1);
m1.add(mi2);
ArrayList<Card> carti = d.getCards();
for (Card c: carti) {
model.addElement(c);
}
mi1.addActionListener (
new ActionListener() {
public void actionPerformed (ActionEvent ev) {
d.amesteca();
ArrayList<Card> carti = d.getCards();
model.clear();
for (Card c: carti) {
model.addElement(c);
}
}
}
);
mi2.addActionListener (
new ActionListener() {
public void actionPerformed (ActionEvent ev) {
System.exit(0);
}
}
);
setSize(500,500);
setVisible(true);
}
public static void main (String [] args) {
new Joc();
}
}
Can anyone help me with this problem ? I'm new to java and coding , i just play around with the code... but i want results

I don't see where you add the model to the list, so the JList has an empty model with nothing to display
Create the JList using the model:
DefaultListModel<Card> model = new DefaultListModel<Card>();
//JList<Card> list = new JList<Card>();
JList<Card> list = new JList<Card>(model);
Also, you should be using:
list.setVisibleRowCount(...);
This will allow the JList to determine its own preferred size and you can use frame.pack() instead of frame.setSize(...).

Related

problems in selecting the JComboBox items(in JMenu) Java Swing

Im having trouble in selecting the JCombox items here is a gif:
Gif that shows exactly what is the problem
here is code that you can test your own as asked by #UNKNOWN
i delete everything that no needed so you can test , i have no error in console;
the code works perfectly but the combo slection error is there
import javax.swing.*;
import java.awt.*;
public class ForTest extends JFrame {
private JTextArea txtArea= null;
private JComboBox cmbFontSize = null;
private JComboBox cmbFontFamily = null;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new ForTest();
}
});
}
private ForTest(){
init();
}
public void init(){
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setTitle("FOR TEST");
this.setLocationRelativeTo(null);
this.setSize(850,500);
txtArea= new JTextArea();
txtArea.setSize(830,470);
JScrollPane panScrollable = new JScrollPane(txtArea);
panScrollable.setSize(840,480);
this.add(panScrollable, BorderLayout.CENTER);
JMenuBar jmbTop= new JMenuBar();
JMenu modifica = new JMenu("Modifica");
modifica.setFont(new Font("arial",Font.PLAIN,15));
JMenu fontSettings= new JMenu("Font Settings");
JMenuItem cmbFam= new JMenuItem("FONT FAMILY");
cmbFontSize = new JComboBox();
cmbFontFamily = new JComboBox();
cmbFam.add(cmbFontFamily);
fontSettings.add(cmbFam);
// fontSettings.add(cmbFontSize);
//fontSettings.add(cmbFontFamily);
modifica.add(fontSettings);
jmbTop.add(modifica);
loadFontFamily();
loadFontSize();
this.setJMenuBar(jmbTop);
this.setVisible(true);
}
private void loadFontFamily() {
String fonts[] =
GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for ( int i = 0; i < fonts.length; i++ )
{
cmbFontFamily.addItem(fonts[i]);
}
}
private void loadFontSize() {
for (int i= 10; i<50;i++){
cmbFontSize.addItem(i);
}
}
}
the code is simple but cant understand why i cannot select the items
thanks in advance:)
You should wrap the JComboBox inside a JMenuItem object and then add the to the JMenu one. That would do the trick

Editable JComboBox gives error: <identifier> expected and illegal start of type

I am trying to create and Editable JComboBox to allow the user to type the name of the song to purchase. However when I set tunes.setEditable(true); I get an error... any help will be appreciated!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JTextArea;
public class JTunes2 extends JFrame implements ItemListener
{
int songNum,songPrice;
int[] songAmount = {2,5,8,1,4,7,12,10,11,3,6,9};
String result;
JComboBox tunes = new JComboBox();
// set as editable
tunes.setEditable(true);
JLabel labelTunes = new JLabel("Song List");
JLabel outputs = new JLabel();
FlowLayout layout = new FlowLayout();
public JTunes2()
{
super("Song Selector");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(layout);
// add song names to combo box and register an item listener.
tunes.addItem("Song1");
tunes.addItem("Song2");
tunes.addItem("Song3");
tunes.addItem("Song4");
tunes.addItem("Song5");
tunes.addItem("Song6");
tunes.addItem("Song7");
tunes.addItem("Song8");
tunes.addItem("Song9");
tunes.addItem("Song10");
tunes.addItem("Song11");
tunes.addItem("Song12");
tunes.addItemListener(this);
panel.add(labelTunes);
panel.add(tunes);
panel.add(outputs);
//add panel to the frame
setContentPane(panel);
}
public void itemStateChanged(ItemEvent e)
{
//create source object
Object source = e.getSource();
//check the type size
if(source == tunes)
{
songNum = tunes.getSelectedIndex();
songPrice = songAmount[songNum];
result = "Total Price $" + songPrice;
//Display result
outputs.setText(result);
}
}
public static void main(String[] args)
{
// create class object
JTunes frame = new JTunes();
frame.setSize(250, 180);
frame.setVisible(true);
}
}
Thank you!
Actually, Java requires that you setup JComponents in the constructor. In order for your code to work, you need to call on setEditable(true) in the constructor, which means that you just need to move tunes.setEditable(true); to the constructor.
Tip: always allocate memory for JComponents in the constructor (you want to draw the components as soon as you create the Jframe). You can have a reference to the JComboBox at the class level.
Here is another version of your code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JTextArea;
public class JTunes2 extends JFrame implements ItemListener
{
int songNum,songPrice;
int[] songAmount = {2,5,8,1,4,7,12,10,11,3,6,9};
String result;
JComboBox tunes;
JLabel labelTunes = new JLabel("Song List");
JLabel outputs = new JLabel();
FlowLayout layout = new FlowLayout();
public JTunes2()
{
super("Song Selector");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(layout);
tunes = new JComboBox();
// set as editable
tunes.setEditable(true);
// add song names to combo box and register an item listener.
tunes.addItem("Song1");
tunes.addItem("Song2");
tunes.addItem("Song3");
tunes.addItem("Song4");
tunes.addItem("Song5");
tunes.addItem("Song6");
tunes.addItem("Song7");
tunes.addItem("Song8");
tunes.addItem("Song9");
tunes.addItem("Song10");
tunes.addItem("Song11");
tunes.addItem("Song12");
tunes.addItemListener(this);
panel.add(labelTunes);
panel.add(tunes);
panel.add(outputs);
//add panel to the frame
setContentPane(panel);
}
public void itemStateChanged(ItemEvent e)
{
//create source object
Object source = e.getSource();
//check the type size
if(source == tunes)
{
songNum = tunes.getSelectedIndex();
songPrice = songAmount[songNum];
result = "Total Price $" + songPrice;
//Display result
outputs.setText(result);
}
}
public static void main(String[] args)
{
// create class object
JTunes2 frame = new JTunes2();
frame.setSize(250, 180);
frame.setVisible(true);
}
}
You added the tunes.setEditable(true) at the class level, instead of at the method level. No statements allowed at the class level!
Here's a fixed version: I renamed JTunes2 to JTunes to fix the compilation errors, and moved the setEditable to the constructor. Also I fixed the indentation - this makes it harder to make this mistake:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JTextArea;
public class JTunes extends JFrame implements ItemListener
{
int songNum,songPrice;
int[] songAmount = {2,5,8,1,4,7,12,10,11,3,6,9};
String result;
JComboBox tunes = new JComboBox();
JLabel labelTunes = new JLabel("Song List");
JLabel outputs = new JLabel();
FlowLayout layout = new FlowLayout();
public JTunes()
{
super("Song Selector");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(layout);
tunes.setEditable(true);
// add song names to combo box and register an item listener.
tunes.addItem("Song1");
tunes.addItem("Song2");
tunes.addItem("Song3");
tunes.addItem("Song4");
tunes.addItem("Song5");
tunes.addItem("Song6");
tunes.addItem("Song7");
tunes.addItem("Song8");
tunes.addItem("Song9");
tunes.addItem("Song10");
tunes.addItem("Song11");
tunes.addItem("Song12");
tunes.addItemListener(this);
panel.add(labelTunes);
panel.add(tunes);
panel.add(outputs);
//add panel to the frame
setContentPane(panel);
}
public void itemStateChanged(ItemEvent e)
{
//create source object
Object source = e.getSource();
//check the type size
if(source == tunes)
{
songNum = tunes.getSelectedIndex();
songPrice = songAmount[songNum];
result = "Total Price $" + songPrice;
//Display result
outputs.setText(result);
}
}
public static void main(String[] args)
{
// create class object
JTunes frame = new JTunes();
frame.setSize(250, 180);
frame.setVisible(true);
}
}

Nothing appears when I try to create a JFrame. (JButtons, JMenuBar etc.)

I'm working on a pathfinder program which uses GUI. I tried to run the program before I added some stuff and it showed everything fine. But after working on it some more it stopped showing the buttons and the menu bar.
Here is the code. Some variable names are in Swedish as well, but I hope that it won't be an issue. (Please take in mind that the program is far from finished.)
Thank you in advance!
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
public class Pathfinder extends JFrame {
JButton hittaVäg, visaFörbindelse, nyPlats, nyFörbindelse, ändraFörbindelse;
JMenuBar menyBar;
JMenuItem ny, avsluta, hittaVägMeny, visaFörbindelseMeny, nyPlatsMeny, nyFörbindelseMeny, ändraFörbindelseMeny;
String str = System.getProperty("user.dir");
JFileChooser jfc;
BildPanel Bild = null;
Pathfinder(){
super("PathFinder");
setLayout(new BorderLayout());
setSize(590, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
jfc = new JFileChooser(".");
JPanel norra = new JPanel();
add(norra, "norra");
JButton hittaVäg = new JButton("Hitta väg");
JButton visaFörbindelse = new JButton("Visa förbindelse");
JButton nyPlats = new JButton("Ny plats");
JButton nyFörbindelse = new JButton("Ny förbindelse");
JButton ändraFörbindelse = new JButton("Ändra förbindelse");
norra.add(hittaVäg);
norra.add(visaFörbindelse);
norra.add(nyPlats);
norra.add(nyFörbindelse);
norra.add(ändraFörbindelse);
hittaVäg.addActionListener(new HittaLyss());
visaFörbindelse.addActionListener(new VisaLyss());
nyPlats.addActionListener(new NyPlatsLyss());
nyFörbindelse.addActionListener(new NyFörbindelseLyss());
ändraFörbindelse.addActionListener(new NyFörbindelseLyss());
JMenuBar menyBar = new JMenuBar();
setJMenuBar(menyBar);
JMenu arkivMeny = new JMenu("Arkiv");
JMenu operationerMeny = new JMenu("Operationer");
menyBar.add(arkivMeny);
menyBar.add(operationerMeny);
JMenuItem ny = new JMenuItem("Ny");
JMenuItem avsluta = new JMenuItem("Avsluta");
arkivMeny.add(ny);
arkivMeny.add(avsluta);
ny.addActionListener(new NyLyss());
avsluta.addActionListener(new AvslutaLyss());
JMenuItem hittaVägMeny = new JMenuItem("Hitta väg");
JMenuItem visaFörbindelseMeny = new JMenuItem("Visa förbindelse");
JMenuItem nyPlatsMeny = new JMenuItem("Ny plats");
JMenuItem nyFörbindelseMeny = new JMenuItem("Ny förbindelse");
JMenuItem ändraFörbindelseMeny = new JMenuItem("Ändra förbindelse");
operationerMeny.add(hittaVägMeny);
operationerMeny.add(visaFörbindelseMeny);
operationerMeny.add(nyPlatsMeny);
operationerMeny.add(nyFörbindelseMeny);
operationerMeny.add(ändraFörbindelseMeny);
hittaVäg.addActionListener(new HittaLyss());
visaFörbindelse.addActionListener(new VisaLyss());
nyPlats.addActionListener(new NyPlatsLyss());
nyFörbindelse.addActionListener(new NyFörbindelseLyss());
ändraFörbindelse.addActionListener(new ÄndraFörbindelseLyss());
}
class HittaLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){
}
}
class VisaLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){
}
}
class NyPlatsLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){
}
}
class NyFörbindelseLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){
}
}
class ÄndraFörbindelseLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){
}
}
class NyLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){
int svar = jfc.showOpenDialog(Pathfinder.this);
if (svar == JFileChooser.APPROVE_OPTION){
File f = jfc.getSelectedFile();
String filnamn = f.getAbsolutePath();
if (Bild != null)
remove(Bild);
Bild = new BildPanel(filnamn);
add(Bild, BorderLayout.CENTER);
validate();
repaint();
pack();
}
}
}
class AvslutaLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){
}
}
public static void main (String[] args){
new Pathfinder();
}
}
One of the issues is IllegalArgumentException at line
add(norra, "norra");
The layout of the frame's content pane is set to BorderLayout, but this layout does not understand "norra" constraint. See How to Use BorderLayout for more details and examples.
Also, you should call setVisible once all the components are added and initialized.
Call setVisible(true) on the JFrame after adding all components, not before. The order should be:
Add components
Call pack() on the JFrame
Then call setVisible(true) on the JFrame.
You need to first add the components and then call setVisible(true).
So the order matters here. Try it, see if it helps.

Need help to fix ComboBox programming error

I'm a kinda newbie actually and just self-studying. I really want to learn how to use JComboBox properly. I have created a simple program but it took me forever to fix it.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SampleButtonKo {
JComboBox combo;
public void ComboBox1() {
String course[] = {
"PM1", "PM2", "PM3", "PM4"
};
JFrame frame = new JFrame("Mang Inasal Ordering System");
JPanel panel = new JPanel();
combo = new JComboBox(course);
combo.setBackground(Color.gray);
combo.setForeground(Color.red);
panel.add(combo);
frame.add(panel);
combo.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ie) {
String str = (String) combo.getSelectedItem();
System.out.print("You have chosen " + str);
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
}
public static void main(String[] args) {
JComboBox = new JComboBox();
}
}
You forgot a name for the variable
Instead of
JComboBox = new JComboBox();
try
JComboBox j = new JComboBox();
^
But perhaps, as iTech suggests, you want to create an instance of your class.
new SampleButtonKo();
There are obvious few errors in your code, you need to have the constructor named exactly as your class with no return type. Second, in your main you should create an instance of your class not the JComboBox
public class SampleButtonKo{
JComboBox combo;
public SampleButtonKo(){
// Copy your code from "ComboBox1" here
}
public static void main(String[] args) {
new SampleButtonKo();
}
}

Add to JList and select element?

I need some help to be able to add element to a JList and how to select element whith event.
This is my JList:
DefaultListModel model = new DefaultListModel();
JList list = new JList(model);
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
JScrollPane listScroller = new JScrollPane(list);
listScroller.setPreferredSize(new Dimension(430, 80));
This is part of my actionlistener that handle different buttons. It's here I want to use model.add("Name"); bot I get a red underline in Eclipse!?
public void actionPerformed(ActionEvent event){
// New customer
if(event.getSource() == buttonNewCustomer && statusButtonNewCustomer)
{
String name = textInputName.getText();
String number = textInputPersonalNumber.getText();
boolean checkCustomerExist = customHandler.findCustomer(name, number);
if(!checkCustomerExist) // If not true add new customer
{
customHandler.addCustomer(name, number); // Call method to add name
setTitle(title + "Kund: " + name); // Set new title
model.addElement(name);
}
}
}
Then I would preciate some help how I should select the element inside the JList? Should I use implements ActionListener to the class or a FrameHandler object? Thanks!
EDIT: My main problem that I can't solve is that the JList is inside the construcor and when I use model.add("name"); inside the constructor it works, but it's not working when I want to add something outside the constructor? I have read the tutorial several times, but can't find any help for this.
EDIT 2: The completet code. Probably some out of scope problem?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI4EX extends JFrame implements ActionListener{
private JButton buttonNewCustomer, buttonTerminate, buttonAddNewName, buttonAddNewSavingsAccount, buttonAddNewCreditAccount;
private JLabel textLabelName, textLabelPersonalNumber, textLabelNewName;
private JTextField textInputName, textInputPersonalNumber, textInputNewName;
private JPanel panelNewCustomer, panelQuit, panelNewAccount, panelChangeName, panelSelectCustomer;
private boolean statusButtonNewCustomer = true;
private boolean statusButton2 = true;
private boolean statusButtonAddNewName = true;
private String title = "Bank ";
// Create a customHandler object
CustomHandler customHandler = new CustomHandler();
// Main method to start program
public static void main(String[] args){
GUI4EX frame = new GUI4EX();
frame.setVisible(true);
frame.setDefaultCloseOperation(3);
}
// Cunstructor
public GUI4EX(){
// Create window
setTitle(title);
setSize(450,500);
setLocation(400,100);
setResizable(false);
// Set layout to boxlayout
Container container = getContentPane( );
setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
DefaultListModel model = new DefaultListModel();
JList list = new JList(model);
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
JScrollPane listScroller = new JScrollPane(list);
listScroller.setPreferredSize(new Dimension(430, 80));
model.addElement("test");
model.addElement("test");
model.addElement("test");
model.addElement("test");
model.addElement("test");
model.addElement("test");
// Create jpanels
panelNewCustomer = new JPanel();
panelQuit = new JPanel();
panelNewAccount = new JPanel();
panelChangeName = new JPanel();
panelSelectCustomer = new JPanel();
// Create and add components - buttons
buttonNewCustomer = new JButton("OK");
buttonTerminate = new JButton("Avsluta");
buttonAddNewName = new JButton("OK");
buttonAddNewSavingsAccount = new JButton("Sparkonto");
buttonAddNewCreditAccount = new JButton("Kreditkonto");
// Create and add components - labels
textLabelName = new JLabel("Namn");
textLabelPersonalNumber = new JLabel("Personnummer");
textLabelNewName = new JLabel("Nytt namn");
//add(textLabel1);
// Create and add components - textfields
textInputName = new JTextField("");
textInputPersonalNumber = new JTextField("");
textInputName.setColumns(10);
textInputPersonalNumber.setColumns(10);
textInputNewName = new JTextField();
textInputNewName.setColumns(20);
// Add components to panel new customer
panelNewCustomer.add(textLabelName);
panelNewCustomer.add(textInputName);
panelNewCustomer.add(textLabelPersonalNumber);
panelNewCustomer.add(textInputPersonalNumber);
panelNewCustomer.add(buttonNewCustomer);
// Add components to panel to select customer
panelSelectCustomer.add(listScroller);
// Add components to panel new name
panelChangeName.add(textLabelNewName);
panelChangeName.add(textInputNewName);
panelChangeName.add(buttonAddNewName);
// Add components to panel new accounts
panelNewAccount.add(buttonAddNewSavingsAccount);
panelNewAccount.add(buttonAddNewCreditAccount);
// Add components to panel quit
panelQuit.add(buttonTerminate);
// Set borders to jpanels
panelNewCustomer.setBorder(BorderFactory.createTitledBorder("Skapa ny kund"));
panelChangeName.setBorder(BorderFactory.createTitledBorder("Ändra namn"));
panelNewAccount.setBorder(BorderFactory.createTitledBorder("Skapa nytt konto"));
panelQuit.setBorder(BorderFactory.createTitledBorder("Avsluta programmet"));
panelSelectCustomer.setBorder(BorderFactory.createTitledBorder("Välj kund"));
// Add panels to window
add(panelNewCustomer);
add(panelSelectCustomer);
add(panelChangeName);
add(panelNewAccount);
add(panelQuit);
// Listener
// FrameHandler handler = new FrameHandler();
// Add listener to components
//button1.addActionListener(handler);
buttonNewCustomer.addActionListener(this);
buttonAddNewName.addActionListener(this);
buttonAddNewSavingsAccount.addActionListener(this);
buttonAddNewCreditAccount.addActionListener(this);
buttonTerminate.addActionListener(this);
}
//private class FrameHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
// New customer
if(event.getSource() == buttonNewCustomer && statusButtonNewCustomer)
{
String name = textInputName.getText();
String number = textInputPersonalNumber.getText();
boolean checkCustomerExist = customHandler.findCustomer(name, number); // Check if customer exist
if(!checkCustomerExist) // If not true add new customer
{
customHandler.addCustomer(name, number); // Call method to add name
setTitle(title + "Kund: " + name); // Set new title
model.addElement("name");
}
}
// Change name
if(event.getSource() == buttonAddNewName && statusButtonAddNewName)
{
String newName = textInputNewName.getText();
customHandler.changeName(newName); // call method to change name
setTitle(title + "Kund: " + newName);
}
// Create new savings account
if(event.getSource() == buttonAddNewSavingsAccount)
{
customHandler.CreateNewSavingsAccount();
}
// Create new credit account
if(event.getSource() == buttonAddNewCreditAccount)
{
customHandler.CreateNewCreditAccount();
}
// Terminate program
if(event.getSource()==buttonTerminate && statusButton2)
{
System.exit(3);
}
}
//}
}
You are lucky I am in a good mood. Here a very basic example, matching the code you provided. Type something in the textfield, hit the enter button and watch the list get populated.
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AddToJListDemo {
private static JFrame createGUI(){
JFrame frame = new JFrame( );
final DefaultListModel model = new DefaultListModel();
JList list = new JList( model );
final JTextField input = new JTextField( 10 );
input.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent aActionEvent ) {
String text = input.getText();
if ( text.length() > 0 ) {
model.addElement( text );
input.setText( "" );
}
}
} );
frame.add( list, BorderLayout.CENTER );
frame.add( input, BorderLayout.SOUTH );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
return frame;
}
public static void main( String[] args ) {
EventQueue.invokeLater( new Runnable() {
public void run() {
JFrame frame = createGUI();
frame.setSize( 200,200 );
frame.setVisible( true );
}
} );
}
}
Edit
Based on your full code, you must make the list a field in your GUI4EX class, similar to for example the buttonNewCustomer field
public class GUI4EX extends JFrame implements ActionListener{
//... all other field
DefaultListModel model;
//constructor
public GUI4EX(){
//all other code
//DefaultListModel model = new DefaultListModel(); instantiate the field instead
model = new DefaultListModel();
JList list = new JList(model);
//rest of your code
}
}
This will make sure you can access the model in the actionPerformed method. But if you cannot figure out something this basic, you should not be creating GUIs but reading up on basic Java syntax and OO principles

Categories

Resources