Reputation: 1
I am attempting to get the next set of values to display in the JTextFields after hitting the next button. I am new to programming so I am not really sure what I am missing. What I want to occur is when I hit the next button when the window displays the next set of values will now display in the appropriate JTextFields unfortunately what ends up happening is nothing. I have tried several different ways of getting this to work and so far nothing. I know it is not the button its self because if I change the actionPerformed next to say setTitle(“5000”) it will change the title within the window. Any help is appreciated.
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.Arrays;
import javax.swing.*;
import java.awt.*;
public class GUI extends JFrame implements ActionListener {
JButton next;
JButton previous;
JButton first;
JButton last;
private JLabel itmNum = new JLabel("Item Number: ", SwingConstants.RIGHT);
private JTextField itemNumber;
private JLabel proNm = new JLabel ("Product Name: ", SwingConstants.RIGHT);
private JTextField prodName;
private JLabel yr = new JLabel("Year Made: ", SwingConstants.RIGHT);
private JTextField year;
private JLabel unNum = new JLabel("Unit Number: ", SwingConstants.RIGHT);
private JTextField unitNumber;
private JLabel prodPrice = new JLabel("Product Price: ", SwingConstants.RIGHT);
private JTextField price;
private JLabel restkFee = new JLabel("Restocking Fee", SwingConstants.RIGHT);
private JTextField rsFee;
private JLabel prodInValue = new JLabel("Product Inventory Value", SwingConstants.RIGHT);
private JTextField prodValue;
private JLabel totalValue = new JLabel("Total Value of All Products", SwingConstants.RIGHT);
private JTextField tValue;
private double toValue;
int nb = 0;
char x = 'y';
public GUI()
{
super ("Inventory Program Part 5");
setSize(800,800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLookAndFeel();
first = new JButton("First");
previous = new JButton("Previous");
next = new JButton("Next");
last = new JButton("Last");
first.addActionListener(this);
previous.addActionListener(this);
next.addActionListener(this);
last.addActionListener(this);
Movies[] titles = new Movies[9];
titles [0] = new Movies(10001, "King Arthur", 25 , 9.99, 2004, .05);
titles [1] = new Movies(10002,"Tron", 25, 7.99, 1982, .05);
titles [2] = new Movies(10003, "Tron: Legacy",25,24.99,2010,.05);
titles [3] = new Movies(10004,"Braveheart", 25,2.50,1995,.05);
titles [4] = new Movies(10005,"Gladiator",25,2.50,2000,.05);
titles [5] = new Movies(10006,"CaddyShack SE",25,19.99,1980,.05);
titles [6] = new Movies (10007,"Hackers",25,12.50,1995,.05);
titles [7] = new Movies (10008,"Die Hard Trilogy",25,19.99,1988,.05);
titles [8] = new Movies (10009,"Terminator",25,4.99,1984,.05);
Arrays.sort (titles, DVD.prodNameComparator);
itemNumber.setText(Double.toString(titles[nb].getitemNum()));
prodName.setText(titles[nb].getprodName());
year.setText(Integer.toString(titles[nb].getYear()));
unitNumber.setText(Integer.toString(titles[nb].getunitNum()));
price.setText(Float.toString(titles[nb].getprice()));
rsFee.setText(Double.toString(titles[nb].getRestkFee()));
prodValue.setText(Double.toString(titles[nb].getprodValue()));
tValue.setText("2636");
setLayout(new GridLayout(8,4));
add(itmNum);
add(itemNumber);
add(proNm);
add(prodName);
add(yr);
add(year);
add(unNum);
add(unitNumber);
add(prodPrice);
add(price);
add(restkFee);
add(rsFee);
add(prodInValue);
add(prodValue);
add(totalValue);
add(tValue);
add(first);
add(previous);
add(next);
add(last);
setLookAndFeel();
setVisible(true);
}
public void updateFields()
{
itemNumber.getText();
prodName.getText();
year.getText();
unitNumber.getText();
price.getText();
rsFee.getText();
prodValue.getText();
tValue.getText();
}
public void actionPerformed(ActionEvent evt){
Object source = evt.getSource();
if (source == next)
{
nb++;
}
}
private void setLookAndFeel()
{
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
System.err.println("couln't use the system"+ "look and feel: " + e);
}
}
}
Upvotes: 0
Views: 183
Reputation: 4776
I think the logic of chaning JTextFields is inside the constructor of GUI class. So unless you are creating another object of GUI class, which I can see it is not being created. Your JTextFields will not be updated.
So basically to solve this problem you will have to move your logic of changing JTextFields inside another method like for example
public void updateFields(){
//place your logic code here
}
and then from actionPerformed method call this updateFields() method
public void actionPerformed(ActionEvent evt){
Object source = evt.getSource();
if (source == next)
{
nb =1;
}
updateFields();
///repaint(); /// you don't need the repaint method
}
As per your question in the comments and as far as my understanding is concerned place your titles array outside the constructor, and do the following changes so it will look something like this:
private JTextField tValue;
private double toValue;
int nb = 0; //note initialize this to 0
Movies[] titles = new Movies[9];//this is the line that comes out of the Constructor
public GUI()
{
///inside the constructor do the following changes
for (int i = 0; i < titles.length; i ++)
{
toValue += (titles[i].gettotalVal());
}
//note I will be accessing the 0th position of the array assuming you need the contents of the first objects to be displayed
itemNumber = new JTextField(Double.toString(titles[0].getitemNum()));
prodName = new JTextField(titles[0].getprodName());
year = new JTextField(Integer.toString(titles[0].getYear()));
unitNumber = new JTextField (Integer.toString(titles[0].getunitNum()));
price = new JTextField (Float.toString(titles[0].getprice()));
rsFee = new JTextField (Double.toString(titles[0].getRestkFee()));
prodValue = new JTextField(Double.toString(titles[0].getprodValue()));
tValue = new JTextField ("2636");
nb = 0;
//if (nb == 0) you don't need a if statement
//{
next.addActionListener(this);
//}
///Now for the updateFields() method
public updateFields(){
nb++;
itemNumber.setText(Double.toString(titles[nb].getitemNum());
prodName.setText(titles[nb].getprodName());
year.setText(Integer.toString(titles[nb].getYear()));
unitNumber.setText(Integer.toString(titles[nb].getunitNum()));
price.setText(titles[nb].getprice()));
rsFee.setText(Double.toString(titles[nb].getRestkFee()));
prodValue.setText(Double.toString(titles[nb].getprodValue()));
}
Upvotes: 1
Reputation: 159844
Your ActionListener
only has one assignment
nb = 1;
and a repaint
. This does not update the JTextFields
with the values from your Movies
array titles
. You need to do this explicitly.
itemNumber.setText(Double.toString(titles[nb].getItemNum()));
...
To make this possible, you will need to make your Movies
titles
array a class member variable so that it can be accessed from your ActionListener
.
Also you never actually change the value of nb
in your ActionListener
. As it's a "next" button, you will probably want to increment it:
nb++;
Side Notes:
getitemNum
getItemNum
.ArrayList
would give you more flexibility for adding Movie titles over an array which is fixed in size.Upvotes: 2