Re: JTextField won't update
It's a bit hard to tell from the snippet of code posted. A common mistake is to setText() on a component that is not the same as the one you added to the gui. For example by declaring another component using the same variable. Another is to set up a method like displayCurrentEntry() which never gets called: you can check that this method is called by adding a System.out.println() at the start.
Consider writing a SSCCE that illustrates the problem. Ie, something compilable and runnable but with only a single JTextField showing the problem. I realise that this might involve stepping back from Netbeans.
-----
When you post code, use the "code" tags. Put [code] at the start of the code and [/code] at the end. That way the formatting will be preserved. It's also a good idea to indent with spaces rather than tabs as the latter tend to get rendered rather generously by web browsers.
Re: JTextField won't update
Thanks for heads up about the Code tags. I'm new to this forum so I wasn't aware.
I actually only used NetBeans to build the GroupLayout. I wrote the rest of the code myself in
Eclipse.
It is calling the correct object. Because if I println, using getText, it gives me the correct String.
Re: JTextField won't update
Again to repeat what Peter stated, given what you've posted so far, we can't guess why it's not working. You'll either have to do some more debugging yourself or post more code -- best an SSCCE (again as suggested above).
Re: JTextField won't update
Quote:
Originally Posted by
multarnc
It is calling the correct object. Because if I println, using getText, it gives me the correct String.
Also I fear that you're misinterpreting the results you're getting. All the println statement is telling you is that you're filling in the JTextField of some Panel object. It doesn't tell you that you're doing this for the Panel object that is currently being displayed which is what I am guessing is wrong (again, all we can do is guess at this point in time til you tell/show more).
1 Attachment(s)
Re: JTextField won't update
I don't have time to do an SSCCE today, but I will tomorrow.
However, here's the full class code. I've also attached all classes in a zip file.
Code:
/* This class creates the Panel. It uses
* two panels, one for the text fields and one
* for the buttons.
*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
import javax.swing.WindowConstants;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle;
public class Panel extends JFrame implements ActionListener
{
// Initializes the variables
JFrame myJFrame;
JLabel jLabelAddressBook, jLabelFirstName, jLabelLastName, jLabelPhoneNumber, jLabelEmailAddress;
JLabel jLabelStreet, jLabelCityName, jLabelStateName, jLabelZipCode;
JPanel jPanel1, jPanel2;
public JTextField jTextFieldFirstName, jTextFieldLastName, jTextFieldPhoneNumber, jTextFieldEmailAddress, jTextFieldStreetNumber, jTextFieldStreetName;
public JTextField jTextFieldCityName, jTextFieldStateName, jTextFieldZipCode;
JButton jButtonSave, jButtonDelete, jButtonClear, jButtonUpdate, jButtonAdd, jButtonForward, jButtonBack, jButtonExit;
String firstName, lastName, phoneNumber, emailAddress, streetNumber, streetName, cityName, stateName, zipCode;
int entryNumber;
// Constructor
public Panel()
{
firstName = "";
lastName = "";
phoneNumber = "";
emailAddress = "";
streetNumber = "";
streetName = "";
cityName = "";
stateName = "";
zipCode = "";
entryNumber = 0;
// JFrame object declaration
myJFrame = new JFrame("Address Book");
// Declares the JPanel objects
jPanel1 = new JPanel();
jPanel2 = new JPanel();
// Declares the JLabel objects
jLabelAddressBook = new JLabel();
jLabelFirstName = new JLabel();
jLabelLastName = new JLabel();
jLabelPhoneNumber = new JLabel();
jLabelEmailAddress = new JLabel();
jLabelStreet = new JLabel();
jLabelCityName = new JLabel();
jLabelStateName = new JLabel();
jLabelZipCode = new JLabel();
// Declares the JTextField objects
jTextFieldFirstName = new JTextField(10);
jTextFieldLastName = new JTextField(15);
jTextFieldPhoneNumber = new JTextField(12);
jTextFieldEmailAddress = new JTextField(30);
jTextFieldStreetNumber = new JTextField(5);
jTextFieldStreetName = new JTextField(15);
jTextFieldCityName = new JTextField(15);
jTextFieldStateName = new JTextField(2);
jTextFieldZipCode = new JTextField(5);
// Declares the JButton objects
jButtonSave = new JButton();
jButtonDelete = new JButton();
jButtonClear = new JButton();
jButtonAdd = new JButton();
jButtonForward = new JButton();
jButtonBack = new JButton();
jButtonExit = new JButton();
GroupLayout jPanel1Layout = new GroupLayout(jPanel1); // Creates the Panel layout
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup
(
jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup
(
jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// Sets the JLabel text
jLabelAddressBook.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
jLabelAddressBook.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabelAddressBook.setText("Address Book");
jLabelFirstName.setText("First");
jLabelLastName.setText("Last");
jLabelPhoneNumber.setText("Phone");
jLabelEmailAddress.setText("Email");
jLabelStreet.setText("Street Address");
jLabelCityName.setText("City");
jLabelStateName.setText("State");
jLabelZipCode.setText("Zip");
// Sets the JButton text
jButtonSave.setText("Save");
jButtonDelete.setText("Delete");
jButtonAdd.setText("Add");
jButtonBack.setText("Back");
jButtonForward.setText("Forward");
jButtonClear.setText("Clear");
jButtonExit.setText("Exit");
// Uses the GroupLayout class, using NetBeans to graphically set up the application layout.
GroupLayout jPanel2Layout = new GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addGap(0, 10, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addComponent(jButtonClear, GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtonSave, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtonDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jButtonBack, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtonAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtonForward)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonExit)
.addGap(8, 8, 8))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButtonForward)
.addComponent(jButtonAdd)
.addComponent(jButtonBack))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButtonClear)
.addComponent(jButtonSave)
.addComponent(jButtonDelete))
.addGap(0, 0, Short.MAX_VALUE))
.addComponent(jButtonExit, javax.swing.GroupLayout.PREFERRED_SIZE, 64, GroupLayout.PREFERRED_SIZE)))
);
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(jLabelAddressBook, GroupLayout.PREFERRED_SIZE, 355, GroupLayout.PREFERRED_SIZE)
.addComponent(jPanel2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(5, 5, 5)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabelEmailAddress)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextFieldEmailAddress, GroupLayout.PREFERRED_SIZE, 217, GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
.addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jLabelPhoneNumber)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextFieldPhoneNumber))
.addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jLabelFirstName)
.addGap(13, 13, 13)
.addComponent(jTextFieldFirstName, GroupLayout.PREFERRED_SIZE, 101, GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabelLastName)))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextFieldLastName, GroupLayout.PREFERRED_SIZE, 134, GroupLayout.PREFERRED_SIZE))
.addComponent(jLabelStreet)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabelZipCode)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextFieldZipCode, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
.addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jLabelCityName)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextFieldCityName)
.addGap(18, 18, 18)
.addComponent(jLabelStateName)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextFieldStateName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jTextFieldStreetNumber, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextFieldStreetName, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabelAddressBook, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jTextFieldFirstName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(jLabelFirstName)
.addComponent(jTextFieldLastName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(jLabelLastName))
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabelPhoneNumber)
.addComponent(jTextFieldPhoneNumber, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabelEmailAddress)
.addComponent(jTextFieldEmailAddress, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jLabelStreet)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jTextFieldStreetNumber, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(jTextFieldStreetName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jTextFieldCityName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(jLabelCityName)
.addComponent(jLabelStateName)
.addComponent(jTextFieldStateName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(jLabelZipCode)
.addComponent(jTextFieldZipCode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(23, 23, 23)
.addComponent(jPanel2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-371)/2, (screenSize.height-419)/2, 371, 419);
// Establishes the action event listeners on the below objects and executes the actionPerformed method
jButtonSave.addActionListener(this);
jButtonDelete.addActionListener(this);
jButtonClear.addActionListener(this);
jButtonAdd.addActionListener(this);
jButtonForward.addActionListener(this);
jButtonBack.addActionListener(this);
jButtonExit.addActionListener(this);
} // End Constructor
/* Listener action method.
* Checks if the event source equals a button,
* then performs the desired function.
*/
public void actionPerformed (ActionEvent e)
{
if (e.getSource () == jButtonSave)
{
saveEntry();
clear();
}
else if (e.getSource() == jButtonDelete)
{
/*
* I need to be able to call the
* method deleteArrayValues in Class ArrayOperations.
* How do I pass it the object "myAddressBook"?
*/
clear();
}
else if (e.getSource() == jButtonAdd)
{
addPerson();
}
else if (e.getSource() == jButtonForward)
{
displayNextEntry();
}
else if (e.getSource() == jButtonBack)
{
displayPreviousEntry();
}
else if (e.getSource() == jButtonClear)
{
clear();
}
else if (e.getSource() == jButtonExit)
{
System.exit(0);
}
} // End actionPerformed method
public void displayCurrentEntry(AddressFields myAddressFields)
{
/* Trying to figure out why I can set
* and get the text, but it won't update
* in the display
*/
jTextFieldFirstName.setText(myAddressFields.getFirstName(myAddressFields.getEntryNumber()));
jTextFieldLastName.setText(myAddressFields.getLastName(myAddressFields.getEntryNumber()));
jTextFieldPhoneNumber.setText(myAddressFields.getPhoneNumber(myAddressFields.getEntryNumber()));
jTextFieldEmailAddress.setText(myAddressFields.getEmailAddress(myAddressFields.getEntryNumber()));
jTextFieldStreetNumber.setText(myAddressFields.getStreetNumber(myAddressFields.getEntryNumber()));
jTextFieldStreetName.setText(myAddressFields.getStreetName(myAddressFields.getEntryNumber()));
jTextFieldCityName.setText(myAddressFields.getCityName(myAddressFields.getEntryNumber()));
jTextFieldStateName.setText(myAddressFields.getStateName(myAddressFields.getEntryNumber()));
jTextFieldZipCode.setText(myAddressFields.getZipCode(myAddressFields.getEntryNumber()));
jPanel1.repaint();
} // End displayEntry method
// Save the Person into the Address Book
public void saveEntry()
{
// Action for saving
}
public void deleteEntry()
{
}
//Perform a Case-Insensitive Search to find the Person
public void addPerson()
{
// Action for adding a record
} // End searchPerson method
public void displayNextEntry()
{
// Code for moving to the next record
} // End displayNextEntry method
public void displayPreviousEntry()
{
// Code for moving to the previous record
} // End displayPreviousEntry method
public void clear()
{
jTextFieldFirstName.setText("");
jTextFieldLastName.setText("");
jTextFieldPhoneNumber.setText("");
jTextFieldEmailAddress.setText("");
jTextFieldStreetNumber.setText("");
jTextFieldStreetName.setText("");
jTextFieldCityName.setText("");
jTextFieldStateName.setText("");
jTextFieldZipCode.setText("");
} // End clear method
} // End Panel Class
Re: JTextField won't update
I might be wrong as that GroupLayout stuff is hard to follow, but it looks like you're sticking all the text widgets into the contentPane and not jPanel1.
Your redraw is called on jPanel1?
jPanel1 is never added to the frame (and does not hold those fields).