⬆️ ⬇️

Swing Ten useful simple things

image



I want to share my recent experience in developing Swing applets and tell us about the pitfalls, techniques found and used in the process.





')

If you have already dealt with the Swing library, you can skip straight to the second chapter .



Three steps for a quick start


  1. To begin with, we carefully study the documentation on various Layout managers , without such basic knowledge it will be difficult to achieve the desired mapping.
  2. Then we carefully study the basic visual components of java look and feel or windows look and feel . How and what to use in the work can be peeped in the textbook .
  3. We put Eclipse and create a java project, create an applet and ... "next file" (c).




For a quick start and building simple interfaces this knowledge will be quite enough.



Now we come to the most interesting.



Ten useful simple things




1. In order to set look and feel as in the system (for example, the corresponding windows theme):

javax.swing.UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 




2. Change the default font:

 FontUIResource f = new FontUIResource(new Font("Verdana", 0, 12); Enumeration<Object> keys = UIManager.getDefaults().keys(); while (keys.hasMoreElements()) { Object key = keys.nextElement(); Object value = UIManager.get(key); if (value instanceof FontUIResource) { FontUIResource orig = (FontUIResource) value; Font font = new Font(f.getFontName(), orig.getStyle(), f.getSize()); UIManager.put(key, new FontUIResource(font)); } } 




3. A simple version of the modal dialogue:

 int reply = JOptionPane.showConfirmDialog(null, "Is Hello world?", "Title", JOptionPane.YES_NO_OPTION); if (reply == JOptionPane.YES_OPTION){ //do something } 




If a more advanced dialog is required, there is an option with the ability to add a JComponent:

 JComponent[] inputs //   int reply = JOptionPane.showConfirmDialog(null, inputs, "", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if (reply == JOptionPane.OK_OPTION) { ... 




4. For quick localization of dialogs:

 UIManager.put("OptionPane.yesButtonText", ""); UIManager.put("OptionPane.noButtonText", ""); UIManager.put("OptionPane.cancelButtonText", ""); UIManager.put("OptionPane.okButtonText", ""); 




5. To filter the elements in the columns of the table JTable there is a very good open library Swing Bits with which you can make a filter “like in Excel”.



6. In order to give the user the opportunity to choose dates there is an excellent component jcalendar

image




7. To play sound packaged in a jar archive:

 myapplet.getAudioClip(MyPlayerClass.class.getResource("     ").play(); 




Now some nontrivial chips:



8. In order to embed a button in the table row (with the correct click render):

 public class MyTable extends JTable { public class MyButtonRenderer extends JButton implements TableCellRenderer, TableCellEditor{ private int selectedRow; private int selectedColumn; private final List<MyButtonListener> listener = new ArrayList<MyButtonListener>(); public MyButtonRenderer() { super(); addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { for (MyButtonListener l : listener) { l.tableButtonClicked(selectedRow, selectedColumn); //     } } }); } public Component getTableCellRendererComponent(JTable a, Object b, boolean c, boolean d, int e, int f) { setFocusable(false); return this; } public boolean stopCellEditing() {return true;} public Object getCellEditorValue() { return null;} public boolean isCellEditable(EventObject anEvent) { return true;} public boolean shouldSelectCell(EventObject anEvent) {return true;} public Component getTableCellEditorComponent(JTable a, Object b, boolean c, int d, int e) { selectedRow = row; selectedColumn = column; setFocusable(false); return this; } public void addTableButtonListener(ITableButtonListener l) {listener.add(l);} public void removeTableButtonListener(ITableButtonListener l) {listener.remove(l);} } public MyTable(){ MyButtonRenderer button = new MyButtonRenderer(); button.addTableButtonListener(new MyButtonListenerImpl()); //  ""   .   2       this.getColumnModel().getColumn(0).setCellRenderer(new MyButtonRenderer()); this.getColumnModel().getColumn(0).setCellEditor(button); } public TableCellRenderer getCellRenderer(int row, int column) { TableCellRenderer result = super.getCellRenderer(row, column); if (result instanceof MyButtonRenderer) return result; return <  (  )>; } } 




9. In order to change the swing components from another thread, you need to use an intermediary:

 class MyWorker extends SwingWorker<Void, MyObject>{ private IProxyObject action; protected SwingWorkerReal(MyObject data){ this.action = data; } protected Void doInBackground() throws Exception { if (action != null) publish(action); return null; } protected void process(List<MyObject> chunks) { //        process! } } ... new MyWorker(data).execute(); //  ... } 




10. And finally, the SwingWorker counter. Only ten SwingWorker threads can be executed at a time, this is due to the fact that the thread pool of the SwingWorker handler has a maximum size of ten, so try to keep the tasks small. A real-life example: in IE, you can open 10 pages with applets, and 11 will already have to wait (“hang”) until the ThreadPoolExecutor's place is empty for processing data received from the server in another stream.



Afterword


In general, the development under the swing paraded with an abundance of documentation and examples for all occasions. If you're interested, I can tell you more about JTable chips in more detail.

Source: https://habr.com/ru/post/137347/



All Articles