GUI class with the list. What i want to do is have another class add to this list when button is clicked but im not sure how to do that.
public class LView extends MasterViewPanel{
private static final long serialVersionUID = 1L;
private JButton host, join, quit, start;
private JLabel plLabel, title, title2;
private JList players, square;
private DefaultListModel playerModel;
private LobbyModel lm;
private Player pl;
public LView(RiskMasterView m) {
super(m);
setUpLayout();
setUpLists();
setUpButtons();
setUpLabels();
}
private void setUpLayout() {
this.setLayout(null);
this.setBackground(Color.black);
this.setSize(640,480);
}
private void setUpLabels() {
plLabel = new JLabel("Player List");
plLabel.setSize(400,150);
plLabel.setLocation(530, 100);
plLabel.setFont(new Font("Roman",1, 30));
plLabel.setForeground(Color.white);
this.add(plLabel);
}
private void setUpLists() {
playerModel = new DefaultListModel();
players = new JList(playerModel);
players.setSize(150,250);
players.setLocation(535,200);
this.add(players);
}
public void addPlayers(){
String name = JOptionPane.showInputDialog(playerModel, "Enter Name" );
// add new playerModel
playerModel.addElement(name);
}
This is the class with the button
public class TitleView extends MasterViewPanel {
RiskMasterView rmv;
public TitleView(RiskMasterView m) {
super(m);
rmv = m;
setUpGui();
this.setBackground(getForeground());
}
private JButton lobby;
private JButton exit;
private void setUpGui() {
JPanel titlePanel = new JPanel(new GridLayout(1,2));
//Set Layout and background
//Set Up buttons
lobby = new JButton("Join lobby");
lobby.addActionListener(new LobbyListener());
this.add(lobby);
exit = new JButton("Exit");
exit.addActionListener(new ExitListener());
this.add(exit);
}
private class LobbyListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
//I want to put the code here to add to the JList in the other class
rmv.switchViews(Views.L);//switch view to L
}
}