Search in sources :

Example 1 with IconUIResource

use of javax.swing.plaf.IconUIResource in project java-swing-tips by aterai.

the class EmptyIcon method makeTree.

private static JTree makeTree() {
    Icon emptyIcon = new EmptyIcon();
    UIManager.put("Tree.expandedIcon", new IconUIResource(emptyIcon));
    UIManager.put("Tree.collapsedIcon", new IconUIResource(emptyIcon));
    JTree tree = new JTree();
    tree.setEditable(true);
    tree.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
    int row = 0;
    while (row < tree.getRowCount()) {
        tree.expandRow(row++);
    }
    tree.addTreeWillExpandListener(new TreeWillExpandListener() {

        @Override
        public void treeWillExpand(TreeExpansionEvent e) {
        // throws ExpandVetoException {
        // throw new ExpandVetoException(e, "Tree expansion cancelled");
        }

        @Override
        public void treeWillCollapse(TreeExpansionEvent e) throws ExpandVetoException {
            throw new ExpandVetoException(e, "Tree collapse cancelled");
        }
    });
    return tree;
}
Also used : TreeWillExpandListener(javax.swing.event.TreeWillExpandListener) IconUIResource(javax.swing.plaf.IconUIResource) TreeExpansionEvent(javax.swing.event.TreeExpansionEvent) ExpandVetoException(javax.swing.tree.ExpandVetoException)

Example 2 with IconUIResource

use of javax.swing.plaf.IconUIResource in project java-swing-tips by aterai.

the class EmptyIcon method makeRadioPane.

private Box makeRadioPane(JTable table) {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    URL ascendingPath = cl.getResource("example/ascending.png");
    URL descendingPath = cl.getResource("example/descending.png");
    JRadioButton r0 = new JRadioButton("Default", true);
    JRadioButton r1 = new JRadioButton("Empty");
    JRadioButton r2 = new JRadioButton("Custom");
    ActionListener al = e -> {
        JRadioButton r = (JRadioButton) e.getSource();
        Icon ascending;
        Icon descending;
        if (r.equals(r2) && ascendingPath != null && descendingPath != null) {
            ascending = new IconUIResource(new ImageIcon(ascendingPath));
            descending = new IconUIResource(new ImageIcon(descendingPath));
        } else if (r.equals(r1)) {
            ascending = new IconUIResource(EMPTY_ICON);
            descending = new IconUIResource(EMPTY_ICON);
        } else {
            // if (r.equals(r0)) { // default
            ascending = UIManager.getLookAndFeelDefaults().getIcon("Table.ascendingSortIcon");
            descending = UIManager.getLookAndFeelDefaults().getIcon("Table.descendingSortIcon");
        }
        UIManager.put("Table.ascendingSortIcon", ascending);
        UIManager.put("Table.descendingSortIcon", descending);
        table.getTableHeader().repaint();
    };
    Box box1 = Box.createHorizontalBox();
    box1.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
    ButtonGroup bg = new ButtonGroup();
    box1.add(new JLabel("Table Sort Icon: "));
    Stream.of(r0, r1, r2).forEach(rb -> {
        box1.add(rb);
        box1.add(Box.createHorizontalStrut(5));
        bg.add(rb);
        rb.addActionListener(al);
    });
    box1.add(Box.createHorizontalGlue());
    return box1;
}
Also used : java.awt(java.awt) ActionListener(java.awt.event.ActionListener) Stream(java.util.stream.Stream) DefaultTableModel(javax.swing.table.DefaultTableModel) URL(java.net.URL) IconUIResource(javax.swing.plaf.IconUIResource) javax.swing(javax.swing) TableModel(javax.swing.table.TableModel) ActionListener(java.awt.event.ActionListener) IconUIResource(javax.swing.plaf.IconUIResource) URL(java.net.URL)

Aggregations

IconUIResource (javax.swing.plaf.IconUIResource)2 java.awt (java.awt)1 ActionListener (java.awt.event.ActionListener)1 URL (java.net.URL)1 Stream (java.util.stream.Stream)1 javax.swing (javax.swing)1 TreeExpansionEvent (javax.swing.event.TreeExpansionEvent)1 TreeWillExpandListener (javax.swing.event.TreeWillExpandListener)1 DefaultTableModel (javax.swing.table.DefaultTableModel)1 TableModel (javax.swing.table.TableModel)1 ExpandVetoException (javax.swing.tree.ExpandVetoException)1