Error:
error: cannot find symbol
What appears doesn't works:
If I write: "InvoerVakhandler extends boven" then the error disapears but then I get an endless loop and in the end the program crashes and says stack overflow error.
If I try to change the "}" symbol from class boven and place it in the end of the text then I even get more error messages.
Question: What do I have to change in my code to make it work?
Note:
I'm new to Java and I know there are more posts like this one but I just can't apply them to my code with my current limited understanding of Java.
If someone wants to know: I'm using JCreator.
What i'm trying to make: What I'm trying to make is fairly simple.
1) Fill in a name in the JTextField, press enter and the name should appear in the JTextArea. After the name is in the JTextArea the JTextField becomes empty so you can fill another name and so on there should appear a list of names in JTextArea. (this is what I'm now trying to make)
2) Push the button kiesWin to make the program choose a random person from the list.
3) Push the button resetL to reset the program so I can make a new list to choose a random winner from it.
Part of the code where the error appears: (from the class InvoerVakHandler)
String invoer = invoervak1.getText();
With my limited knowledge of java the problem could be litterly anywhere so in case I will post the whole code.
Whole code:
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
// Main method to make the frame
public class Loterij3 extends JFrame {
public static void main( String args[] ) {
JFrame frame = new Loterij3();
frame.setExtendedState( frame.MAXIMIZED_BOTH );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setTitle( "Klanten Register" );
frame.setContentPane( new Paneel() );
frame.setVisible( true );
}
}
class Paneel extends JPanel {
private boven Boven;
JTextArea textvak1;
JTextField textvak2;
OnthoudNaam onthoudNaam = new OnthoudNaam();
public Paneel() {
setLayout( new BorderLayout() ); // using border Layout.
setBackground( Color.LIGHT_GRAY );
textvak1 = new JTextArea();
add( new JScrollPane( textvak1 ) );
textvak1.setBackground( Color.WHITE );
textvak2 = new JTextField();
textvak2.setHorizontalAlignment(JTextField.CENTER);
textvak2.setEditable( false );
Boven = new boven();
add( Boven, BorderLayout.NORTH );
add( textvak1, BorderLayout.CENTER );
add( textvak2, BorderLayout.SOUTH );
}
public class boven extends Paneel {
JButton kiesWin, resetL;
JLabel label1;
JTextField invoervak1;
public boven() {
setBackground( Color.LIGHT_GRAY );
setLayout( new GridLayout( 1, 4, 100, 5 ) ); // using GridLayout.
Border border =
BorderFactory.createEmptyBorder( 10, 10, 10, 10 );
setBorder( border );
kiesWin = new JButton("Kies een Winnaar!");
kiesWin.addActionListener( new kies() );
resetL = new JButton("Reset alles");
resetL.addActionListener( new reset() );
label1 = new JLabel("Voer Persoon in en druk op enter: ", JLabel.RIGHT);
invoervak1 = new JTextField( 20 );
invoervak1.addActionListener( new InvoerVakHandler() );
add( label1 );
add( invoervak1 );
add( kiesWin );
add( resetL );
}
}
// de naam
class naam {
private String ingevoerdNaam;
public naam( String ingevoerdNaam) {
this.ingevoerdNaam = ingevoerdNaam;
}
public String getIngevoerdNaam() {
return ingevoerdNaam;
}
}
// Arraylist
class OnthoudNaam extends JPanel {
private ArrayList<naam> lijst;
public OnthoudNaam() {
lijst = new ArrayList<naam>();
}
public void voegNaamToe(naam x ) {
lijst.add(x);
}
public String toString() {
StringBuffer buffer = new StringBuffer();
for(naam x : lijst ) {
buffer.append( x );
buffer.append( "\n" );
}
return buffer.toString();
}
}
// this is the part where the code goes wrong
public class InvoerVakHandler implements ActionListener {
public void actionPerformed( ActionEvent e ) {
String invoer = invoervak1.getText();
naam naam = new naam( invoer );
onthoudNaam.voegNaamToe( naam );
textvak1.setText( onthoudNaam.toString() );
}
}
// kies
class kies implements ActionListener {
public void actionPerformed( ActionEvent e ) {
}
}
// reset
class reset implements ActionListener {
public void actionPerformed( ActionEvent e ) {
}
}
}
For everyone who is trying to help me: Thank you for all your help and patience in advance!
The line in question in in class: InvoerVakHandler
The variable is defined in class: boven
That is why it can't find it.
I think you can get the source of the event from the ActionEvent passed to the actionPerformed() method.
Note that normally we use an upper-case letter to begin the name of any class and lower case to begin methods and variables. (Constants are an exception.)
The variable invoervak1 is defined in the boven class and is not visible within InvoerVakHandler. Since InvoerVakHandler is registered exclusively with the invoervak1 component, you can use the source object of your ActionListener to obtain the reference to the JTextField:
JTextField textField = (JTextField) e.getSource();
String invoer = textField.getText();
Typically anonymous ActionListener implementations are used. This makes the intended source clear along with its associated functionality.
The error goes away with the answers from Reimeus. However your program will not run because you created a stackoverflow.
In the class Paneel you call
Boven = new boven();
However boven extends from your Paneel class. So Java needs to build a Paneel instance when building a Boven instance which will require a Paneel and so on.
Make boven extend from JPanel and your GUI starts and works.
However your output will look like this
Paneel$naam#3dee7a6c
This happens because your class "naam" did not overwritte the toString method and so Object.toString is called which produces a String based on the class name and hashCode of a Object.
So extend your naam class to this:
class naam {
private String ingevoerdNaam;
public naam( String ingevoerdNaam) {
this.ingevoerdNaam = ingevoerdNaam;
}
public String getIngevoerdNaam() {
return ingevoerdNaam;
}
public String toString() {
return ingevoerdNaam;
}
}
Hope this helps. And like mentioned already please stick to the Java Coding Conventions.
Related
package helloworld;
import javax.swing.*;
import java.awt.event.*;
public class helloworld extends JFrame{
public static void main( String args[] ){
JFrame frame = new helloworld();
frame.setSize( 400, 200 );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setTitle( "HelloWorld" );
JPanel panel = new Panel();
frame.setContentPane( panel );
frame.setVisible( true );
}
}
class Panel extends JPanel {
private JButton button, resetbutton;
private JTextField textfield;
public Panel(){
button = new JButton( "click" );
button.addActionListener( new ButtonHandler() );
resetbutton = new JButton( "erase" );
resetbutton.addActionListener( new ResetbuttonHandler() );
textfield = new JTextField( 10 );
add( button );
add( textfield );
add( resetbutton );
}
class ButtonHandler implements ActionListener{
public void actionPerformed( ActionEvent e ){
textfield.setText( "you clicked" );
}
}
class ResetbuttonHandler implements ActionListener{
public void actionPreformed( ActionEvent e ){
textfield.setText( "" );
}
}
}
I just set up some basic code to learn a bit more about java. But I have a problem regarding my button classes.
The error says the following: The type Panel.ResetbuttonHandler must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent) Previously I also had this problem with the ButtonHandler class, somehow I solved this problem, but the ResetbuttonHandler still shows the same error, and I couldn't figure out what the differences between them were.I also tried to #Override them, but that didn't work. I've got a book about java (that is also where I'm learning from), and they do this in the exact same way. Even searched the whole internet, still didn't find the solution. I hope that someone can help me with this problem!
Please correct spelling of actionPreformed method to actionPerformed
class ResetbuttonHandler implements ActionListener{
public void actionPerformed( ActionEvent e ){
textfield.setText( "" );
}
}
First time posting. I feel this should be rather straight-forward, but after searching and trying, I've been left without a solution. I re-created my problem in a very simple MVC program - it's behaving the same in both this sample application and in my actual application; I'm clearly making the same mistake in both places.
In my Java desktop application, I have the GUI code in a "View" class. This class is strictly responsible for the appearance of the GUI, ensuring all viewable objects are laid out on the screen/window. The "Controller" is handling all the program flow, and is responsible for listening for the user events, such as a button click, and then calling the necessary View methods to either retrieve or put data from/to the GUI.
The classes, below, do this in a very simple way - all it does is draw a small JFrame window containing a JPanel with 4 objects, 3 JButtons and a JTextField. When any of the 3 buttons is pressed, the number of that button, 1, 2, or 3, is to be displayed in the text field. Only, the field isn't updating. I have a method in the View that calls the repaint() and revalidate() methods of the JFrame, called from the controller right after it updates the text field after a button press. I have a print statement that writes the text to the console, so I know that part works. I also believe the updating of the text field works as I have a message dialog display its value. My understanding is JFrame.repaint(); is supposed to repaint the frame and all its descendants (objects within), it just, isn't.
Thanks in advance for helping. Here's my sample code:
Model.java (currently empty)
package com.techbybryan;
public class Model {
}
View.java
package com.techbybryan;
import javax.swing.*;
import java.awt.*;
public class View {
JButton button1 = new JButton( "One" );
JButton button2 = new JButton( "Two" );
JButton button3 = new JButton( "Three" );
JTextField jTextField = new JTextField();
JPanel jPanel = new JPanel();
JFrame jFrame = new JFrame( "Gui Test" );
public View(){
jPanel.add( button1 );
jPanel.add( button2 );
jPanel.add( button3 );
jPanel.add( jTextField );
jFrame.add( jPanel );
jFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
jFrame.setSize(new Dimension( 500, 100 ) );
jFrame.setLocationRelativeTo( null );
jFrame.setResizable( false );
jFrame.setVisible( true );
}
public JButton getButton1() {
return button1;
}
public void setButton1( JButton button1 ) {
this.button1 = button1;
}
public JButton getButton2() {
return button2;
}
public void setButton2( JButton button2 ) {
this.button2 = button2;
}
public JButton getButton3() {
return button3;
}
public void setButton3( JButton button3 ) {
this.button3 = button3;
}
public JTextField getjTextField() {
return jTextField;
}
public void setJTextField( JTextField jTextField ) {
this.jTextField = jTextField;
}
public void repaint(){
jFrame.revalidate();
jFrame.repaint();
JOptionPane.showMessageDialog( null, jTextField );
}
}
Controller.java
package com.techbybryan;
import javax.swing.*;
public class Controller {
Model model;
View view;
Controller controller;
public Controller( Model model, View view ){
this.model = model;
this.view = view;
}
public void init(){
view.getButton1().addActionListener( e -> setOutputText( "One" ) );
view.getButton2().addActionListener( e -> setOutputText( "Two" ) );
view.getButton3().addActionListener( e -> setOutputText( "Three" ) );
}
public void setOutputText( String textToDisplay ){
System.out.println( textToDisplay );
view.setJTextField( new JTextField( textToDisplay ) );
view.repaint();
}
}
GuiText.java
package com.techbybryan;
public class GuiTest {
public static void main(String[] args) {
Model model = new Model();
View view = new View();
Controller controller = new Controller( model, view );
controller.init( );
}
}
Thank you so much for any constructive advice you may have; I appreciate your feedback.
This code in your controller is not right:
view.setJTextField( new JTextField( textToDisplay ) );
The controller should not be adding components to the view, but rather that method, the actionlistener, should be changing the state of the model, something that you can't do since your model class is inexplicably empty.
In short, you're doing things backwards -- you should get your model working first, and then wire the control and the view to work with the model first and foremost. Your model should notify the view when it changes (a PropertyChangeListener would work well here), and then the view should update the display in the currently displayed JTextField (not adding a new JTextField --sorry to be blunt, but that's plum crazy), based on the model's state.
Also, get rid of all those repaints and revalidates as that code will not help your basic problem and is unnecessary.
Note also that I would get rid of this code:
public void setJTextField( JTextField jTextField ) {
this.jTextField = jTextField;
}
Instead do something more like:
public void setJTextFieldText(String text) {
this.jTextField.setText(text);
}
don't go adding components unnecessarily but instead change the state of the components that you already have
Ok so i have a comboBox, and a JTextField, whenever i chose the quantity, it would be displayed on the textfield. I have another class, which will retrieve the whatever inside the textfield, but the order class doesn't retrieve the information from catalogue class.
class Catalogue extends JPanel {
String[] h1Quantity = {"0","1","2","3","4","5","6","7","8","9","10"};
h1CBox = new JComboBox <String> (h1Quantity);
h1CBox.setSelectedIndex(0);
h1CBox.addActionListener (new Listener());
h1CBox.setPreferredSize ( new Dimension (50,30));
JLabel noBooks = new JLabel ("Quantity");
booksF = new JTextField(8);
public class Listener implements ActionListener {
public void actionPerformed (ActionEvent event) {
int total = h1CBox.getSelectedIndex();
booksF.setText(Integer.toString(total));
}
}
public String booksFText() {
return booksF.getText();
}
}
class Order extends JPanel {
Catalogue catalogue ;
public Order (Catalogue catalogue)
{
this.catalogue = catalogue;
JPanel panel = new JPanel ();
String text2= catalogue.booksFText();
textArea1 = new JTextArea (text2, 20, 35);
add(textArea1);
add(panel);
}
}
I'm new to java so please keep it simple. thanks alot.
You have 2 constructors in the Order class and catalogue is only set in the first one. Set this in the second contructor as well and the NPE should go away (although hard to know for sure without the stacktrace!)
Always try to post complete code. and stack trace too.
See your order class.
class Order extends JPanel {
public Order (Catalogue catalogue)
{
add(textArea);
}
}
If you use the second constructor , then the class variable catalogue will not be given memory. Thus NULL POINTER EXCEPTION . The code inside the second constructor has been moved to the first one.
The other reason may be that the variable being passed in order constructor is not defined properly. Should be done something like this.
Catalogue catalogue = new Catalogue();
Order order = new Order(catalogue);
See Updated Catalogue class.
class Catalogue extends JPanel {
String[] h1Quantity = {"0","1","2","3","4","5","6","7","8","9","10"};
JComboBox<String> h1CBox ; //Assuming you forgot to define it.
JLabel noBooks ;
JTextField booksF ;
//Define a new constructor
public Catalogue () {
//set jlabel
noBooks = new JLabel ("Quantity");
//set combobox
h1CBox = new JComboBox <String> (h1Quantity);
h1CBox.setSelectedIndex(0);
h1CBox.addActionListener (new Listener());
h1CBox.setPreferredSize ( new Dimension (50,30));
//set textfield
booksF = new JTextField(8);
//add UI items to your panel class
add(h1CBox); //combobox
add(noBooks); // label
add(booksF); // textfield
}
public class Listener implements ActionListener {
public void actionPerformed (ActionEvent event) {
int total = h1CBox.getSelectedIndex();
booksF.setText(Integer.toString(total));
}
}
public String booksFText() {
return booksF.getText();
}
}
Always define your UI like this . Of course , there are better ways . Thus code looks clean and you understand things. Learn to put comments to remind you what you tried to do somewhere.
The MAIN Class
public class Main {
static JTextArea textArea = new JTextArea(40,40);
static class Order extends JPanel{
public Order(){
add(textArea);
}
}
static class Catalogue extends JPanel{
....
private ActionListener listener = new ActionListener(){
public void actionPerformed(ActionEvent event){
textArea.setText(h1CBox.getSelectedIndex()+"");
}
};
}
public static void main(String args[]){
//Construct a frame and add panels and you are good to go.
}
}
One last suggestion, if you plan on NOT changing the data of textarea yourself , use textfield or label instead of textarea. Sometimes the text inside textarea is set , but user is unable to see because of improper bounds . So , to be sure just replace the textarea with label or textfield. Cheers :)
I am using Swing to build a GUI in Java. The code to make the button and add it is like this:
//Create a button
JButton exitButton = new JButton("Exit");
exitButton.setSize(90, 40);
exitButton.setLocation(800, 450);
exitButton.setVisible(true);
//Adding components
window.getContentPane().add(exitButton);
When I run the app, the button appears in the whole window, sometimes appears as its intended and sometimes doesn't come. Is this some sort of java bug or a prob with my sdk. In case you wish to know what sort of window it is,
//Create a window
JFrame window = new JFrame("First Window");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
window.setVisible(true);
window.setSize(1000, 550);
window.setLocation(150, 150);
It's all within static void main. BTW, how I get the button to close the window through System.exit(0); (I am a beginner and this is my first self-written GUI)
You need a layout. See A Visual Guide to Layout Managers.
Also please check my tutorials here.
I got this sample code. This might be helpful Simple swing buttons
package com.ack.gui.swing.simple;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class SimpleSwingButtons extends JFrame {
public static void main( String[] argv ) {
SimpleSwingButtons myExample = new SimpleSwingButtons( "Simple Swing Buttons" );
}
public SimpleSwingButtons( String title ) {
super( title );
setSize( 150, 150 );
addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent we ) {
dispose();
System.exit( 0 );
}
} );
init();
setVisible( true );
}
private void init() {
JPanel my_panel = new JPanel();
my_panel.setLayout( new GridLayout( 3, 3 ) );
for( int i = 1; i < 10; i++ ) {
ImageIcon icon = new ImageIcon( i + ".gif" );
JButton jb = new JButton( icon );
jb.setToolTipText( i + ".gif" );
my_panel.add( jb );
}
getContentPane().add( my_panel );
my_panel.setBorder( BorderFactory.createEtchedBorder() );
}
}
courtesy Java.happycodings
You must check your layout, if you want to use custom position for your components, set Layout as null and use the setBounds(x,y,weight,height) method.
JFrame window = new JFrame("First Window");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
window.setLayout(null);
JButton exitButton = new JButton("Exit");
exitButton.setBounds(15,45,150,30);//This is just an example
exitButton.setVisible(true);
Best regards.
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