Will also need to make a pop up asking for the new users name when adding a player, but i'll get to that later. Am i doing the buttons right?
I figured i would need to set new variables so it know which KI is selected and which player, but im kinda of confused how you did the radio buttons for the players using the tables, im not really good with them.
Code:
private class AddKIListener implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
String selection = "";
String player = "";
if(fatSkin.isSelected()) {
selection = "Fat-lined Cockatrice Skin";
}
else if(manticoreMane.isSelected()) {
selection = "Luxuriant Manticore Mane";
}
else if(wormHusk.isSelected()) {
selection = "Sodden Sandworm Husk";
}
else if(adzeWing.isSelected()) {
selection = "Sticky Gnat Wing";
}
//add methods to find which player is selected
// print to the file the selected KI for the selected player
}
}
Gonna head to bed for the night and work on it tomorrow i'm off all day. Heres what i got so far.
Update:
-Started working on KI add/remove actionlisteners
- Now writes to glavoidPlayer.txt when add player is added (I think the list should atuo update its self the way its wrote sence its rewriting the file each time.
- JOptionPane now pops up when hitting add Player
- saves added players name to txt file (currently overwrites need to fix to simply add)
Code:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import java.io.*;
import javax.swing.JOptionPane;
public class KItracker extends JFrame {
JRadioButton manticoreMane, wormHusk, adzeWing, fatSkin;
JButton addki, removeki, addp, removep ;
JPanel topPanel;
JPanel playerPanel;
ButtonGroup kIButtonGroup;
ButtonGroup playerButtonGroup;
AddKIListener addKI;
RemoveKIListener removeKI;
AddPlayerListener addPlayer;
RemovePlayerListener removePlayer;
ArrayList<String[]> dummyData;
DefaultTableModel playerDataModel;
JTable playerDataTable;
String[] tableNames;
public KItracker() {
//making some dummy data. This will come from your other class that reads in the file/maintains an updated list.
String[] ayleeSt = {"Aylee","0","0","0","0"};
String[] blackmassSt = {"Blackmass","0","0","0","0"};
String[] meikaSt = {"Meika","0","0","0","0"};
String[] spitfyreSt = {"Spitfyre","0","0","0","0"};
dummyData = new ArrayList<String[]>();
dummyData.add(ayleeSt);
dummyData.add(blackmassSt);
dummyData.add(meikaSt);
dummyData.add(spitfyreSt);
addKI = new AddKIListener();
removeKI = new RemoveKIListener();
addPlayer = new AddPlayerListener();
removePlayer = new RemovePlayerListener();
kIButtonGroup = new ButtonGroup();
playerButtonGroup = new ButtonGroup();
playerPanel = new JPanel();
playerPanel.setLayout(new BoxLayout(playerPanel,BoxLayout.Y_AXIS));
JScrollPane playerPanelScrollPane = new JScrollPane(playerPanel);
playerPanelScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
playerPanelScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
topPanel = new JPanel();
this.setLayout(new BorderLayout());
JPanel blank = new JPanel();
blank.setLayout(new FlowLayout());
JPanel blank2 = new JPanel();
blank2.setLayout(new GridLayout(2,0));
JPanel subPanelA = new JPanel();
subPanelA.setLayout(new BorderLayout());
JPanel subPanelB = new JPanel();
subPanelB.setLayout(new BorderLayout());
JPanel popList2 = new JPanel();
popList2.setLayout(new GridLayout(4,0));
JPanel kiPanel = new JPanel();
kiPanel.setLayout(new GridLayout(4,0));
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
tableNames = new String[] {"Player Name",
"Fat-lined Cockatrice Skin",
"Luxuriant Manticore Mane",
"Sodden Sandworm Husk",
"Sticky Gnat Wing"};
//Populate the table!
drawPlayerTable();
//stick the table in a scroller
JScrollPane tableScroller = new JScrollPane(playerDataTable);
tableScroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
tableScroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
fatSkin = new JRadioButton("Fat-lined Cockatrice Skin", true);
manticoreMane = new JRadioButton("Luxuriant Manticore Mane", false);
wormHusk = new JRadioButton("Sodden Sandworm Husk", false);
adzeWing = new JRadioButton("Sticky Gnat Wing", false);
kIButtonGroup.add(fatSkin);
kIButtonGroup.add(manticoreMane);
kIButtonGroup.add(wormHusk);
kIButtonGroup.add(adzeWing);
addki = new JButton("Add KI");
addki.addActionListener(addKI);
removeki = new JButton("Remove KI");
removeki.addActionListener(removeKI);
addp = new JButton("Add Player");
addp.addActionListener(addPlayer);
removep = new JButton("Remove Player");
removep.addActionListener(removePlayer);
addPlayerRadioButtons();
kiPanel.add(fatSkin);
kiPanel.add(manticoreMane);
kiPanel.add(wormHusk);
kiPanel.add(adzeWing);
buttonPanel.add(addki);
buttonPanel.add(removeki);
buttonPanel.add(addp);
buttonPanel.add(removep);
this.add(tableScroller, BorderLayout.NORTH);
this.add(kiPanel, BorderLayout.WEST);
//this.add(blank, BorderLayout.CENTER);
this.add(playerPanelScrollPane, BorderLayout.EAST);
this.add(buttonPanel, BorderLayout.SOUTH);
}
private class AddKIListener implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
String selection = "";
String player = "";
if(fatSkin.isSelected()) {
selection = "Fat-lined Cockatrice Skin";
}
else if(manticoreMane.isSelected()) {
selection = "Luxuriant Manticore Mane";
}
else if(wormHusk.isSelected()) {
selection = "Sodden Sandworm Husk";
}
else if(adzeWing.isSelected()) {
selection = "Sticky Gnat Wing";
}
//add methods to find which player is selected
// print to the file the selected KI for the selected player
}
}
private class RemoveKIListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String selection = "";
if(fatSkin.isSelected()) {
selection = "Luxuriant Manticore Mane";
}
else if(manticoreMane.isSelected()) {
selection = "Luxuriant Manticore Mane";
}
else if(wormHusk.isSelected()) {
selection = "Sodden Sandworm Husk";
}
else if(adzeWing.isSelected()) {
selection = "Sodden Sandworm Husk";
}
}
}
private class AddPlayerListener implements ActionListener{
public void actionPerformed(ActionEvent event) {
String chrName = JOptionPane.showInputDialog("Character Name :");
try{
FileWriter fstream = new FileWriter("glavoidPlayers.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write(chrName);
out.close();
}
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
private class RemovePlayerListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
/**
* Creates the buttons for players.
*/
private void addPlayerRadioButtons(){
for(String[] eachPlayer : dummyData){
System.out.println(eachPlayer[0]);
JRadioButton playerButton = new JRadioButton(eachPlayer[0]);
playerButtonGroup.add(playerButton);
playerPanel.add(playerButton);
}
}
/**
* Creates the table for the players and their items.
*/
private void drawPlayerTable(){
String [][] dummyDataAry = new String[dummyData.size()][5];
int row = 0;
for(String[] eachPlayer : dummyData){
dummyDataAry[row][0] = dummyData.get(row)[0];
dummyDataAry[row][1] = dummyData.get(row)[1];
dummyDataAry[row][2] = dummyData.get(row)[2];
dummyDataAry[row][3] = dummyData.get(row)[3];
dummyDataAry[row][4] = dummyData.get(row)[4];
row++;
}
playerDataModel = new DefaultTableModel(dummyDataAry,tableNames);
playerDataTable = new JTable(playerDataModel){
public boolean isCellEditable(int rowIndex, int colIndex){
return false;
}
};
}
public static void main(String[] args) {
KItracker win = new KItracker();
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.pack();
win.setVisible(true);
}
}