java/ch/wlkl/javaExamples/ExGui.java

/*
 * ExGui.java
 *
 * Created on 21. Dezember 2003, 14:55
 */

/**
 *
 * @author  walter
 */
package ch.wlkl.javaExamples;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

/**
 * 
 * @author walter
 */
public class ExGui extends JPanel {

    void doPack() {
        /*
         * for (Component x = this; x != null; x = x.getParent())
         * System.out.println("parent = " + x); System.out.println("root2 " +
         * getRootPane());
         */
        Object pp = getRootPane();
        while (pp != null) {
            if (JFrame.class.isInstance(pp)) {
                ((JFrame) pp).pack();
                System.out.println("doPack packed " + pp);
                return;
            }
            if (!JComponent.class.isInstance(pp))
                break;
            pp = ((JComponent) pp).getParent();
        }
        System.out.println("doPack could not pack root " + getRootPane()
                + "\n parent " + getRootPane().getParent());
        // ((JFrame) getRootPane().getParent()).pack();
    }

    private JTextField le, ri, res;

    private int cnt;

    /** Creates a new instance of GuiAdd */
    void adder() {
        add(le = new JTextField(10));

        add(new JLabel("+"));
        add(ri = new JTextField(10));
        ((JButton) add(new JButton("=")))
                .addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        res.setText(le.getText() + ri.getText());
                    }
                });
        add(res = new JTextField(20));
        res.setEditable(false);
        ((JButton) add(new JButton("ornaments class nach jar")))
                .addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        adderOrnaments();
                    }
                });
        adderRadio();
    }

    void adderOrnaments() {
        cnt++;
        setToolTipText("toolTip " + cnt + " f�r GuiAdd < JPanl");
        le.setToolTipText("toolTip f�r le " + cnt);
        // -16 % 256 is negativ which produces an exception....
        le.setBackground(new Color(16 * cnt % 256, 0, (511 - 16 * cnt) % 256));
        le.setForeground(Color.green);
        Font fo = le.getFont();
        fo = new Font(fo.getName(), Font.ITALIC, 12 + 2 * cnt);
        // System.out.println("font old " + le.getFont() + " new " + fo);
        le.setFont(fo);
        ri.setBorder(BorderFactory.createLineBorder(Color.yellow, cnt));
        // ((JFrame) getParent()).
        int ix = 0;
        doPack();
    }

    void adderRadio() {
        final JRadioButton radBut[] = { new JRadioButton("eins"),
                new JRadioButton("zwei"), new JRadioButton("drei") };
        Box box = Box.createVerticalBox();
        add(box);
        final JTextField res = new JTextField(10);
        ButtonGroup bg = new ButtonGroup();
        // setLayout(new GridLayout(radBut.length + 2,1)); // allowed for most
        // conatiners but not for box!
        box.add(new JLabel("Radio Select"));
        ItemListener lst = new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                String t = "";
                for (int i = 0; i < radBut.length; i++)
                    if (radBut[i].isSelected())
                        t += ", " + radBut[i].getText();
                res.setText("sels: " + t);
            }
        };
        for (int i = 0; i < radBut.length; i++) {
            radBut[i].addItemListener(lst);
            box.add(radBut[i]);
            bg.add(radBut[i]); // selection is unique within group
        }
        box.add(res);
    }

    void fileView() {
        setLayout(new BorderLayout());
        Box bx = Box.createHorizontalBox();
        JButton but = new JButton(new ImageIcon(ExGui.class
                .getResource("ExGif.gif")));
        bx.add(but);
        add(bx, BorderLayout.NORTH);
        final JDesktopPane desk = new JDesktopPane();
        desk.setSize(400, 500);
        add(desk);
        but.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                JFileChooser dia = new JFileChooser("d:\\java");
                int sel = dia.showOpenDialog(ExGui.this);
                if (sel == JFileChooser.APPROVE_OPTION)
                    fileViewOpen(desk, dia.getSelectedFile());
            }
        });
    }

    void fileViewOpen(JDesktopPane desk, File fi) {
        try {
            JInternalFrame fr = new JInternalFrame(fi.toString(), true, true,
                    true, true);
            JTextArea txt = new JTextArea(20, 50);
            txt.setLineWrap(true);
            txt.read(new FileReader(fi), null);
            fr.getContentPane().add(new JScrollPane(txt));
            fr.pack();
            fr.setVisible(true);
            desk.add(fr);
            // doPack();
        } catch (Throwable ex) {
            JOptionPane.showMessageDialog(this, "File " + fi + " exception "
                    + ex);
        }
    }

    void table() {
        Object names[] = { "name", "wert", "quadrat" };
        Object values[][] = { { "null", new Float(0.0), new Float(0.0) },
                { "eins", new Float(1.0), new Float(1.0) },
                { "zwei", new Float(2.0), new Float(4.0) },
                { "drei", new Float(3.0), new Float(9.0) } };
        final JTable tab = new JTable(values, names);
        // tab.setPreferredScrollableViewportSize(new Dimension(500,70));
        add(new JScrollPane(tab));
        JButton but = new JButton("list");
        add(but);
        but.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                System.out.println("action " + ev);
                Object o;
                for (int i = 0; i < tab.getRowCount(); i++) {
                    for (int j = 0; j < tab.getColumnCount(); j++)
                        System.out.println(i + ", " + j + ": "
                                + (o = tab.getValueAt(i, j)).getClass() + " = "
                                + o);

                    o = tab.getValueAt(i, 1);
                    if (String.class.isInstance(o))
                        try {
                            float f = Float.parseFloat((String) o);
                            tab.setValueAt(new Float(f * f), i, 2);
                        } catch (Throwable ex) {
                            tab.setValueAt("NaN " + o + ": " + ex, i, 2);
                        }
                }

            }
        });
    }

    void option() {
        final JComboBox cb = new JComboBox(
                new Integer[] { new Integer(-1), new Integer(0),
                        new Integer(1), new Integer(2), new Integer(3) });
        add(cb);
        cb.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                Object sel = cb.getSelectedItem();
                if (!Integer.class.isInstance(sel))
                    JOptionPane.showMessageDialog(cb, "bad Selection class "
                            + sel.getClass() + " of " + sel);
                else
                    JOptionPane.showMessageDialog(cb, "message Dialog Type "
                            + sel, "Title Dialog", ((Integer) sel).intValue());
            }
        });
    }

    public static void main(String[] args) {
        JPanel pan = new ExGui();
        System.out.println("start " + pan.getClass());
        new Java(args, new String[] { "fileView" }).invokeMethods(pan);
        JFrame f = new JFrame("ExGui JFrame");
        f.getContentPane().add(pan);
        f.setSize(400, 500);
        f.pack();
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.setVisible(true);
        System.out.println("end " + pan.getClass());
    }

}