I'm writing an IM client GUI for a project. On the left of the window, I want a scrolling pane that has radio buttons for each active user, so that when a New Chat button is pushed, a chat will be created with the selected user.
I've implemented a sample GUI with 3 given users. I create a JRadioButton for each, set an ActionCommand and an ActionListener, add it to the ButtonGroup and then add it to 'this', which is a class extending JScrollPane. However, when I run the code, I see only an empty frame on the left hand side, with no buttons present. Can anybody explain? Relevant code is below.
package gui;
import javax.swing.*;
public class ActiveList extends JScrollPane implements ActionListener {
private ButtonGroup group;
private String selected;
public ActiveList() {
//TODO: will eventually need access to Server's list of active usernames
String[] usernames = {"User1", "User2", "User3"};
ButtonGroup group = new ButtonGroup();
this.group = group;
for (String name: usernames) {
JRadioButton button = new JRadioButton(name);
button.setActionCommand(name);
button.addActionListener(this);
this.group.add(button);
this.add(button);
}
}
public String getSelected() {
return this.selected;
}
@Override
public void actionPerformed(ActionEvent e) {
this.selected = e.getActionCommand();
System.out.println(e.getActionCommand());
}
}
The main method that I'm running comes from another class, ChatGUI.java. The ConversationsPane container is another class in my GUI, which is working correctly.
package gui;
import javax.swing.*;
public class ChatGUI extends JFrame {
private ConversationsPane convos;
private ActiveList users;
public ChatGUI() {
ConversationsPane convos = new ConversationsPane();
this.convos = convos;
ActiveList users = new ActiveList();
this.users = users;
GroupLayout layout = new GroupLayout(this.getContentPane());
this.getContentPane().setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addComponent(users, 100, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(convos)
);
layout.setVerticalGroup(
layout.createParallelGroup()
.addComponent(users)
.addComponent(convos)
);
}
public static void main(String[] args) {
ChatGUI ui = new ChatGUI();
ui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ui.setVisible(true);
ui.setSize(800,400);
ui.convos.newChat("Chat A");
}
}
This is not how scroll panes work. You do not "add" components to them. You set A component to the scroll panes view port
Instead of extending JScrollPane
, as you are adding no value to it, try doing something more like...
JScrollPane scrollPane = new JScrollPane();
JPanel view = new JPanel(new GridLayout(0, 1));
String[] usernames = {"User1", "User2", "User3"};
ButtonGroup group = new ButtonGroup();
this.group = group;
for (String name: usernames) {
JRadioButton button = new JRadioButton(name);
button.setActionCommand(name);
button.addActionListener(this);
this.group.add(button);
view.add(button);
}
scrollPane.setViewportView(view);
// Add scrollpane to WEST position of main view
Instead...
Take a look at How to use scroll panes for more details...