public static void main(String[] args) { JFrame frame = new JFrame("redgreen"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new FlowLayout()); SButton red = new SButton("red"); SButton green = new SButton("green"); Stream<String> sRed = red.sClicked.map(u -> "red"); Stream<String> sGreen = green.sClicked.map(u -> "green"); Stream<String> sColor = sRed.orElse(sGreen); Cell<String> color = sColor.hold(""); SLabel lbl = new SLabel(color); frame.add(red); frame.add(green); frame.add(lbl); frame.setSize(400, 160); frame.setVisible(true); }
public static void main(String[] args) { JFrame frame = new JFrame("form"); Listener l = Transaction.run( () -> { frame.setLayout(new FlowLayout()); SButton newClient = new SButton("Open new client"); frame.add(newClient); BackEnd be = new BackEnd(); Value<String> vName = be.allocate("name", "Joe Bloggs"); Value<Date> vBirthDate = be.allocate("birthDate", new Date(1980, 5, 1)); Value<Integer> vYear = vBirthDate.lens(d -> d.year, (dt, y) -> dt.setYear(y)); Value<Integer> vMonth = vBirthDate.lens(d -> d.month, (dt, y) -> dt.setMonth(y)); Value<Integer> vDay = vBirthDate.lens(d -> d.day, (dt, y) -> dt.setDay(y)); Bijection<Integer, String> toString = new Bijection<>( i -> Integer.toString(i), s -> { try { return Integer.parseInt(s); } catch (NumberFormatException e) { return 0; } }); Value<String> vYearStr = vYear.map(toString); Value<String> vMonthStr = vMonth.map(toString); Value<String> vDayStr = vDay.map(toString); frame.setSize(500, 250); frame.setVisible(true); return newClient.sClicked.listen( u -> { SwingUtilities.invokeLater( () -> { JFrame client = new JFrame("form client"); GridBagLayout gridbag = new GridBagLayout(); client.setLayout(gridbag); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL; c.weighty = 0.0; c.weightx = 1.0; c.gridwidth = 1; c.gridheight = 1; c.gridx = 0; c.gridy = 0; Transaction.runVoid( () -> { c.gridx = 0; client.add(new JLabel("Name"), c); c.gridx = 1; c.gridwidth = 3; client.add(new VTextField(vName, 15), c); c.gridwidth = 1; c.gridy = 1; c.gridx = 0; client.add(new JLabel("Birth date"), c); c.gridx = 1; client.add(new VTextField(vYearStr, 4), c); c.gridx = 2; client.add(new VTextField(vMonthStr, 2), c); c.gridx = 3; client.add(new VTextField(vDayStr, 2), c); client.setSize(300, 100); client.setVisible(true); }); client.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { client.dispose(); } }); }); }); }); frame.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { l.unlisten(); System.exit(0); } }); while (true) { Runtime.getRuntime().gc(); try { Thread.sleep(1000); } catch (InterruptedException e) { } } }