1 65 66 package org.jfree.ui; 67 68 import java.awt.BorderLayout ; 69 import java.awt.Color ; 70 import java.awt.Container ; 71 import java.awt.Dialog ; 72 import java.awt.Dimension ; 73 import java.awt.Font ; 74 import java.awt.Toolkit ; 75 import java.awt.Window ; 76 77 import javax.swing.JButton ; 78 import javax.swing.JLabel ; 79 import javax.swing.JPanel ; 80 import javax.swing.JScrollPane ; 81 import javax.swing.JTable ; 82 import javax.swing.table.TableColumn ; 83 import javax.swing.table.TableModel ; 84 85 import org.jfree.util.Log; 86 import org.jfree.util.LogContext; 87 88 93 public abstract class RefineryUtilities { 94 95 96 protected static final LogContext logger = Log.createContext(RefineryUtilities.class); 97 98 103 public static void centerFrameOnScreen(final Window frame) { 104 positionFrameOnScreen(frame, 0.5, 0.5); 105 } 106 107 117 public static void positionFrameOnScreen(final Window frame, 118 final double horizontalPercent, 119 final double verticalPercent) { 120 121 final Dimension s = Toolkit.getDefaultToolkit().getScreenSize(); 122 final Dimension f = frame.getSize(); 123 final int w = Math.max(s.width - f.width, 0); 124 final int h = Math.max(s.height - f.height, 0); 125 final int x = (int) (horizontalPercent * w); 126 final int y = (int) (verticalPercent * h); 127 frame.setBounds(x, y, f.width, f.height); 128 129 } 130 131 137 public static void positionFrameRandomly(final Window frame) { 138 positionFrameOnScreen(frame, Math.random(), Math.random()); 139 } 140 141 146 public static void centerDialogInParent(final Dialog dialog) { 147 positionDialogRelativeToParent(dialog, 0.5, 0.5); 148 } 149 150 157 public static void positionDialogRelativeToParent(final Dialog dialog, 158 final double horizontalPercent, 159 final double verticalPercent) { 160 final Dimension d = dialog.getSize(); 161 final Container parent = dialog.getParent(); 162 final Dimension p = parent.getSize(); 163 164 final int baseX = parent.getX() - d.width; 165 final int baseY = parent.getY() - d.height; 166 final int w = d.width + p.width; 167 final int h = d.height + p.height; 168 int x = baseX + (int) (horizontalPercent * w); 169 int y = baseY + (int) (verticalPercent * h); 170 171 final Dimension s = Toolkit.getDefaultToolkit().getScreenSize(); 173 x = Math.min(x, (s.width - d.width)); 174 x = Math.max(x, 0); 175 y = Math.min(y, (s.height - d.height)); 176 y = Math.max(y, 0); 177 178 dialog.setBounds(x, y, d.width, d.height); 179 180 } 181 182 189 public static JPanel createTablePanel(final TableModel model) { 190 191 final JPanel panel = new JPanel (new BorderLayout ()); 192 final JTable table = new JTable (model); 193 for (int columnIndex = 0; columnIndex < model.getColumnCount(); columnIndex++) { 194 final TableColumn column = table.getColumnModel().getColumn(columnIndex); 195 final Class c = model.getColumnClass(columnIndex); 196 if (c.equals(Number .class)) { 197 column.setCellRenderer(new NumberCellRenderer()); 198 } 199 } 200 panel.add(new JScrollPane (table)); 201 return panel; 202 203 } 204 205 213 public static JLabel createJLabel(final String text, final Font font) { 214 215 final JLabel result = new JLabel (text); 216 result.setFont(font); 217 return result; 218 219 } 220 221 230 public static JLabel createJLabel(final String text, final Font font, final Color color) { 231 232 final JLabel result = new JLabel (text); 233 result.setFont(font); 234 result.setForeground(color); 235 return result; 236 237 } 238 239 247 public static JButton createJButton(final String label, final Font font) { 248 249 final JButton result = new JButton (label); 250 result.setFont(font); 251 return result; 252 253 } 254 255 } 256 257 258 | Popular Tags |