Example usage for javax.swing DefaultListCellRenderer DefaultListCellRenderer

List of usage examples for javax.swing DefaultListCellRenderer DefaultListCellRenderer

Introduction

In this page you can find the example usage for javax.swing DefaultListCellRenderer DefaultListCellRenderer.

Prototype

public DefaultListCellRenderer() 

Source Link

Document

Constructs a default renderer object for an item in a list.

Usage

From source file:com.googlecode.sarasvati.visual.jung.JungVisualizer.java

@SuppressWarnings("serial")
public static void main(String[] args) throws Exception {
    TestSetup.init();//from   w  w  w .j  a v  a2 s.  c o  m

    Session session = TestSetup.openSession();
    HibEngine engine = new HibEngine(session);

    JFrame frame = new JFrame("Workflow Visualizer");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setMinimumSize(new Dimension(800, 600));

    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    frame.getContentPane().add(splitPane);

    DefaultListModel listModel = new DefaultListModel();
    for (Graph g : engine.getRepository().getGraphs()) {
        listModel.addElement(g);
    }

    ListCellRenderer cellRenderer = new DefaultListCellRenderer() {
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

            Graph g = (Graph) value;

            setText(g.getName() + "." + g.getVersion() + "  ");
            return this;
        }
    };

    final JList graphList = new JList(listModel);
    graphList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    graphList.setCellRenderer(cellRenderer);

    JScrollPane listScrollPane = new JScrollPane(graphList);
    listScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    listScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

    splitPane.add(listScrollPane);

    //TreeLayout<NodeRef, Arc> layout = new TreeLayout<NodeRef, Arc>();

    DirectedSparseMultigraph<Node, Arc> graph = new DirectedSparseMultigraph<Node, Arc>();

    //final SpringLayout2<HibNodeRef, HibArc> layout = new SpringLayout2<HibNodeRef, HibArc>(graph);
    //final KKLayout<HibNodeRef, HibArc> layout = new KKLayout<HibNodeRef, HibArc>(graph);
    final TreeLayout layout = new TreeLayout(graph);
    final BasicVisualizationServer<Node, Arc> vs = new BasicVisualizationServer<Node, Arc>(layout);
    //vs.getRenderContext().setVertexLabelTransformer( new NodeLabeller() );
    //vs.getRenderContext().setEdgeLabelTransformer( new ArcLabeller() );
    vs.getRenderContext().setVertexShapeTransformer(new NodeShapeTransformer());
    vs.getRenderContext().setVertexFillPaintTransformer(new NodeColorTransformer());
    vs.getRenderContext().setLabelOffset(5);
    vs.getRenderContext().setVertexIconTransformer(new Transformer<Node, Icon>() {
        @Override
        public Icon transform(Node node) {
            return "task".equals(node.getType()) ? new TaskIcon(node) : null;
        }
    });

    Transformer<Arc, Paint> edgeColorTrans = new Transformer<Arc, Paint>() {
        private Color darkRed = new Color(128, 0, 0);

        @Override
        public Paint transform(Arc arc) {
            return "reject".equals(arc.getName()) ? darkRed : Color.black;
        }
    };

    vs.getRenderContext().setEdgeDrawPaintTransformer(edgeColorTrans);
    vs.getRenderContext().setArrowDrawPaintTransformer(edgeColorTrans);

    final JScrollPane scrollPane = new JScrollPane(vs);
    scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);

    splitPane.add(scrollPane);
    scrollPane.setBackground(Color.white);

    graphList.addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting()) {
                return;
            }

            final Graph g = (Graph) graphList.getSelectedValue();

            if ((g == null && currentGraph == null) || (g != null && g.equals(currentGraph))) {
                return;
            }

            currentGraph = g;

            DirectedSparseMultigraph<Node, Arc> jungGraph = new DirectedSparseMultigraph<Node, Arc>();

            for (Node ref : currentGraph.getNodes()) {
                jungGraph.addVertex(ref);
            }

            for (Arc arc : currentGraph.getArcs()) {
                jungGraph.addEdge(arc, arc.getStartNode(), arc.getEndNode());
            }

            GraphTree graphTree = new GraphTree(g);
            layout.setGraph(jungGraph);
            layout.setInitializer(new NodeLocationTransformer(graphTree));
            scrollPane.repaint();
        }
    });

    frame.setVisible(true);
}

From source file:Main.java

/**
 * Used to get a cell renderer for JLists that shows no visible selection
 * /*  w ww . j  a  v a 2s . co m*/
 * @return
 */
public static ListCellRenderer<?> createInvisibleCellRenderer() {
    return new DefaultListCellRenderer() {
        public Component getListCellRendererComponent(JList<?> list, Object value, int index,
                boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, false, false);

            return this;
        }
    };
}

From source file:Main.java

private ListCellRenderer<? super String> getRenderer() {
    return new DefaultListCellRenderer() {
        @Override/* ww w.java  2 s .co  m*/
        public Component getListCellRendererComponent(JList<?> list, Object value, int index,
                boolean isSelected, boolean cellHasFocus) {
            JLabel listCellRendererComponent = (JLabel) super.getListCellRendererComponent(list, value, index,
                    isSelected, cellHasFocus);
            listCellRendererComponent.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));
            return listCellRendererComponent;
        }
    };
}

From source file:Main.java

private void setListCellRendererOf(JComboBox comboBox) {
    comboBox.setRenderer(new ListCellRenderer() {
        @Override//  w w w.j  a va  2s.c  o  m
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                boolean cellHasFocus) {

            Component component = new DefaultListCellRenderer().getListCellRendererComponent(list, value, index,
                    isSelected, cellHasFocus);

            component.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
            return component;
        }
    });
}

From source file:Main.java

public Main() {
    add(myCombo);//from  w  w  w  . j av  a 2  s. com
    add(new JLabel("text:"));
    add(textField);
    add(new JLabel("value:"));
    add(valueField);
    add(new JLabel("weekend:"));
    add(weekendField);

    myCombo.setRenderer(new DefaultListCellRenderer() {
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                boolean cellHasFocus) {
            String text = value == null ? "" : ((MyData) value).getText();
            return super.getListCellRendererComponent(list, text, index, isSelected, cellHasFocus);
        }
    });
    myCombo.setSelectedIndex(-1);

    myCombo.addActionListener(e -> {
        MyData myData = (MyData) myCombo.getSelectedItem();
        textField.setText(myData.getText());
        valueField.setText(String.valueOf(myData.getValue()));
        weekendField.setText(String.valueOf(myData.isWeekend()));
    });
}

From source file:com.microsoft.alm.plugin.idea.git.ui.branch.CreateBranchForm.java

private void ensureInitialized() {
    if (!this.initialized) {
        // override the renderer so it doesn't show the object toString but instead shows the branch name
        remoteBranchComboBox.setRenderer(new DefaultListCellRenderer() {
            @Override//ww  w .  ja v a2 s . c  om
            public Component getListCellRendererComponent(JList list, Object value, int index,
                    boolean isSelected, boolean cellHasFocus) {
                return super.getListCellRendererComponent(list,
                        value != null ? ((GitRemoteBranch) value).getName() : StringUtils.EMPTY, index,
                        isSelected, cellHasFocus);
            }
        });

        this.initialized = true;
    }
}

From source file:gui.SpamPanel.java

public void generate() {
    Message[] arrMsg = GmailAPI.Spam.toArray(new Message[GmailAPI.Spam.size()]);
    SpamList = new JList(arrMsg);
    SpamList.setCellRenderer(new DefaultListCellRenderer() { // Setting the DefaultListCellRenderer
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                boolean cellHasFocus) {
            Message message = (Message) value; // Using value we are getting the object in JList
            Map<String, String> map = null;
            try {
                map = GmailAPI.getMessageDetails(message.getId());
            } catch (MessagingException ex) {
                Logger.getLogger(SpamPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(SpamPanel.class.getName()).log(Level.SEVERE, null, ex);
            }//from w  w  w. j  a  va2 s  . c om
            String sub = map.get("subject");
            if (map.get("subject").length() > 22) {
                sub = map.get("subject").substring(0, 20) + "...";
            }
            setText(sub); // Setting the text
            //setIcon( shape.getImage() ); // Setting the Image Icon
            return this;
        }
    });
    SpamList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    SpamList.setLayoutOrientation(JList.VERTICAL);
    SpamList.setVisibleRowCount(-1);
    jScrollPane1.setViewportView(SpamList);

    SpamList.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent evt) {
            try {
                JList list = (JList) evt.getSource();
                int index = list.locationToIndex(evt.getPoint());
                String id = arrMsg[index].getId();
                Map<String, String> map = GmailAPI.getMessageDetails(id);
                jTextField1.setText(map.get("from"));
                jTextField2.setText(map.get("subject"));
                dateTextField.setText(map.get("senddate"));
                BodyTextPane.setText(map.get("body"));
                BodyTextPane.setContentType("text/html");
                //BodyTextArea.setCo
            } catch (IOException ex) {
                Logger.getLogger(SpamPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (MessagingException ex) {
                Logger.getLogger(SpamPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });
}

From source file:gui.InboxPanel.java

public void generate() {
    Message[] arrMsg = GmailAPI.Inbox.toArray(new Message[GmailAPI.Inbox.size()]);
    InboxList = new JList(arrMsg);
    InboxList.setCellRenderer(new DefaultListCellRenderer() { // Setting the DefaultListCellRenderer
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                boolean cellHasFocus) {
            Message message = (Message) value; // Using value we are getting the object in JList
            Map<String, String> map = null;
            try {
                map = GmailAPI.getMessageDetails(message.getId());
            } catch (MessagingException ex) {
                Logger.getLogger(InboxPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(InboxPanel.class.getName()).log(Level.SEVERE, null, ex);
            }/*from w  ww .j  a  va  2 s .  com*/
            String sub = map.get("subject");
            if (map.get("subject").length() > 22) {
                sub = map.get("subject").substring(0, 20) + "...";
            }
            setText(sub); // Setting the text
            //setIcon( shape.getImage() ); // Setting the Image Icon
            return this;
        }
    });
    InboxList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    InboxList.setLayoutOrientation(JList.VERTICAL);
    InboxList.setVisibleRowCount(-1);
    jScrollPane1.setViewportView(InboxList);

    InboxList.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent evt) {
            try {
                JList list = (JList) evt.getSource();
                int index = list.locationToIndex(evt.getPoint());
                String id = arrMsg[index].getId();
                curId = id;
                Map<String, String> map = GmailAPI.getMessageDetails(id);
                jTextField1.setText(map.get("from"));
                jTextField2.setText(map.get("subject"));
                dateTextField.setText(map.get("senddate"));
                BodyTextPane.setText(map.get("body"));
                //BodyTextPane.setContentType("text/html");
                //BodyTextArea.setCo
            } catch (IOException ex) {
                Logger.getLogger(InboxPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (MessagingException ex) {
                Logger.getLogger(InboxPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });
}

From source file:entity.files.SYSFilesTools.java

public static ListCellRenderer getSYSFilesRenderer() {
    //        final int v = verbosity;
    return new ListCellRenderer() {
        @Override/*from   ww  w.ja v a2  s.co m*/
        public Component getListCellRendererComponent(JList jList, Object o, int i, boolean isSelected,
                boolean cellHasFocus) {
            String text;
            if (o == null) {
                text = SYSTools.toHTML("<i>Keine Auswahl</i>");
            } else if (o instanceof SYSFiles) {
                SYSFiles sysfile = (SYSFiles) o;
                text = sysfile.getFilename() + SYSTools.catchNull(sysfile.getBeschreibung(), " (", ")");
            } else {
                text = o.toString();
            }
            return new DefaultListCellRenderer().getListCellRendererComponent(jList, text, i, isSelected,
                    cellHasFocus);
        }
    };
}

From source file:components.SharedModelDemo.java

public SharedModelDemo() {
    super(new BorderLayout());

    Vector data = new Vector(7);
    String[] columnNames = { "French", "Spanish", "Italian" };
    String[] oneData = { "un", "uno", "uno" };
    String[] twoData = { "deux", "dos", "due" };
    String[] threeData = { "trois", "tres", "tre" };
    String[] fourData = { "quatre", "cuatro", "quattro" };
    String[] fiveData = { "cinq", "cinco", "cinque" };
    String[] sixData = { "six", "seis", "sei" };
    String[] sevenData = { "sept", "siete", "sette" };

    //Build the model.
    SharedDataModel dataModel = new SharedDataModel(columnNames);
    dataModel.addElement(oneData);//from   ww  w  . ja v  a2s.  c o  m
    dataModel.addElement(twoData);
    dataModel.addElement(threeData);
    dataModel.addElement(fourData);
    dataModel.addElement(fiveData);
    dataModel.addElement(sixData);
    dataModel.addElement(sevenData);

    list = new JList(dataModel);
    list.setCellRenderer(new DefaultListCellRenderer() {
        public Component getListCellRendererComponent(JList l, Object value, int i, boolean s, boolean f) {
            String[] array = (String[]) value;
            return super.getListCellRendererComponent(l, array[0], i, s, f);
        }
    });

    listSelectionModel = list.getSelectionModel();
    listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());
    JScrollPane listPane = new JScrollPane(list);

    table = new JTable(dataModel);
    table.setSelectionModel(listSelectionModel);
    JScrollPane tablePane = new JScrollPane(table);

    //Build control area (use default FlowLayout).
    JPanel controlPane = new JPanel();
    String[] modes = { "SINGLE_SELECTION", "SINGLE_INTERVAL_SELECTION", "MULTIPLE_INTERVAL_SELECTION" };

    final JComboBox comboBox = new JComboBox(modes);
    comboBox.setSelectedIndex(2);
    comboBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String newMode = (String) comboBox.getSelectedItem();
            if (newMode.equals("SINGLE_SELECTION")) {
                listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            } else if (newMode.equals("SINGLE_INTERVAL_SELECTION")) {
                listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
            } else {
                listSelectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
            }
            output.append("----------" + "Mode: " + newMode + "----------" + newline);
        }
    });
    controlPane.add(new JLabel("Selection mode:"));
    controlPane.add(comboBox);

    //Build output area.
    output = new JTextArea(10, 40);
    output.setEditable(false);
    JScrollPane outputPane = new JScrollPane(output, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

    //Do the layout.
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    add(splitPane, BorderLayout.CENTER);

    JPanel topHalf = new JPanel();
    topHalf.setLayout(new BoxLayout(topHalf, BoxLayout.X_AXIS));
    JPanel listContainer = new JPanel(new GridLayout(1, 1));
    listContainer.setBorder(BorderFactory.createTitledBorder("List"));
    listContainer.add(listPane);
    JPanel tableContainer = new JPanel(new GridLayout(1, 1));
    tableContainer.setBorder(BorderFactory.createTitledBorder("Table"));
    tableContainer.add(tablePane);
    tablePane.setPreferredSize(new Dimension(300, 100));
    topHalf.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
    topHalf.add(listContainer);
    topHalf.add(tableContainer);

    topHalf.setMinimumSize(new Dimension(400, 50));
    topHalf.setPreferredSize(new Dimension(400, 110));
    splitPane.add(topHalf);

    JPanel bottomHalf = new JPanel(new BorderLayout());
    bottomHalf.add(controlPane, BorderLayout.NORTH);
    bottomHalf.add(outputPane, BorderLayout.CENTER);
    //XXX: next line needed if bottomHalf is a scroll pane:
    //bottomHalf.setMinimumSize(new Dimension(400, 50));
    bottomHalf.setPreferredSize(new Dimension(450, 135));
    splitPane.add(bottomHalf);
}