I've been working on this utility for a while now, and have since gone through a couple reworks to try and increase the readability and best practices, but since I don't usually work a lot with Swing, or UI design in general, I still have the nagging feeling there's a better way to approach this. Code for both files is below along with a GitHub link in case full context is needed.
Specific Questions
- Is
GridBagUIPanel
a proper abstraction of functionality, or is it too little/too much? - Is there anything I should be doing to clean up init()/update() more in
LifePathUI
? - Am I using things right with the
TextChangeListener
s and Button Events inLifePathUI
? Defining classes inside method calls still feels very off to me. - Is there any glaring reinventing the wheel or other mistakes going on? I want to cut out any particularly embarrassing things since this is one of my few projects that would be able to be used in portfolios and such.
Additional Edit/Note: There is plans for additional UI/functions for this project, so code was brought outside of LifePathUI for reuse purposes as well. I just realized that would also be important context as to intent.
Main UI (LifePathUI)
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class LifePathUI implements UI {
final static boolean shouldWeightX = true;
final static String DIVIDER_STRING = "\n------------------------------------------\n";
// we still hardcode some stats like this because the page would break if they were user definable anyways
final static String[] primStats = {"COG","COO","INT","REF","SAV","SOM","WIL"};
final static String[] secStats = {"DUR","WT","DR","LUC","TT","IR","INIT","SPD","DB"};
private BorderLayout windowLayout;
private JFrame mainWindow;
private GridBagUIPanel mainPanel, statPanel,sideBar;
private JTextArea mainStatus;
private LifePathGenerator gen;
/**
* @throws HeadlessException
*/
public LifePathUI() throws HeadlessException {
DataProc.init("LifepathPackages.dat","internalInfo.dat");
gen = new LifePathGenerator("",this,true);
windowLayout = new BorderLayout();
mainWindow = new JFrame();
mainPanel = new GridBagUIPanel();
statPanel = new GridBagUIPanel();
mainWindow.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
}
/* (non-Javadoc)
* @see UI#promptUser(java.lang.String, java.lang.String)
*/
@Override
public String promptUser(String message, String extraContext) {
String prompt = message + "\n" + extraContext;
// check for a few things that lets us provide extra info
String[] result = DataProc.getExtraPromptOptions(message,extraContext);
if (result != null )
{
prompt += result[1];
}
String inputValue = "";
// for longer prompts we use a more proper scrollable UI object
// for short prompts we use a more compact/prettier/simpler one
if (prompt.length() > 3000)
{
JTextArea textArea = new JTextArea(prompt);
JScrollPane scrollPane = new JScrollPane(textArea);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
scrollPane.setPreferredSize( new Dimension( 800, 500 ) );
inputValue = JOptionPane.showInputDialog(null, scrollPane, "Enter Choice",
JOptionPane.QUESTION_MESSAGE);
}
else
{
inputValue = JOptionPane.showInputDialog(prompt);
}
return inputValue;
}
/* (non-Javadoc)
* @see UI#handleError(java.lang.String)
*/
@Override
public boolean handleError(String message) {
int resp = JOptionPane.showConfirmDialog(null,"Error Resulted, redo step?\n"+message, "Error", JOptionPane.YES_NO_OPTION);
if (resp == JOptionPane.YES_OPTION)
{
return true;
}
else
{
return false;
}
}
/* (non-Javadoc)
* @see UI#statusUpdate(java.lang.String)
*/
@Override
public void statusUpdate(String message) {
this.appendStatusText(message);
}
/* (non-Javadoc)
* @see UI#end()
*/
@Override
public void end() {
// Marks the character gen process as stopped, disabling the buttons that used to advance it
mainPanel.remove(mainPanel.getComponent("Run Next Step"));
mainWindow.revalidate();
mainWindow.repaint();
}
/**
* Main setup method for the UI, creates most UI components and initializes their default state
*/
public void init()
{
mainWindow.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
mainWindow.setLayout(windowLayout);
// to make everything work right we add a mainPanel under the mainWindow
mainWindow.add(mainPanel);
// start first row of rows of mixed size
mainPanel.addMappedTF(0,0,"Character Name",20,new TextChangeListener());
mainPanel.addMappedFixedTF(2,0,"Morph","",10,true);
mainPanel.addMappedFixedTF(4,0,"Background","",10,true);
mainPanel.addMappedFixedTF(6,0,"Natural Language", "",15,true);
mainPanel.addMappedFixedTF(8,0,"Faction","",10,true);
mainPanel.endRow(10,0);
// we init new Panel for the sidebar (skills displays)
sideBar = new GridBagUIPanel();
// addC is (component,x,y,height,width)
mainPanel.addC(sideBar,22,0,GridBagConstraints.REMAINDER,1);
mainPanel.addC(statPanel,0,1,6,GridBagConstraints.RELATIVE);
// stats are predictable in format and appearance, so we do them via loops
// Add first row, with the Base Primary stat values
int idx = 0;
for (String key : primStats)
{
statPanel.addMappedFixedTF(idx,0,"Base "+key, ""+gen.getPC().getAptitude(key),5,true);
idx +=2;
}
statPanel.endRow(idx,0);
// add row for bonuses
idx = 0;
for (String key : primStats)
{
statPanel.addMappedTF(idx, 1, "Bonus", "MorphBonus"+key, 5, new TextChangeListener());
idx +=2;
}
statPanel.endRow(idx,1);
// add final row for totals.
idx = 0;
for (String key : primStats)
{
statPanel.addMappedFixedTF(idx,2,"Total "+key, "",5,true);
idx +=2;
}
statPanel.endRow(idx,2);
// add row for base secondary stats
idx = 0;
for (String key : secStats)
{
statPanel.addMappedFixedTF(idx,3,key, ""+gen.getPC().getSecStat(key),5,true);
idx +=2;
}
statPanel.endRow(idx,3);
// now we do bonuses
idx = 0;
for (String key : secStats)
{
statPanel.addMappedTF(idx, 4, "Bonus", "MorphBonus"+key, 5, new TextChangeListener());
idx +=2;
}
statPanel.endRow(idx,4);
// now we do the totals
idx = 0;
for (String key : secStats)
{
statPanel.addMappedFixedTF(idx,5,"Total "+key, "",5,true);
idx +=2;
}
statPanel.endRow(idx,5);
// a few extra stats get factored in too
statPanel.addMappedFixedTF(0,6,"Stress", "",5,true);
statPanel.addMappedFixedTF(2,6,"MOX", "",5,true);
statPanel.addMappedFixedTF(4,6,"Credits", "",5,true);
statPanel.addMappedFixedTF(6,6,"Free CP", "",5,true);
statPanel.endRow(8,6);
// last bar is Rep values, which can vary based on configuration
int xIdx = 0;
for (Rep r : gen.getPC().getAllRep())
{
statPanel.addMappedFixedTF(xIdx,7,r.getName()+"-rep", ""+r.getValue(),5,true);
xIdx += 2;
}
statPanel.endRow(xIdx,7);
// create the main status window
mainStatus = new JTextArea(40,160);
mainStatus.setMinimumSize(mainStatus.getPreferredSize());
mainStatus.setMaximumSize(mainStatus.getPreferredSize());
mainStatus.setLineWrap(true);
mainStatus.setWrapStyleWord(true);
JScrollPane tempPane = new JScrollPane(mainStatus);
tempPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
tempPane.setMinimumSize(tempPane.getPreferredSize());
mainPanel.addC(tempPane,0,9,14,13,GridBagConstraints.BOTH);
mainPanel.addMappedButton(0,26,"Firewall Events").addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if (gen.getPC().hasVar("{firewall}"))
{
gen.getPC().removeVar("{firewall}");
mainPanel.setButtonText("Firewall Events","Firewall Events (Off)");
}
else
{
gen.getPC().setVar("{firewall}", "1");
mainPanel.setButtonText("Firewall Events","Firewall Events (On)");
}
}
});
// set this afterwards so it doesn't change the mapping name
mainPanel.setButtonText("Firewall Events","Firewall Events (Off)");
mainPanel.addMappedButton(1,26,"Gatecrashing Events").addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if (gen.getPC().hasVar("{gatecrashing}"))
{
gen.getPC().removeVar("{gatecrashing}");
mainPanel.setButtonText("Gatecrashing Events","Gatecrashing Events (Off)");
}
else
{
gen.getPC().setVar("{gatecrashing}", "1");
mainPanel.setButtonText("Gatecrashing Events","Gatecrashing Events (On)");
}
}
});
// set this afterwards so it doesn't change the mapping name
mainPanel.setButtonText("Gatecrashing Events","Gatecrashing Events (Off)");
mainPanel.addMappedButton(3,26,"Run Next Step").addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
gen.step();
update();
}
});
// gives a quick export of the character
mainPanel.addButton(5,26,"Export to Txt").addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
update();
JTextArea updateArea= new JTextArea(gen.getPC().toString() + DIVIDER_STRING + mainStatus.getText(),10,120);
updateArea.setEditable(true);
updateArea.setLineWrap(true);
JScrollPane scroll = new JScrollPane (updateArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JOptionPane.showMessageDialog(null, scroll,"Exported Character", JOptionPane.PLAIN_MESSAGE);
}
});
// gives a quick export of the character
mainPanel.addMappedButton(7,26,"Rolling").addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if (gen.isRolling())
{
gen.setRolling(false);
mainPanel.setButtonText("Rolling","Choosing Rolls");
}
else
{
gen.setRolling(true);
mainPanel.setButtonText("Rolling","Rolling");
}
}
});
mainPanel.endVertical(0,27);
this.update();
mainWindow.setSize(1700, 1000);
mainWindow.setVisible(true);
}
/**
* Adds text to the end of the status text area
* @param text Text to be added
*/
private void appendStatusText(String text)
{
this.mainStatus.setText(this.mainStatus.getText() + "\n\n" + text);
}
/**
* Updates all relevant display fields for the character
*/
private void update()
{
gen.getPC().setName(mainPanel.getTextF("Character Name").getText());
gen.getPC().calcStats(); // updates secondaries
// set morph and background
String morphName = "";
if (gen.getPC().getCurrentMorph() != null)
{
morphName = gen.getPC().getCurrentMorph().getName();
}
mainPanel.getTextF("Morph").setText(morphName);
if (gen.getPC().hasVar("{background}"))
{
mainPanel.setTextF("Background",gen.getPC().getBackground());
}
if (gen.getPC().hasVar("NatLang"))
{
mainPanel.setTextF("Natural Language",gen.getPC().getVar("NatLang"));
}
if (gen.getPC().hasVar("{factionName}"))
{
mainPanel.setTextF("Faction",gen.getPC().getVar("{factionName}"));
}
int[] stats = new int[16];
int[] bonuses = new int[16];
int cnt = 0;
// fill stats with all the primary and secondary stat values
for (String key : primStats)
{
stats[cnt++] = gen.getPC().getAptitude(key);
}
for (String key : secStats)
{
stats[cnt++] = gen.getPC().getSecStat(key);
}
cnt = 0;
// update base stats for both
for (String key : primStats)
{
statPanel.setTextF("Base "+key,stats[cnt++]);
}
for (String key : secStats)
{
statPanel.setTextF(key,stats[cnt++]);
}
// get bonus amounts
cnt = 0;
for (String key : primStats)
{
bonuses[cnt++] = statPanel.getTextFVal("MorphBonus"+key);
}
for (String key : secStats)
{
bonuses[cnt++] = statPanel.getTextFVal("MorphBonus"+key);
}
// build stat totals
cnt = 0;
for (String key : primStats)
{
statPanel.setTextF("Total "+key,(stats[cnt] + bonuses[cnt]));cnt++;
}
for (String key : secStats)
{
statPanel.setTextF("Total "+key,(stats[cnt] + bonuses[cnt]));cnt++;
}
cnt = 0;
// update a few more display fields
statPanel.setTextF("Stress",gen.getPC().getVarInt("{stress}"));
statPanel.setTextF("MOX",gen.getPC().getSecStat("MOX"));
statPanel.setTextF("Credits",gen.getPC().getVarInt("{credits}"));
statPanel.setTextF("Free CP",gen.getPC().getVarInt("{CP}"));
// update rep
for (Rep r : gen.getPC().getAllRep())
{
statPanel.setTextF(r.getName()+"-rep",+r.getValue());
}
// rebuild skills panel
sideBar.removeAll();
sideBar.addC(new JLabel("Skills "),0,0);
sideBar.addC(new JLabel(" "),1,0);
int x = 0, y = 1;
for(String[] pair : gen.getPC().getSkills())
{
sideBar.addMappedFixedTF(x,y,pair[0], pair[1], 5, false);
if (y <= 32)
{
y+=2;
}
else
{
x += 2;
y = 0;
}
}
mainPanel.revalidate();
}
/**
* @param args
*/
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
LifePathUI ui = new LifePathUI();
ui.init();
}
});
}
// triggers an update if the text field changes
private class TextChangeListener implements DocumentListener
{
@Override
public void changedUpdate(DocumentEvent e) {
update();
}
@Override
public void insertUpdate(DocumentEvent e) {
update();
}
@Override
public void removeUpdate(DocumentEvent e) {
update();
}
}
}
Support Class (GridBagUIPanel)
import java.awt.ComponentOrientation;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.LayoutManager;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.DocumentListener;
public class GridBagUIPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 8550646555771187030L;
private HashMap<String,JComponent> mappedComponents;
private GridBagConstraints cons;
private GridBagLayout layout;
private ArrayList<JComponent> children;
/**
* Creates a new UIPanel with a double buffer and a flow layout.
*/
public GridBagUIPanel() {
super();
init();
}
/**
* Create a new buffered UIPanel with the specified layout manager
* @param layout - the LayoutManager to use
*/
public GridBagUIPanel(LayoutManager layout) {
super(layout);
init();
}
/**
* Creates a new UIPanel with FlowLayout and the specified buffering strategy. If isDoubleBuffered is true, the UIPanel will use a double buffer.
* @param isDoubleBuffered - a boolean, true for double-buffering, which uses additional memory space to achieve fast, flicker-free updates
*/
public GridBagUIPanel(boolean isDoubleBuffered) {
super(isDoubleBuffered);
init();
}
/**
*
* @param layout - the LayoutManager to use
* @param isDoubleBuffered - a boolean, true for double-buffering, which uses additional memory space to achieve fast, flicker-free updates
*/
public GridBagUIPanel(LayoutManager layout, boolean isDoubleBuffered) {
super(layout, isDoubleBuffered);
init();
}
/**
* Groups common parts of object initialization away from the separate constructors
*/
public void init()
{
this.mappedComponents = new HashMap<String,JComponent>();
this.children = new ArrayList<JComponent>();
this.cons = new GridBagConstraints();
this.layout = new GridBagLayout();
this.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
this.setLayout(layout);
cons.ipadx = 5;
cons.ipady = 5;
cons.anchor = GridBagConstraints.NORTHWEST;
cons.fill = GridBagConstraints.NONE;
cons.weighty = 1.0;
cons.weightx = 1.0;
cons.gridheight = 1;
cons.gridwidth = 1;
}
/**
* Inserts a spacer to signify the end of components for this row (effects layout so it doesn't center)
* @param x nonzero int
* @param y nonzero int
*/
public void endRow(int x, int y)
{
cons.weightx = 100.0;
addLabel(x,y,"");
cons.weightx = 1.0;
}
/**
* Inserts a spacer to signify the end of components vertically (effects layout so it doesn't center)
* @param x nonzero int
* @param y nonzero int
*/
public void endVertical(int x, int y)
{
cons.weighty = 100.0;
addLabel(x,y,"");
cons.weighty = 1.0;
}
/**
* Adds a JLabel JTextField pair (each weight 1 ), and adds the textField to the mappedComponents list
* using the labelText for a key
*
* @param x non-negative integer
* @param y non-negative integer
* @param labelText Label to display
* @param updateListen If not null, will add listener passed in to the text field
* @param cols number of cols for the text field
*/
public void addMappedTF(int x, int y, String labelText, int cols, DocumentListener updateListen)
{
addMappedTF(x,y,labelText,labelText,cols,updateListen);
}
/**
* Adds a JLabel JTextField pair (each weight 1 ), and adds the textField to the mappedComponents list
* using the labelText for a key. This version specifies a different String for the label and the map key
*
* @param x non-negative integer
* @param y non-negative integer
* @param labelText Label to display
* @param mapName String to use for the map key for the text field
* @param updateListen If not null, will add listener passed in to the text field
* @param cols number of cols for the text field
*/
public void addMappedTF(int x, int y, String labelText, String mapName, int cols, DocumentListener updateListen)
{
addLabel(x,y,labelText);
this.mappedComponents.put(mapName, addTextF(x+1,y,cols,updateListen));
}
/**
* Adds a JLabel JTextField pair (each weight 1 ), and adds the textField to the mappedComponents list
* using the labelText for a key. The text field is not edittable
*
* @param x non-negative integer
* @param y non-negative integer
* @param labelText Label to display
* @param value The text to display in the field
* @param cols number of cols for the text field
* @param horiztonal Whether the pair is arranged horizontally (vertically if false)
*/
public void addMappedFixedTF(int x, int y, String labelText, String value, int cols, boolean horizontal)
{
int newX,newY;
if (horizontal)
{
newX = x+1;
newY = y;
}
else
{
newX = x;
newY = y+1;
}
addLabel(x,y,labelText);
JTextField temp = addTextF(newX,newY,value,cols,null);
temp.setEditable(false);
this.mappedComponents.put(labelText, temp);
}
/**
* Shorthand command to add label at coordinates with text
* @param x non-negative integer
* @param y non-negative integer
* @param text Display Text for label
*/
public JLabel addLabel(int x, int y, String text)
{
cons.gridx = x;
cons.gridy = y;
JLabel temp = new JLabel(text);
addC(temp);
return temp;
}
/**
* Shorthand command to add Button at coordinates with text
* @param x non-negative integer
* @param y non-negative integer
* @param text Display Text for Button
* @return The component created
*/
public JButton addButton(int x, int y, String text)
{
cons.gridx = x;
cons.gridy = y;
JButton temp = new JButton(text);
addC(temp);
return temp;
}
/**
* Shorthand command to add Button at coordinates with text add adds it to the mapped components list
* with key = text
* @param x non-negative integer
* @param y non-negative integer
* @param text Display Text for Button
* @return The component created
*/
public JButton addMappedButton(int x, int y, String text)
{
JButton temp = addButton(x,y,text);
this.mappedComponents.put(text, temp);
return temp;
}
/**
* Shorthand command to add Text Field at coordinates with text
* @param x non-negative integer
* @param y non-negative integer
* @param value the default value for the text field
* @param cols Number of Columns
* @param updateListen If not null, will add listener passed in to the text field
* @return The component created
*/
public JTextField addTextF(int x, int y, String value, int cols, DocumentListener updateListen)
{
cons.gridx = x;
cons.gridy = y;
JTextField temp = new JTextField(value, cols);
// this prevents the common issue of the text fields turning into slits
temp.setMinimumSize(temp.getPreferredSize());
if (updateListen != null)
{
temp.getDocument().addDocumentListener(updateListen);
}
addC(temp);
return temp;
}
/**
* Shorthand command to add Text Field at coordinates with text. No value set in this version
* @param x non-negative integer
* @param y non-negative integer
* @param cols Number of Columns
* @param updateListen If not null, will add listener passed in to the text field
* @return The component created
*/
public JTextField addTextF(int x, int y, int cols, DocumentListener updateListen)
{
return this.addTextF(x, y, "", cols, updateListen);
}
/**
* Shorthand command to add Text Area at coordinates with text
* @param x non-negative integer
* @param y non-negative integer
* @param rows Number of Rows
* @param cols Number of Columns
* @return The component created
*/
public JTextArea addTextArea(int x, int y, int rows, int cols)
{
cons.gridx = x;
cons.gridy = y;
JTextArea temp = new JTextArea(rows,cols);
addC(temp);
return temp;
}
/**
* Shorthand to add new components to the UI tab
* @param comp Component to add to UI
*/
public void addC(JComponent comp) {
this.add(comp,cons);
this.children.add(comp);
}
/**
* Shorthand to add new components to the UI tab at given coords
* @param comp Component to add to UI
* @param x non-negative integer
* @param y non-negative integer
*
*/
public void addC(JComponent comp, int x, int y) {
cons.gridx = x;
cons.gridy = y;
this.addC(comp);
this.children.add(comp);
}
/**
* Shorthand to add new components to the UI tab at given coords with height and width
* @param comp Component to add to UI
* @param x non-negative integer
* @param y non-negative integer
* @param gridHeight valid number or GridBagConstraints constant
* @param gridWidth valid number or GridBagConstraints constant
*/
public void addC(JComponent comp, int x, int y, int gridHeight, int gridWidth) {
int oldHeight = cons.gridheight;
int oldWidth = cons.gridwidth;
cons.gridheight = gridHeight;
cons.gridwidth = gridWidth;
this.addC(comp,x,y);
cons.gridheight = oldHeight;
cons.gridwidth = oldWidth;
}
/**
* Shorthand to add new components to the UI tab at given coords with height, width, and fill value
* @param comp Component to add to UI
* @param x non-negative integer
* @param y non-negative integer
* @param gridHeight valid number or GridBagConstraints constant
* @param gridWidth valid number or GridBagConstraints constant
* @param fill valid number or GridBagConstraints constant
*/
public void addC(JComponent comp, int x, int y, int gridHeight, int gridWidth, int fill) {
int oldFill = cons.fill;
cons.fill = fill;
this.addC(comp,x,y,gridHeight,gridWidth);
cons.fill = oldFill;
}
/**
* Returns whether either this Panel or any of it's children have the component in question
* @param name Name of the mapped component to look for
* @return true or false as appropriate
*/
public boolean hasComponent(String name)
{
if (this.mappedComponents.containsKey(name))
{
return true;
}
else
{
for (JComponent j : this.children)
{
if (j.getClass().getSimpleName().equalsIgnoreCase("GridBagUIPanel"))
{
GridBagUIPanel temp = (GridBagUIPanel)j;
return temp.hasComponent(name);
}
}
return false;
}
}
/**
* Returns eitehr the mapped component of the given name, or null
* @param name Name of the mapped component to look for
* @return the JComponent if it exists, null otherwise
*/
public JComponent getComponent(String name)
{
if (this.mappedComponents.containsKey(name))
{
return this.mappedComponents.get(name);
}
else
{
for (JComponent j : this.children)
{
if (j.getClass().getSimpleName().equalsIgnoreCase("GridBagUIPanel"))
{
GridBagUIPanel temp = (GridBagUIPanel)j;
if ( temp.hasComponent(name) )
{
return temp.getComponent(name);
}
}
}
return null;
}
}
/**
* Helper method, searches mappedComponents for a JTextField with name, and returns it properly cast to JTextField
*
* @param name Valid name
* @return The JTextField of matching name, or null if no matching JTextField exists
*/
public JTextField getTextF(String name)
{
// does it exist?
if (!this.hasComponent(name) )
{
return null;
}
else
{
JComponent temp = this.getComponent(name);
// is it the right type?
if ( !temp.getClass().getSimpleName().equalsIgnoreCase("jtextfield") )
{
return null;
}
else
{
return (JTextField)temp;
}
}
}
/**
* Returns the integer value stored in a mapped textfield, if applicable
* @param name Textfield to search for
* @return Either the appropriate int value or 0 if no valid integer bearing field could be found
*/
public int getTextFVal(String name)
{
JTextField temp = getTextF(name);
// does it exist and is it a number
if (temp == null || !Utils.isInteger(temp.getText()))
{
return 0;
}
else
{
return Integer.parseInt(temp.getText());
}
}
/**
* Attempts to retrieve a mapped text field with the matching name and change its text value to val
*
* This function will fail silently if the field doesn't exist
*
* @param name Name of the text field to look for
* @param val Value to setText to
*/
public void setTextF(String name, String val)
{
JTextField temp = getTextF(name);
// does it exist and is it a number
if (temp == null)
{
return;
}
else
{
temp.setText(val);
}
}
/**
* Attempts to retrieve a mapped text field with the matching name and change its text value to val
* This version takes an int value for convenience
* @param name Name of the text field to look for
* @param val int Value to setText to, will be converted to string
*/
public void setTextF(String name, int val)
{
this.setTextF(name, ""+val);
}
/**
* Attempts to retrieve a mapped button with the matching name and change its display text to val
*
* This function will fail silently if the button doesn't exist
*
* @param name Name of the button to look for
* @param val Value to setText to
*/
public void setButtonText(String name, String val)
{
JComponent temp = mappedComponents.get(name);
// does it exist and is it a number
if (temp == null || !temp.getClass().getSimpleName().equalsIgnoreCase("JButton"))
{
return;
}
else
{
((JButton)temp).setText(val);
}
}
/**
* Adds a component to this Panel's mappedComponet list
* @param name Name/key for the component
* @param comp Component to add
* @return the component added
*/
public JComponent put(String name, JComponent comp)
{
return this.mappedComponents.put(name, comp);
}
}