Editing JTextFields using button clicks
I have to create a GUI that works to navigate through an already created list of plants in another class. The user should be able to cycle through the plants in the in the list, and edit the list with the GUI. I have the GUI set up, but I can't figure out how to display new plant info in each field for a button click.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.io.*;
public class Interface extends JFrame {
JButton nextButton = new JButton("Next");
JButton previousButton = new JButton("Previous");
JButton editButton = new JButton("Edit Plant");
JTextField commonNameField = new JTextField("Common Name");
JTextField scientificNameField = new JTextField("Scientific Name");
JTextField plantTypeField = new JTextField("Plant Type");
JTextField lightingField = new JTextField("Lighting");
JTextField heightField = new JTextField("Height");
JTextField priceField = new JTextField("Price");
public Interface() {
setLayout(new FlowLayout());
add(nextButton);
add(previousButton);
add(editButton);
add(commonNameField);
add(scientificNameField);
add(plantTypeField);
add(lightingField);
add(heightField);
add(priceField);
nextButton.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
}
}
);
previousButton.addActionListener(new ActionListener () {
public void actionPerformed (ActionEvent e) {
}
}
);
editButton.addActionListener(new ActionListener () {
public void actionPerformed (ActionEvent e) {
}
}
);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Interface frame = new Interface();
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Plant Interface");
}
}
# 1
I have just done the navigation part, you can fill the Data array with any data. To edit the values just put edited values to that array.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.io.*;
public class Interface extends JFrame {
static int navigate=0;
JButton nextButton = new JButton("Next");
JButton previousButton = new JButton("Previous");
JButton editButton = new JButton("Edit Plant");
JTextField commonNameField = new JTextField("Common Name");
JTextField scientificNameField = new JTextField("Scientific Name");
JTextField plantTypeField = new JTextField("Plant Type");
JTextField lightingField = new JTextField("Lighting");
JTextField heightField = new JTextField("Height");
JTextField priceField = new JTextField("Price");
public Interface() {
setLayout(new FlowLayout());
add(nextButton);
add(previousButton);
add(editButton);
add(commonNameField);
add(scientificNameField);
add(plantTypeField);
add(lightingField);
add(heightField);
add(priceField);
nextButton.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
if(navigate<=3){
commonNameField.setText(DataList.Data[navigate][0]);
scientificNameField.setText(DataList.Data[navigate][1]);
plantTypeField.setText(DataList.Data[navigate][2]);
lightingField.setText(DataList.Data[navigate][3]);
heightField.setText(DataList.Data[navigate][4]);
priceField.setText(DataList.Data[navigate][5]);
navigate++;
}
}
}
);
previousButton.addActionListener(new ActionListener () {
public void actionPerformed (ActionEvent e) {
if(navigate>0){
navigate--;
commonNameField.setText(DataList.Data[navigate][0]);
scientificNameField.setText(DataList.Data[navigate][1]);
plantTypeField.setText(DataList.Data[navigate][2]);
lightingField.setText(DataList.Data[navigate][3]);
heightField.setText(DataList.Data[navigate][4]);
priceField.setText(DataList.Data[navigate][5]);
}
}
}
);
editButton.addActionListener(new ActionListener () {
public void actionPerformed (ActionEvent e) {
}
}
);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Interface frame = new Interface();
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Plant Interface");
}
}
class DataList{
public static String [] []Data={{"Common1","Scientific1","Type1","L1","H1","P1"},{"Common2","Scientific2","Type2","L2","H2","P2"},{"Common3","Scientific3","Type3","L3","H3","P3"},{"Common4","Scientific4","Type4","L4","H4","P4"}};
}