I am trying to access java swing components from a different class. For example, I need to change the text on a button once a specific operation has been completed. I am trying to use "getters" and "setters" to do the operation(at the bottom of the section of code.) Right now I only want to "set" the text.
I have another class that calls the "set" method and tries to set the text of the chosen button. This is the main class. The line of code that is "gui.setProcessItemBtn().setText("Some Text");" Throws:
Exception in thread "main" java.lang.NullPointerException
I believe this means that there isnt anything to set the text to. Am i missing something to link my setter methods to the actual gui components?
Main class
package book;
import book.UserInterface;
/**
*
* @author KJ4CC
*/
public class Book {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
UserInterface gui = new UserInterface();
gui.startUI();
gui.setProcessItemBtn().setText("Some Text");
}
}
User Interface class
public class UserInterface extends JFrame {
private JButton processItem;
private JButton confirmItem;
private JButton viewOrder;
private JButton finishOrder;
private JButton newOrder;
private JButton exit;
public void startUI(){
UserInterface gui = new UserInterface();
gui.bookingUI();
}
public static void bookingUI(){
//sets windows, and pane in the UI
JFrame frame = new JFrame("Ye old Book stoppe");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel toppane = new JPanel(new GridBagLayout());
JPanel bottomPane = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
frame.setSize(800, 300);
frame.setVisible(true);
//adds labels to the window
//----------------------------------------------------------BUTTOM PANE-------------------------
//setting up buttons to be placed onto the bottompanel
JButton processItem = new JButton("Process Item");
JButton confirmItem = new JButton("Confirm Item");
JButton viewOrder = new JButton("View Order");
JButton finishOrder = new JButton("Finish Order ");
JButton newOrder = new JButton("New Order");
JButton exit = new JButton("Exit");
//adding the buttons to the pane.---------------------------------------------------------------
GridBagConstraints b = new GridBagConstraints();
b.insets = new Insets(5,5,5,5);
b.ipadx = 10;
b.ipady = 10;
b.gridx = 1;
b.gridy = 0;
bottomPane.add(processItem, b);
b.gridx = 2;
b.gridy = 0;
bottomPane.add(confirmItem,b);
confirmItem.setEnabled(false);
b.gridx = 3;
b.gridy = 0;
bottomPane.add(viewOrder, b);
viewOrder.setEnabled(false);
b.gridx = 4;
b.gridy = 0;
bottomPane.add(finishOrder,b);
finishOrder.setEnabled(false);
b.gridx = 5;
b.gridy = 0;
bottomPane.add(newOrder,b);
b.gridx = 6;
b.gridy = 0;
bottomPane.add(exit, b);
bottomPane.setBackground(Color.BLUE);
frame.add(bottomPane,BorderLayout.SOUTH);
frame.setSize(810, 310);
}
//Creating getters and setters to change the text for the buttons and labels.
public JButton setProcessItemBtn(){
return processItem;
}
public JButton setConfirmItemBtn(){
return confirmItem;
}
public JButton setViewOrderbtn(){
return viewOrder;
}
public JButton setFinishOrderBtn(){
return finishOrder;
}
public JButton setNewOrderBtn(){
return newOrder;
}
public JButton setsetExitBtn(){
return exit;
}