I have a homework assignment that provides a GUI interface and displays ten teams, each represented by a button. Each button has a radio button to the left of it that allows the user to select the radio and then click another teams button to move the selected team to that position. The selected team will move and the other teams will shift either up or down.
This is not the issue. The code is complete but, I have a problem. Currently it works but after each click of a button the program makes a new call to the constructor which then opens another instance of the program, but the data shifts correctly.
I'm looking for a suggestion on how to make this work. I tried to make it call the method that creates the GUI but it doesn't work properly and throws a nasty exception.
My code is below and the area of concern is line 99 and 100. Line 99 is commented out. It is what I tried instead of the new constructor call on line 100.
I hope this makes sense and thanks in advance for any suggestions.
Note: (I'm not looking for someone to complete my homework for me. I have completed this code from scratch and I have exhausted myself trying to figure this thing out. Thanks!)
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class P03TopTenTeams extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
String[] teams = {"Texas", "Vanderbilt", "Florida", "Missouri","Arkansas", "Kentucky", "Alabama", "Tennessee", "Georgia","Louisiana"};
new P03TopTenTeams(teams);
}
private JPanel panel1;
private JPanel panel2;
private JButton[] btn = new JButton[10];
private JRadioButton[] rbtn = new JRadioButton[10];
private ButtonGroup bg1 = new ButtonGroup();
private ButtonGroup bg2 = new ButtonGroup();
public P03TopTenTeams(String[] teams) {
buildWindow(teams);
}
private void buildWindow(String[] teams) {
int index;
this.setSize(300, 500);
this.setLocationRelativeTo(null);
this.setTitle("Top Ten Teams");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
panel1 = new JPanel();
panel1.setLayout(new GridLayout(10, 1));
for (index = 0; index < 10; index++) {
rbtn[index] = new javax.swing.JRadioButton(
Integer.toString(index + 1));
bg1.add(rbtn[index]);
panel1.add(rbtn[index]);
}
panel2 = new JPanel();
panel2.setLayout(new GridLayout(10, 1));
BtnHdlr btnhdlr = new BtnHdlr();
for (index = 0; index < 10; index++) {
btn[index] = new javax.swing.JButton(teams[index]);
bg2.add(btn[index]);
panel2.add(btn[index]);
btn[index].addActionListener(btnhdlr);
}
this.add(panel1, BorderLayout.WEST);
this.add(panel2, BorderLayout.CENTER);
this.setVisible(true);
}
private class BtnHdlr implements ActionListener {
public void actionPerformed(ActionEvent event) {
int from = 0;
int to = 0;
String[] teams = new String[10];
int index;
for (index = 0; index < bg1.getButtonCount(); index++) {
teams[index] = btn[index].getText();
if (rbtn[index].isSelected() == true) {
from = index;
}
if (btn[index].getText().equals(event.getActionCommand())) {
to = index;
}
}
updateTeamsArray(teams, from, to);
}
}
private void updateTeamsArray(String[] teams, int from, int to) {
int index;
String valueFrom = teams[from];
if (from < to) {
for (index = from; index < to; index++) {
teams[index] = teams[index + 1];
}
teams[to] = valueFrom;
} else {
for (index = from; index > to; index--) {
teams[index] = teams[index - 1];
}
teams[to] = valueFrom;
}
//buildWindow(teams);
new P03TopTenTeams(teams);
}
}