import javax.swing.*; import javax.swing.event.*; import javax.swing.table.TableColumn; import javax.swing.plaf.basic.BasicArrowButton; import java.awt.event.*; import java.awt.*; import java.text.*; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeEvent; public class PatioConfigUI extends JInternalFrame implements ActionListener, ChangeListener, PropertyChangeListener { public static boolean debugging = true; // controls showing of debug messages static final int xOffset = 30, yOffset = 30; static final int xSize = 800, ySize = 600; private static int dragBaseX = 0; private static int dragBaseY = 0; static int position = 1; // tiling position periodic 1-8 private PatioConfig config; // control panes public JPanel controls; private JPanel elementsPane; private JPanel currentElmntPane; private JPanel projectPane; private JPanel addRemovePane; private JPanel upDownPane; private JPanel totalPane; private JPanel materialsPane; private JPanel shapesPane; // pane titles private String elementsPaneTitle = "Elements"; private String currentElmntPaneTitle = "Current Element"; private String dimensionsPaneTitle = "Dimensions"; private String materialsPaneTitle = "Materials"; private String projectPaneTitle = "Project"; private String totalPaneTitle = "Total"; // other strings private String areaControlLbl = "area"; private String positionControlLbl = "position"; private String materialSelectionLbl = "material"; private String shapeSelectionLbl = "material"; private String totalLabel = "Total:"; // project element table private final int tablePreferredColumnWidth = 90; private final int tablePreferredShapeColumnWidth = 120; private final int tablePreferredInclColumnWidth =40; private final int tableMaxColumnWidth = 80; private JTable tableModel; // add/remove element buttons private JButton addElementButton; private JButton removeElementButton; private String addButtonStr = "add"; private String removeButtonStr = "remove"; private String upButtonStr = "move up"; private String downButtonStr = "move down"; private JButton upElementButton; private JButton downElementButton; private JTable elementsTable; private JScrollPane tableScroller; // project pane widgets private JFormattedTextField totalCostField; private JSpinner scaleSelector; private SpinnerModel scaleModel = new SpinnerNumberModel(1.0, 0.25, 8.0, 0.5); // input field formats private NumberFormat scaleFormat; private NumberFormat dimensionFormat; private NumberFormat totalCostFormat; String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"}; Object[][] data = { {"Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false)}, {"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, {"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)}, {"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)}, {"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)} }; /** * PatioConfigUI constructor */ public PatioConfigUI(PatioConfig parent, String title) { super(title, true, true, true, true); config = parent; position %= 8; position++; //...Create the GUI and put it in the window... // use the default BorderLayout for the whole window setUpFormats(); controls = setupControlsPane(); add(controls, BorderLayout.LINE_END); // right // finish by setting size and location setSize( xSize, ySize); setLocation(xOffset*position, yOffset*position); } /** * create and setup input formats * these objects parse use input */ private void setUpFormats() { scaleFormat = NumberFormat.getNumberInstance(); dimensionFormat = NumberFormat.getNumberInstance(); totalCostFormat = NumberFormat.getCurrencyInstance(); } /** * setup the controls pane */ private JPanel setupControlsPane() { JPanel currentPane = new JPanel(); currentPane.setLayout(new BoxLayout(currentPane, BoxLayout.Y_AXIS)); elementsPane = setupElementsPane(); currentPane.add(elementsPane); return currentPane; } /** * setup the elements pane */ private JPanel setupElementsPane() { JPanel currentPane = new JPanel(); currentPane.setBorder(BorderFactory.createTitledBorder(elementsPaneTitle)); currentPane.setLayout(new GridBagLayout()); // the gridbag constraints instance GridBagConstraints c = new GridBagConstraints(); c.anchor = GridBagConstraints.FIRST_LINE_START; addElementButton = new JButton(addButtonStr); removeElementButton = new JButton(removeButtonStr); c.gridx=0; c.gridy=0; currentPane.add(addElementButton, c); c.gridx=1; c.gridy=0; currentPane.add(removeElementButton, c); tableScroller = setupElementsTable(); c.gridx=0; c.gridy=1; c.weightx=1.0; c.weighty=1.0; c.fill=GridBagConstraints.BOTH; c.gridwidth=GridBagConstraints.REMAINDER; currentPane.add(tableScroller, c); c.weightx=0.0; c.weighty=0.0; c.gridwidth=1; c.fill=GridBagConstraints.NONE; // up/down buttons upElementButton = new JButton(downButtonStr); downElementButton = new JButton(upButtonStr); c.gridx=0; c.gridy=2; currentPane.add(upElementButton, c); c.gridx=0; c.gridy=3; currentPane.add(downElementButton, c); // using a formatted text field rather than a label makes this text // good for copying with the mouse totalCostField = new JFormattedTextField(totalCostFormat); // set to a temporary value totalCostField.setValue(1234560.993); // TODO make this automatically calculated! totalCostField.setColumns(10); totalCostField.setEditable(false); // make it uneditable c.gridx=2; c.gridy=2; currentPane.add(new JLabel(totalLabel), c); c.gridx=3; c.gridy=2; c.weighty=1.0; c.fill=GridBagConstraints.HORIZONTAL; c.gridwidth=GridBagConstraints.REMAINDER; currentPane.add(totalCostField, c); return currentPane; } /** * setup the elements table */ private JScrollPane setupElementsTable() { TableColumn column; elementsTable = new JTable(data, columnNames); elementsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //elementsTable.setPreferredScrollableViewportSize(new Dimension(240, 500)); //elementsTable.setFillsViewportHeight(true); only in Java 1.6 for (int i = 0; i < elementsTable.getColumnCount(); i++) { column = elementsTable.getColumnModel().getColumn(i); if (i == 0) { // set a different width for the first column column.setPreferredWidth(tablePreferredShapeColumnWidth); } else if (i == 4){ // set a different width for the fifth column column.setPreferredWidth(tablePreferredInclColumnWidth); } else { column.setPreferredWidth(tablePreferredColumnWidth); } } JScrollPane currentPane = new JScrollPane(elementsTable); //currentPane.setPreferredSize(new Dimension(320, 100)); return currentPane; } public void actionPerformed(ActionEvent e) { } /** Called when a field's "value" property changes. */ public void propertyChange(PropertyChangeEvent e) { } public void stateChanged(ChangeEvent e) { } /** * print a debug message if debugging is on (true) * uses System.out.println() */ public static void printDebug(String s) { if (debugging) { System.out.println(s); } } }