//mainFrame.java // generates a frame to cover the screen for the text. //Title: General Classes //Version: 0.1 //Author: John Krantz //Company: Hanover College //Description: Some general classes to control the look and feel //of the book package. package util; import java.awt.*; import javax.swing.*; /** * constructs a table in a frame to do a i/o dialog labels * used to fill the table elements. * @author John Krantz * @version 0.1 */ public class TableFrame extends MainFrame { static final long serialVersionUID = 0; public final static Dimension SCREENSIZE = Toolkit.getDefaultToolkit().getScreenSize(); // container to hold panel private Container c = this.getContentPane(); //Panel object to hold parameters to be set private JPanel titlePanel = new JPanel(new BorderLayout()); private JPanel tablePanel = new JPanel(new BorderLayout()); private JPanel elementPanel = new JPanel(); // title object private JLabel titleLabel = new JLabel("Title"); // table elements private JPanel topPanel = new JPanel(new BorderLayout()); private JLabel topLabel = new JLabel("Top Label"); private JLabel sideLabel = new JLabel("Side Label"); private JLabel elem [][]; // buffer string private final static String BUFFER = " "; // height and width of the panel int h = 0; int w = 0; public TableFrame(){}; // add a title to the same frame as above. public TableFrame(String title,String colTitle, String rowTitle, String [] colTitles, String [] rowTitles, String [][] items){ // set the basic format of the table c.setBackground(Color.black); this.setForeground(Color.white); this.setTitle(titleLabel.getText()); c.add(getTablePanel(title, colTitle, rowTitle, colTitles, rowTitles, items),BorderLayout.CENTER); this.setSize(new Dimension(w*20/10,h*20/10)); } // set methods /** * Change the text of one item in the table * @param elm the new text * @param col the column position of the item * @param row the row position of the item */ public void setTableElement(String elm, int col, int row){ if (elem.length > row+1 & elem[0].length > col+1){ elem[row+1][col+1].setText(elm); } } public void setRowTitle(String title){ sideLabel.setText(title); } public void setColumnTitle(String title){ topLabel.setText(title); } // Get Methods /** * Get the panel that holds the table * @param title The title of the table * @param colTitle The column Title * @param rowTitle The row title * @param colTitles the column headers * @param rowTitles the row headers * @param items the items in the table * @return JPanel the table */ public JPanel getTablePanel(String title,String colTitle, String rowTitle, String [] colTitles, String [] rowTitles, String [][] items){ JPanel panel = new JPanel(new BorderLayout()); // set up the title label Font titleFont = new Font("SansSerif",Font.BOLD,18); Font labelFont = new Font("SansSerif",Font.BOLD,12); Font elemFont = new Font("SansSerif",Font.PLAIN,12); titleLabel.setFont(titleFont); titleLabel.setText(title); // set up the top and side headers topLabel.setText(colTitle+BUFFER); sideLabel.setText(rowTitle); // set up the table elements elem = new JLabel[items.length+1][items[0].length+1]; // do the top elements labels // row labels elem[0][0] = new JLabel(" "); for (int i = 0; i < colTitles.length; i ++){ elem[0][i+1] = new JLabel(colTitles[i]); elem[0][i+1].setFont(labelFont); } // column labels for (int i = 0; i < rowTitles.length; i ++){ elem[i+1][0] = new JLabel(rowTitles[i]); elem[i+1][0].setFont(labelFont); } // now fill in the elements for (int i = 0; i < items.length; i ++){ for (int j=0; j < items[0].length; j ++){ elem[i+1][j+1] = new JLabel(items[i][j]); elem[i+1][j+1].setFont(elemFont); } } // parameters to determine the size of the window h = 0; w = 0; FontMetrics fm = sideLabel.getFontMetrics(labelFont); h = fm.getHeight(); w = fm.stringWidth(sideLabel.getText()); // set primary layout panel.add(titlePanel,BorderLayout.NORTH); titlePanel.add(titleLabel,BorderLayout.CENTER); topPanel.add(topLabel,BorderLayout.EAST); tablePanel.add(topPanel,BorderLayout.NORTH); tablePanel.add(sideLabel,BorderLayout.WEST); // add the elements to the element panel elementPanel.setLayout(new GridLayout(elem.length, elem[0].length,5,5)); for (int i = 0; i < elem.length; i ++){ h += fm.getHeight()+5; for (int j = 0; j < elem[0].length; j ++){ elementPanel.add(elem[i][j]); if (i == 1){ w += fm.stringWidth(elem[i][j].getText())+10; } } } tablePanel.add(elementPanel,BorderLayout.CENTER); h += fm.getHeight(); panel.add(tablePanel,BorderLayout.CENTER); return panel; } public int getFrameHeight() { return getHeight();} public int getFrameWidth() { return getWidth();} public int getPanelHeight() { return h; } public int getPanelWidth() { return w; } }