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
			
		}

	}

Recommended Answers

All 3 Replies

1. Have a public method in LView that performs the add function as you need it.
2. When the TitleView instance is created, pass the LView instance to it as a parameter in the constructor. Save a copy of the reference in an instance variable.
3. In your actionPerformed you can now call the LView's public method

Like this?
1) Public method in Lview

public void addPlayers(){
		String name = JOptionPane.showInputDialog(playerModel, "Enter Name" );
        
        // add new playerModel
		playerModel.addElement(name);
	}

Create instance and call the LViews public method

private class LobbyListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			LView lv = new LView(m);
			lv.addPlayers();
		    
			rmv.switchViews(Views.L);
			
		}

	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.