I know the solution is using a for loop to step through the array and display in a pane. However I am not finding an straight forward explanations on this. I need a next and a previous button that displays each array element, and just returns to the first element once it reaches the end when the next button is pressed.
for ( int i = 0; i < initem.length; i++ ){
JOptionPane.showMessageDialog( null, initem[i]);
}
initem is the name of my array.
Incorporated ActionListerner
class RecordViewer extends JDialog
{
private JButton next;
private JButton prev;
private JLabel label = new JLabel();
private int current = 0;
private CDinventoryItem [] items;
public RecordViewer(CDinventoryItem [] array){
super();
items = array;
label = this.setLabel(items[Current()]);
next = new JButton("Next");
next.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if( getCurrent() == items.length ){
setCurrent(0);
}else{
setCurrent(getCurrent() + 1);
}
setTitle("Inventory Item");
setSize(1200, 300);
setLocation(200,200);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
getLabel().setText(items[getCurrent()].toString());
}
});
prev = new JButton("Previous");
prev.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if( getCurrent() == 0){
setCurrent(items.length - 1);
}else{
setCurrent(getCurrent() - 1);
}
setTitle("Inventory Item");
setSize(1200, 300);
setLocation(200,200);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
getLabel().setText(items[getCurrent()].toString());
}
});
setLayout(new FlowLayout());
add(label);
add(next);
add(prev);
pack();
this.setVisible(true);
}
public JButton getNext() {
return next;
}
public void setNext(JButton next) {
this.next = next;
}
public JButton getPrev() {
return prev;
}
public void setPrev(JButton prev) {
this.prev = prev;
}
public JLabel getLabel() {
return label;
}
private int getCurrent() {
return current;
}
public void setCurrent(int current) {
this.current = current;
}
private JLabel setLabel(CDinventoryItem cDinventoryItem) {
return label;
}
private int Current() {
return current;
}}
Here is a simple example that leaves out all the extraneous details.
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
public class RecordViewer extends JDialog {
String items[] = {"One", "Two", "Three"};
JButton next, prev;
JLabel label;
int current;
public RecordViewer(){
super();
current = 0;
label = new JLabel(items[current]);
next = new JButton("Next");
next.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(current == items.length - 1){
current = 0;
}else{
current++;
}
label.setText(items[current]);
}
});
prev = new JButton("Previous");
prev.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(current == 0){
current = items.length - 1;
}else{
current--;
}
label.setText(items[current]);
}
});
setLayout(new FlowLayout());
add(label);
add(next);
add(prev);
pack();
this.setVisible(true);
}
}
To view this dialog you can call:
new RecordViewer();
This example uses a simple String array and simply cycles through the array using two buttons and a simple layout. You can modify it to display your array and use a more advanced layout to make it look better.
UPDATE:
You can try this:
public class RecordViewer extends JDialog {
JButton next, prev;
JLabel label;
int current;
CDinventoryItem [] items;
public RecordViewer(CDinventoryItem [] array)
{
super();
current = 0;
items = array;
....
And see if that helps.