TVpublic class TV extends JPanel TV - Main GUI View of one Exam. Has a JToolbar and a JPanel managed
by a CardLayout to show one QView at a time. |
Fields Summary |
---|
TD | theTDThe data model | JFrame | frmThe Frame above us | JToolBar | toolBarA toolbar for editing icons | CardLayout | myCardLayoutA cardLayout for the many questions | JTextField | numTFA number choice entry | JSlider | numSliderA number chooser. | JPanel | questionsPanelA panel to hold all the qview's, managed by myCardLayout | FileDialog | fcThe obligatory file chooser | ExamInfo | xinfoThe data |
Constructors Summary |
---|
public TV(JFrame f, TD mod)Construct the TV object - that is, the main GUI for the program
super();
frm = f;
theTD = mod;
JButton b;
// Build the GUI
toolBar = new JToolBar();
toolBar.setFloatable(false);
toolBar.getAccessibleContext().setAccessibleName("File Toolbar");
toolBar.addSeparator();
b = addTool(toolBar, "Cut");
b = addTool(toolBar, "Copy");
b = addTool(toolBar, "Paste");
toolBar.addSeparator();
toolBar.putClientProperty( "JToolBar.isRollover", Boolean.FALSE );
// The Slider
numSlider= new JSlider(JSlider.HORIZONTAL, 1, 40, 1);
numSlider.setPaintTicks(true);
numSlider.setPaintLabels(false);
numSlider.setMajorTickSpacing(10);
numSlider.setMinorTickSpacing( 2);
numSlider.setExtent(1);
numSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent ce) {
// System.out.println("CHANGE: " + ce);
setQNumber(((JSlider)(ce.getSource())).getValue());
}
});
numSlider.setToolTipText("Slide to select question by number");
toolBar.add(numSlider);
// The Question# textfield
toolBar.add(numTF = new JTextField("01"));
numTF.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = ((JTextField)e.getSource()).getText();
int n = Integer.parseInt(num.trim());
setQNumber(n);
}
});
numTF.setToolTipText("Type number to select question by number");
// The First Button
b = addTool(toolBar, "First");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setQNumber(1);
}
});
// The Previous Button
b = addTool(toolBar, "Previous");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (getQNumber() <= 1)
return;
setQNumber(getQNumber() - 1);
}
});
// The Next Button
b = addTool(toolBar, "Next");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (getQNumber() >= getNumQuestions())
return;
setQNumber(getQNumber() + 1);
}
});
// The "Last" Button
b = addTool(toolBar, "Last");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setQNumber(getNumQuestions());
}
});
add(BorderLayout.NORTH, toolBar);
// Rest is a panel to hold the questions, one at a time.
questionsPanel = new JPanel();
questionsPanel.setLayout(myCardLayout = new CardLayout());
add(BorderLayout.SOUTH, questionsPanel);
fc = new FileDialog(frm);
fc.setFilenameFilter(new FilenameFilter() {
public boolean accept(File f, String fname) {
System.out.println("accept("+fname+")");
return fname.endsWith(".xam");
}
});
TV.centre(fc);
|
Methods Summary |
---|
public static javax.swing.JButton | addTool(javax.swing.JToolBar toolBar, java.lang.String name)Simple convenience routine for adding a button/icon to a Toolbar
JButton b;
b = new JButton(new ImageIcon("images/" + name + ".gif",name));
toolBar.add(b);
b.setToolTipText(name);
b.setMargin(new Insets(0,0,0,0));
b.getAccessibleContext().setAccessibleName(name);
return b;
| public static void | centre(java.awt.Window w)Centre a Window on the screen
Dimension us = w.getSize(),
them = Toolkit.getDefaultToolkit().getScreenSize();
int newX = (them.width - us.width) / 2;
int newY = (them.height- us.height)/ 2;
w.setLocation(newX, newY);
| protected int | getNumQuestions()Return the number of questions
return theTD.curX.getNumQuestions();
| public java.awt.Dimension | getPreferredSize()Pure guesswork
return new Dimension(500, 400);
| protected int | getQNumber()Return the currently selected number
return numSlider.getValue();
| public void | installQVs()Connect the vector of QV's into the main View
Exam thisExam = theTD.curX;
Vector qv = thisExam.getqvs();
for (int i=0; i<qv.size(); i++) {
// Use question number (1-origin) as string name.
// System.out.println("Add question " +i +" to " + questionsPanel);
questionsPanel.add(Integer.toString(i+1),(Component)qv.elementAt(i));
}
myCardLayout.first(questionsPanel);
| public void | setNumQuestions(int i)Set the maximum number of questions
numSlider.setMaximum(i);
| protected void | setQNumber(int nn)Change the view to display a given question number (1-origin)
String userShowNumber = Integer.toString(nn);
System.out.println("setQNumber(" + userShowNumber + ");");
// notify the model
// TODO -- if necessary!
// update the view
numTF.setText(userShowNumber);
numSlider.setValue(nn);
// show the right question
myCardLayout.show(questionsPanel, userShowNumber);
| public void | setTitle(java.lang.String s)
frm.setTitle(s);
| protected void | showStats(TStat t)
JOptionPane.showMessageDialog(frm, t.toString());
|
|