Your problems include:
- You're never setting the fileName of the train class (check it -- where do you ever call
fileName = something
?)
- You're creating new train objects and expecting that the new object will be able to sense changes made to other train objects. Java doesn't work that way. You need to test the same object that you change.
- Your program naming conventions are confusing and dangerous. Remember that class names begin with an upper letter. Don't give variables the same names as unrelated class, such as your train class and your train button.
- Your program has a lot of unnecessary complexity such as parallel arrays that make debugging and enhancing difficult.
- You're swapping contents of a JPanel instead of using the much easier to use CardLayout which will do the swapping for you.
For example, you could encapsulate your JButton string names and file names in a class or enum. The advantage of using a class, is then you could read in the data from a file and create objects of the class from the file data, giving extra flexibility, however for purposes of my example, and to avoid adding extra files, I'll use an enum, something like
public enum ButtonFileName {
MONDAY("Monday", KeyEvent.VK_M, "monday_blues.jpg"),
TUESDAY("Tuesday", KeyEvent.VK_T, "tuesday_fun.jpg"),
WEDNESDAY("Wednesday", KeyEvent.VK_W, "humpday.jpg"),
THURSDAY("Thursday", KeyEvent.VK_H, "almost_friday.jpg");
private String buttonText;
private String fileName;
private int mnemonic;
private ButtonFileName(String buttonText, int mnemonic, String fileName) {
this.buttonText = buttonText;
this.mnemonic = mnemonic;
this.fileName = fileName;
}
public String getButtonText() {
return buttonText;
}
public int getMnemonic() {
return mnemonic;
}
public String getFileName() {
return fileName;
}
}
Then I can give my main class a ButtonFileName variable, selectedButtonFileName, and then fill that variable if a button is pressed. I can also create my own Actions, one for each ButtonFileName enum, and use these Actions to set my buttons. For example:
import java.awt.CardLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class Home2 extends JPanel {
private static final String TRAIN_PANEL = "train panel";
private static final String BTN_ROW_PANEL = "btn row panel";
private CardLayout cardLayout = new CardLayout();
private ButtonFileName selectedButtonFileName = null;
private JPanel buttonRowPanel = new JPanel();
private JPanel trainPanel = new JPanel();
public Home2() {
buttonRowPanel = createButtonRowPanel();
trainPanel = createTrainPanel();
setLayout(cardLayout);
add(buttonRowPanel, buttonRowPanel.getName());
add(trainPanel, trainPanel.getName());
}
private JPanel createButtonRowPanel() {
JPanel btnRowPanel = new JPanel(new GridLayout(1, 0, 5, 5));
btnRowPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
btnRowPanel.setName(BTN_ROW_PANEL);
for (ButtonFileName btnFileName : ButtonFileName.values()) {
JButton btn = new JButton(new RowButtonAction(btnFileName, this));
btnRowPanel.add(btn);
}
return btnRowPanel;
}
private JPanel createTrainPanel() {
JPanel trainPanel = new JPanel();
trainPanel.setName(TRAIN_PANEL);
JButton trainBtn = new JButton(new TrainAction("Train", KeyEvent.VK_T, this));
trainPanel.add(trainBtn);
return trainPanel;
}
public ButtonFileName getSelectedButtonFileName() {
return selectedButtonFileName;
}
public void setSelectedButtonFileName(ButtonFileName selectedButtonFileName) {
this.selectedButtonFileName = selectedButtonFileName;
}
public void nextCardLayoutView() {
cardLayout.next(this);
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Home2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Home2());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Again the enum
enum ButtonFileName {
MONDAY("Monday", KeyEvent.VK_M, "monday_blues.jpg"),
TUESDAY("Tuesday", KeyEvent.VK_T, "tuesday_fun.jpg"),
WEDNESDAY("Wednesday", KeyEvent.VK_W, "humpday.jpg"),
THURSDAY("Thursday", KeyEvent.VK_H, "almost_friday.jpg");
private String buttonText;
private String fileName;
private int mnemonic;
private ButtonFileName(String buttonText, int mnemonic, String fileName) {
this.buttonText = buttonText;
this.mnemonic = mnemonic;
this.fileName = fileName;
}
public String getButtonText() {
return buttonText;
}
public int getMnemonic() {
return mnemonic;
}
public String getFileName() {
return fileName;
}
}
My Action for the train JButton
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
@SuppressWarnings("serial")
public class TrainAction extends AbstractAction {
private Home2 home;
public TrainAction(String name, int mnemonic, Home2 home) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
this.home = home;
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Selected File Name: "
+ home.getSelectedButtonFileName().getFileName());
home.nextCardLayoutView();
}
}
My Action for the row buttons
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
@SuppressWarnings("serial")
public class RowButtonAction extends AbstractAction {
private ButtonFileName btnFileName;
private Home2 home;
public RowButtonAction(ButtonFileName btnFileName, Home2 home) {
super(btnFileName.getButtonText());
putValue(MNEMONIC_KEY, btnFileName.getMnemonic());
this.btnFileName = btnFileName;
this.home = home;
}
@Override
public void actionPerformed(ActionEvent e) {
home.setSelectedButtonFileName(btnFileName);
home.nextCardLayoutView();
}
}
As for flexibility, say I wanted to add another JButton and file name. Add a Friday element to the enum and it's done.