Example usage for java.awt GridBagConstraints GridBagConstraints

List of usage examples for java.awt GridBagConstraints GridBagConstraints

Introduction

In this page you can find the example usage for java.awt GridBagConstraints GridBagConstraints.

Prototype

public GridBagConstraints() 

Source Link

Document

Creates a GridBagConstraint object with all of its fields set to their default value.

Usage

From source file:ContainerEventDemo.java

public ContainerEventDemo() {
    super(new GridBagLayout());
    GridBagLayout gridbag = (GridBagLayout) getLayout();
    GridBagConstraints c = new GridBagConstraints();

    // Initialize an empty list of buttons.
    buttonList = new Vector<JButton>(10, 10);

    // Create all the components.
    addButton = new JButton("Add a button");
    addButton.setActionCommand(ADD);//from  ww w .j ava 2  s  . c  o  m
    addButton.addActionListener(this);

    removeButton = new JButton("Remove a button");
    removeButton.setActionCommand(REMOVE);
    removeButton.addActionListener(this);

    buttonPanel = new JPanel(new GridLayout(1, 1));
    buttonPanel.setPreferredSize(new Dimension(200, 75));
    buttonPanel.addContainerListener(this);

    display = new JTextArea();
    display.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(display);
    scrollPane.setPreferredSize(new Dimension(200, 75));

    clearButton = new JButton("Clear text area");
    clearButton.setActionCommand(CLEAR);
    clearButton.addActionListener(this);

    c.fill = GridBagConstraints.BOTH; // Fill entire cell.
    c.weighty = 1.0; // Button area and message area have equal height.
    c.gridwidth = GridBagConstraints.REMAINDER; // end of row
    gridbag.setConstraints(scrollPane, c);
    add(scrollPane);

    c.weighty = 0.0;
    gridbag.setConstraints(clearButton, c);
    add(clearButton);

    c.weightx = 1.0; // Add/remove buttons have equal width.
    c.gridwidth = 1; // NOT end of row
    gridbag.setConstraints(addButton, c);
    add(addButton);

    c.gridwidth = GridBagConstraints.REMAINDER; // end of row
    gridbag.setConstraints(removeButton, c);
    add(removeButton);

    c.weighty = 1.0; // Button area and message area have equal height.
    gridbag.setConstraints(buttonPanel, c);
    add(buttonPanel);

    setPreferredSize(new Dimension(400, 400));
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}

From source file:ClipboardTest.java

public ClipboardTest() {
    super("Clipboard Test");
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    setLayout(gridbag);/* www  . j a  v  a2 s.c o  m*/

    srcText = new TextArea(8, 32);
    c.gridwidth = 2;
    c.anchor = GridBagConstraints.CENTER;
    gridbag.setConstraints(srcText, c);
    add(srcText);

    copyButton = new Button("Copy Above");
    copyButton.setActionCommand("copy");
    copyButton.addActionListener(this);
    c.gridy = 1;
    c.gridwidth = 1;
    gridbag.setConstraints(copyButton, c);
    add(copyButton);

    pasteButton = new Button("Paste Below");
    pasteButton.setActionCommand("paste");
    pasteButton.addActionListener(this);
    pasteButton.setEnabled(false);
    c.gridx = 1;
    gridbag.setConstraints(pasteButton, c);
    add(pasteButton);

    dstText = new TextArea(8, 32);
    c.gridx = 0;
    c.gridy = 2;
    c.gridwidth = 2;
    gridbag.setConstraints(dstText, c);
    add(dstText);

    pack();
}

From source file:com.moss.greenshell.state.StateTray.java

public void reset() {
    widgets.clear();/*from  w  w w  .  j  a v a 2s . com*/
    removeAll();
    setLayout(new GridBagLayout());
    c = new GridBagConstraints();
    c.insets.left = 15;
    c.insets.top = 5;
    c.insets.bottom = 5;
    c.fill = GridBagConstraints.BOTH;
    repaint();
}

From source file:Demo.HistGraph.java

public HistGraph(List<String> paraType_list) {
    this.paraType_list = paraType_list;

    // widgets: charts(JFreeChart), parameter selector (JList)
    chartPane = new ChartPanel(null);
    charts = new JFreeChart[paraType_list.size()];

    DefaultListModel modelOfList = new DefaultListModel();
    paraSelector = new JList(modelOfList);

    for (int i = 0; i < paraType_list.size(); i++) {
        modelOfList.addElement(paraType_list.get(i));
        charts[i] = null;//w ww  .ja  v a2s .  c o m
    }

    paraSelector.setSelectedIndex(0);

    charts[0] = CreateChart(paraType_list.get(0));
    chartPane.setChart(charts[0]);

    JScrollPane selectorPanel = new JScrollPane();
    selectorPanel.setViewportView(paraSelector);

    paraSelector.addMouseListener(this);

    // layout
    GridBagLayout layout = new GridBagLayout();
    setLayout(layout);

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.BOTH;

    gbc.weighty = 1;
    gbc.anchor = GridBagConstraints.WEST;

    gbc.insets = new Insets(5, 20, 5, 5);
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.weightx = 20;
    add(new JLabel("Distribution:"), gbc);

    gbc.insets = new Insets(5, 5, 5, 20);
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.weightx = 1;
    add(new JLabel("Parameters:"), gbc);

    gbc.weighty = 14;

    gbc.insets = new Insets(5, 20, 5, 5);
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.weightx = 20;
    add(chartPane, gbc);

    gbc.insets = new Insets(5, 5, 15, 20);
    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.weightx = 1;
    add(selectorPanel, gbc);
}

From source file:MultipartViewer.java

protected void setupDisplay(Multipart mp) {
    // we display the first body part in a main frame on the left, and then
    // on the right we display the rest of the parts as attachments

    GridBagConstraints gc = new GridBagConstraints();
    gc.gridheight = GridBagConstraints.REMAINDER;
    gc.fill = GridBagConstraints.BOTH;
    gc.weightx = 1.0;//from ww w .j  a va2s  .co  m
    gc.weighty = 1.0;

    // get the first part
    try {
        BodyPart bp = mp.getBodyPart(0);
        Component comp = getComponent(bp);
        add(comp, gc);

    } catch (MessagingException me) {
        add(new Label(me.toString()), gc);
    }

    // see if there are more than one parts
    try {
        int count = mp.getCount();

        // setup how to display them
        gc.gridwidth = GridBagConstraints.REMAINDER;
        gc.gridheight = 1;
        gc.fill = GridBagConstraints.NONE;
        gc.anchor = GridBagConstraints.NORTH;
        gc.weightx = 0.0;
        gc.weighty = 0.0;
        gc.insets = new Insets(4, 4, 4, 4);

        // for each one we create a button with the content type
        for (int i = 1; i < count; i++) { // we skip the first one 
            BodyPart curr = mp.getBodyPart(i);
            String label = null;
            if (label == null)
                label = curr.getFileName();
            if (label == null)
                label = curr.getDescription();
            if (label == null)
                label = curr.getContentType();

            Button but = new Button(label);
            but.addActionListener(new AttachmentViewer(curr));
            add(but, gc);
        }

    } catch (MessagingException me2) {
        me2.printStackTrace();
    }

}

From source file:EditorPaneExample11.java

public EditorPaneExample11() {
    super("JEditorPane Example 11");

    pane = new JEditorPane();
    pane.setEditable(false); // Read-only
    getContentPane().add(new JScrollPane(pane), "Center");

    // Build the panel of controls
    JPanel panel = new JPanel();

    panel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridwidth = 1;// w w w .j a  v a 2s  .  c o m
    c.gridheight = 1;
    c.anchor = GridBagConstraints.EAST;
    c.fill = GridBagConstraints.NONE;
    c.weightx = 0.0;
    c.weighty = 0.0;

    JLabel urlLabel = new JLabel("URL: ", JLabel.RIGHT);
    panel.add(urlLabel, c);
    JLabel loadingLabel = new JLabel("State: ", JLabel.RIGHT);
    c.gridy = 1;
    panel.add(loadingLabel, c);
    JLabel typeLabel = new JLabel("Type: ", JLabel.RIGHT);
    c.gridy = 2;
    panel.add(typeLabel, c);
    c.gridy = 3;
    panel.add(new JLabel(LOAD_TIME), c);

    c.gridy = 4;
    c.gridwidth = 2;
    c.weightx = 1.0;
    c.anchor = GridBagConstraints.WEST;
    onlineLoad = new JCheckBox("Online Load");
    panel.add(onlineLoad, c);
    onlineLoad.setSelected(true);
    onlineLoad.setForeground(typeLabel.getForeground());

    c.gridx = 1;
    c.gridy = 0;
    c.anchor = GridBagConstraints.EAST;
    c.fill = GridBagConstraints.HORIZONTAL;

    urlCombo = new JComboBox();
    panel.add(urlCombo, c);
    urlCombo.setEditable(true);
    loadingState = new JLabel(spaces, JLabel.LEFT);
    loadingState.setForeground(Color.black);
    c.gridy = 1;
    panel.add(loadingState, c);
    loadedType = new JLabel(spaces, JLabel.LEFT);
    loadedType.setForeground(Color.black);
    c.gridy = 2;
    panel.add(loadedType, c);
    timeLabel = new JLabel("");
    c.gridy = 3;
    panel.add(timeLabel, c);

    getContentPane().add(panel, "South");

    // Allocate the empty tree model
    DefaultMutableTreeNode emptyRootNode = new DefaultMutableTreeNode("Empty");
    emptyModel = new DefaultTreeModel(emptyRootNode);

    // Create and place the heading tree
    tree = new JTree(emptyModel);
    tree.setPreferredSize(new Dimension(200, 200));
    getContentPane().add(new JScrollPane(tree), "East");

    // Change page based on combo selection
    urlCombo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            if (populatingCombo == true) {
                return;
            }
            Object selection = urlCombo.getSelectedItem();
            try {
                // Check if the new page and the old
                // page are the same.
                URL url;
                if (selection instanceof URL) {
                    url = (URL) selection;
                } else {
                    url = new URL((String) selection);
                }

                URL loadedURL = pane.getPage();
                if (loadedURL != null && loadedURL.sameFile(url)) {
                    return;
                }

                // Try to display the page
                urlCombo.setEnabled(false); // Disable input
                urlCombo.paintImmediately(0, 0, urlCombo.getSize().width, urlCombo.getSize().height);
                setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                // Busy cursor
                loadingState.setText("Loading...");
                loadingState.paintImmediately(0, 0, loadingState.getSize().width,
                        loadingState.getSize().height);
                loadedType.setText("");
                loadedType.paintImmediately(0, 0, loadedType.getSize().width, loadedType.getSize().height);

                timeLabel.setText("");
                timeLabel.paintImmediately(0, 0, timeLabel.getSize().width, timeLabel.getSize().height);

                // Display an empty tree while loading
                tree.setModel(emptyModel);
                tree.paintImmediately(0, 0, tree.getSize().width, tree.getSize().height);

                startTime = System.currentTimeMillis();

                // Choose the loading method
                if (onlineLoad.isSelected()) {
                    // Usual load via setPage
                    pane.setPage(url);
                    loadedType.setText(pane.getContentType());
                } else {
                    pane.setContentType("text/html");
                    loadedType.setText(pane.getContentType());
                    if (loader == null) {
                        loader = new HTMLDocumentLoader();
                    }
                    HTMLDocument doc = loader.loadDocument(url);
                    loadComplete();
                    pane.setDocument(doc);
                    displayLoadTime();
                    populateCombo(findLinks(doc, null));
                    TreeNode node = buildHeadingTree(doc);
                    tree.setModel(new DefaultTreeModel(node));
                    enableInput();
                }
            } catch (Exception e) {
                System.out.println(e);
                JOptionPane.showMessageDialog(pane,
                        new String[] { "Unable to open file", selection.toString() }, "File Open Error",
                        JOptionPane.ERROR_MESSAGE);
                loadingState.setText("Failed");
                enableInput();
            }
        }
    });

    // Listen for page load to complete
    pane.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals("page")) {
                loadComplete();
                displayLoadTime();
                populateCombo(findLinks(pane.getDocument(), null));
                TreeNode node = buildHeadingTree(pane.getDocument());
                tree.setModel(new DefaultTreeModel(node));
                enableInput();
            }
        }
    });

    // Listener for tree selection
    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent evt) {
            TreePath path = evt.getNewLeadSelectionPath();
            if (path != null) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
                Object userObject = node.getUserObject();
                if (userObject instanceof Heading) {
                    Heading heading = (Heading) userObject;
                    try {
                        Rectangle textRect = pane.modelToView(heading.getOffset());
                        textRect.y += 3 * textRect.height;
                        pane.scrollRectToVisible(textRect);
                    } catch (BadLocationException e) {
                    }
                }
            }
        }
    });
}

From source file:fi.elfcloud.client.dialog.ModifyDataItemDialog.java

public ModifyDataItemDialog(JFrame parent, HashMap<String, String> metaMap, String diName) {
    super(parent, true);
    setTitle(Messages.getString("ModifyDataItemDialog.window_title") + diName); //$NON-NLS-1$
    this.metamap = metaMap;
    tagVector = new Vector<String>();
    setLocationRelativeTo(parent);//  w  w w. jav a2 s  .  c o  m
    panel.setLayout(new GridBagLayout());
    GridBagConstraints cs = new GridBagConstraints();
    cs.insets = new Insets(0, 0, 5, 0);

    lblName = new JLabel(Messages.getString("ModifyDataItemDialog.label_name")); //$NON-NLS-1$
    cs.gridx = 0;
    cs.gridy = 0;
    cs.fill = GridBagConstraints.HORIZONTAL;
    cs.gridwidth = 1;
    cs.weightx = 0;
    panel.add(lblName, cs);

    name = new JTextField(getWidth());
    cs.gridx = GridBagConstraints.RELATIVE;
    cs.gridy = 0;
    cs.fill = GridBagConstraints.HORIZONTAL;
    cs.gridwidth = 2;
    cs.weightx = 1.0;
    name.setText(diName);
    panel.add(name, cs);

    lblDescription = new JLabel(Messages.getString("ModifyDataItemDialog.label_description")); //$NON-NLS-1$
    cs.gridx = 0;
    cs.gridy = 1;
    cs.gridwidth = 1;
    cs.fill = GridBagConstraints.HORIZONTAL;
    cs.weightx = 0;
    panel.add(lblDescription, cs);

    description = new JTextField(getWidth());
    cs.gridx = GridBagConstraints.RELATIVE;
    cs.gridy = 1;
    cs.gridwidth = 2;
    cs.fill = GridBagConstraints.HORIZONTAL;
    cs.weightx = 1.0;
    description.setText(metaMap.get("DSC")); //$NON-NLS-1$
    panel.add(description, cs);

    lblTags = new JLabel(Messages.getString("ModifyDataItemDialog.label_tags")); //$NON-NLS-1$
    cs.gridx = 0;
    cs.gridy = 2;
    cs.gridwidth = 1;
    cs.fill = GridBagConstraints.HORIZONTAL;
    cs.weightx = 0;
    panel.add(lblTags, cs);

    tags = new JTextField(getWidth());
    cs.gridx = GridBagConstraints.RELATIVE;
    cs.gridy = 2;
    cs.fill = GridBagConstraints.HORIZONTAL;
    cs.weightx = 1.0;
    panel.add(tags, cs);

    JButton addButton = new JButton(Messages.getString("ModifyDataItemDialog.button_add_tag")); //$NON-NLS-1$
    cs.gridx = GridBagConstraints.RELATIVE;
    cs.gridy = 2;
    cs.gridwidth = 1;
    cs.weightx = 0;
    addButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String tagText = tags.getText().trim();
            if (!tagText.equals("")) { //$NON-NLS-1$
                addTags(tagText);
            }
            tags.setText(""); //$NON-NLS-1$

            checkboxPanel.revalidate();
            checkboxPanel.repaint();
        }
    });
    panel.add(addButton, cs);

    checkboxPanel = new JPanel(new WrapLayout(FlowLayout.LEFT));
    checkboxPanel.setSize(new Dimension(320, 1));
    try {
        addTags(metamap.get("TGS")); //$NON-NLS-1$
    } catch (NullPointerException e) {
        // No existing tags
    }
    panel.setBorder(new EmptyBorder(5, 5, 0, 5));
    getContentPane().add(panel, BorderLayout.NORTH);
    scrollPane = new JScrollPane(checkboxPanel);
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scrollPane.setBorder(null);
    getContentPane().add(scrollPane, BorderLayout.CENTER);

    JButton generateButton = new JButton(Messages.getString("ModifyDataItemDialog.button_save")); //$NON-NLS-1$
    generateButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (tags.getText().length() > 0) {
                addTags(tags.getText());
            }
            answer = true;
            setVisible(false);
        }
    });
    JButton cancelButton = new JButton(Messages.getString("ModifyDataItemDialog.button_cancel")); //$NON-NLS-1$
    cancelButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            answer = false;
            setVisible(false);
        }
    });
    JPanel bp = new JPanel();
    bp.add(generateButton);
    bp.add(cancelButton);
    getContentPane().add(bp, BorderLayout.PAGE_END);

    setMinimumSize(new Dimension(320, 350));
    pack();
}

From source file:TextDemo.java

public TextDemo() {
    super(new GridBagLayout());

    textField = new JTextField(20);
    textField.addActionListener(this);

    textArea = new JTextArea(5, 20);
    textArea.setEditable(false);//from www  .j  a  v  a 2s . c om
    JScrollPane scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    //Add Components to this panel.
    GridBagConstraints c = new GridBagConstraints();
    c.gridwidth = GridBagConstraints.REMAINDER;

    c.fill = GridBagConstraints.HORIZONTAL;
    add(textField, c);

    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1.0;
    c.weighty = 1.0;
    add(scrollPane, c);
}

From source file:com.std.StockPanel.java

public void setHistoricalData() {
    this.setLayout(new BorderLayout());
    nameAndChangePanel.setLayout(new BoxLayout(nameAndChangePanel, BoxLayout.PAGE_AXIS));
    nameAndChangePanel.add(nameLbl);//  ww  w. j  ava2  s.  c o m
    nameAndChangePanel.add(priceChangePercentLbl);

    data = new Object[30][2];
    String[] colnames = { "1", "2" };

    GridBagConstraints c = new GridBagConstraints();
    dataAndGraph.setLayout(new GridBagLayout());
    c.anchor = GridBagConstraints.NORTHWEST;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1;
    c.weighty = 0;
    c.gridheight = 1;
    c.insets = new Insets(0, 20, 0, 20);
    dataAndGraph.add(nameAndChangePanel, c);
    c.weightx = 0;
    c.weighty = 1;
    c.insets = new Insets(0, 20, 0, 0);
    //c.ipady = 20;
    c.gridx = 0;
    c.gridy = 1;
    remainingInfoTable = new StockTable(data, colnames);
    remainingInfoTable.setShowGrid(false);
    remainingInfoTable.setTableHeader(null);
    remainingInfoTable.setBackground(this.getBackground());
    remainingInfoTable.setFocusable(false);
    remainingInfoTable.setColumnSelectionAllowed(false);
    remainingInfoTable.setRowSelectionAllowed(false);
    remainingInfoTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    dataAndGraph.add(remainingInfoTable, c);
    this.add(dataAndGraph, BorderLayout.CENTER);

    c.anchor = GridBagConstraints.NORTHWEST;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1;
    c.weighty = 0;
    c.gridheight = 2;
    //c.ipady = 20;
    c.gridx = 1;
    c.gridy = 0;
    dataAndGraph.add(chartPane, c);
    chartPane.setVisible(false);
    this.add(dataAndGraph, BorderLayout.CENTER);

    Test.show_hist_data(currentStock, b);
    currentStock.calculate_beta(sp500, 1200);

    nameLbl.setText(currentStock.get_name() + " (" + currentStock.get_symbol() + ")");
    nameLbl.setFont(new Font(nameLbl.getName(), Font.BOLD, 24));
    priceChangePercentLbl.setText(currentStock.get_price() + " ");
    String message = currentStock.get_change() + " " + currentStock.get_percent_change();
    if (Double.parseDouble(currentStock.get_change()) < 0)
        priceChangePercentLbl.setText(String.format("<html>%s<font color='red'>%s</font></html>",
                priceChangePercentLbl.getText(), message));
    else
        priceChangePercentLbl.setText(String.format("<html>%s<font color='green'>%s</font></html>",
                priceChangePercentLbl.getText(), message));
    priceChangePercentLbl.setFont(new Font(priceChangePercentLbl.getName(), Font.PLAIN, 15));

    data[0][0] = "Prev Close: " + currentStock.get_prev_close();
    data[1][0] = "Open: " + currentStock.get_open_price();
    data[2][0] = "Bid: " + currentStock.get_bid();
    data[3][0] = "Ask: " + currentStock.get_ask();
    data[4][0] = "One Year Target: " + currentStock.get_one_year_target();
    data[5][0] = "Ebita: " + currentStock.get_ebitda();
    data[0][1] = "Day Range: " + currentStock.get_days_range();
    data[1][1] = "52 Week Range: " + currentStock.get_year_range();
    data[2][1] = "Volume: " + currentStock.get_volume();
    data[3][1] = "Average Daily Volume: " + currentStock.get_avg_daily_volume();
    data[4][1] = "Market Cap: " + currentStock.get_market_cap();
    data[5][1] = "P/E: " + currentStock.get_pe();
    data[6][1] = "EPS: " + currentStock.get_earnings_per_share();
    data[7][1] = "Dividend (Yield): " + currentStock.get_dividend_per_share() + "("
            + currentStock.get_dividend_yield() + ")";
    data[6][0] = "Reveune:" + currentStock.get_revenue();
    data[7][0] = "Earnings Estimate: " + currentStock.get_earnings_estimate_current_year();
    data[8][0] = "Beta: " + currentStock.get_beta();
    data[8][1] = "PEG Ratio: " + currentStock.get_peg_ratio();
    data[9][0] = "Short Ratio: " + currentStock.get_short_ratio();

    data[11][0] = "50 Day MA: " + currentStock.get_fiftyday_moving_avg();
    data[12][0] = "200 Day MA: " + currentStock.get_twohundredday_moving_avg();
    if (currentStock.get_change_from_fiftyday_moving_avg() != null
            && currentStock.get_change_from_fiftyday_moving_avg().contains("-")) {
        data[13][0] = String.format("<html>%s<font color='red'>%s</font></html>", "Change 50 Day MA: ",
                currentStock.get_change_from_fiftyday_moving_avg());
        data[14][0] = String.format("<html>%s<font color='red'>%s</font></html>", "Percent 50 Day MA: ",
                currentStock.get_percent_change_from_fiftyday_moving_avg());

    } else {
        data[13][0] = String.format("<html>%s<font color='green'>%s</font></html>", "Change 50 Day MA: ",
                currentStock.get_change_from_fiftyday_moving_avg());
        data[14][0] = String.format("<html>%s<font color='green'>%s</font></html>", "Percent 50 Day MA: ",
                currentStock.get_percent_change_from_fiftyday_moving_avg());
    }
    if (currentStock.get_change_from_twohundredday_moving_avg() != null
            && currentStock.get_change_from_twohundredday_moving_avg().contains("-")) {
        data[15][0] = String.format("<html>%s<font color='red'>%s</font></html>", "Change 200 Day MA: ",
                currentStock.get_change_from_twohundredday_moving_avg());
        data[16][0] = String.format("<html>%s<font color='red'>%s</font></html>", "Percent 200 Day MA: ",
                currentStock.get_percent_change_from_twohundredday_moving_avg());
    } else {
        data[15][0] = String.format("<html>%s<font color='green'>%s</font></html>", "Change 200 Day MA: ",
                currentStock.get_change_from_twohundredday_moving_avg());
        data[16][0] = String.format("<html>%s<font color='green'>%s</font></html>", "Percent 200 Day MA: ",
                currentStock.get_percent_change_from_twohundredday_moving_avg());
    }
    data[17][0] = "Year High: " + currentStock.get_year_high();
    data[18][0] = "Year Low: " + currentStock.get_year_low();
    if (currentStock.get_change_from_year_high() != null
            && currentStock.get_change_from_year_high().contains("-")) {
        data[19][0] = String.format("<html>%s<font color='red'>%s</font></html>", "Year High Change: ",
                currentStock.get_change_from_year_high());
        data[20][0] = String.format("<html>%s<font color='red'>%s</font></html>", "Year High Percent: ",
                currentStock.get_percent_change_from_year_high());
    } else {
        data[19][0] = String.format("<html>%s<font color='green'>%s</font></html>", "Year High Change: ",
                currentStock.get_change_from_year_high());
        data[20][0] = String.format("<html>%s<font color='green'>%s</font></html>", "Year High Percent: ",
                currentStock.get_percent_change_from_year_high());
    }
    if (currentStock.get_change_from_year_low() != null
            && currentStock.get_change_from_year_low().contains("-")) {
        data[21][0] = String.format("<html>%s<font color='red'>%s</font></html>", "Year Low Change: ",
                currentStock.get_change_from_year_low());
        data[22][0] = String.format("<html>%s<font color='red'>%s</font></html>", "Year Low Percent:  ",
                currentStock.get_percent_change_from_year_low());
    } else {
        data[21][0] = String.format("<html>%s<font color='green'>%s</font></html>", "Year Low Change: ",
                currentStock.get_change_from_year_low());
        data[22][0] = String.format("<html>%s<font color='green'>%s</font></html>", "Year Low Percent:  ",
                currentStock.get_percent_change_from_year_low());
    }
    if (currentStock.get_five_day_change()[0] != null && currentStock.get_five_day_change()[0].contains("-"))
        data[11][1] = String.format("<html>%s<font color='red'>%s</font></html>", "5D Change: ",
                currentStock.get_five_day_change()[0] + " (" + currentStock.get_five_day_change()[1] + ")");
    else
        data[11][1] = String.format("<html>%s<font color='green'>%s</font></html>", "5D Change: ",
                currentStock.get_five_day_change()[0] + " (" + currentStock.get_five_day_change()[1] + ")");
    if (currentStock.get_one_month_change()[0] != null && currentStock.get_one_month_change()[0].contains("-"))
        data[12][1] = String.format("<html>%s<font color='red'>%s</font></html>", "1M Change: ",
                currentStock.get_one_month_change()[0] + " (" + currentStock.get_one_month_change()[1] + ")");
    else
        data[12][1] = String.format("<html>%s<font color='green'>%s</font></html>", "1M Change: ",
                currentStock.get_one_month_change()[0] + " (" + currentStock.get_one_month_change()[1] + ")");
    if (currentStock.get_three_month_change()[0] != null
            && currentStock.get_three_month_change()[0].contains("-"))
        data[13][1] = String.format("<html>%s<font color='red'>%s</font></html>", "3M Change: ",
                currentStock.get_three_month_change()[0] + " (" + currentStock.get_three_month_change()[1]
                        + ")");
    else
        data[13][1] = String.format("<html>%s<font color='green'>%s</font></html>", "3M Change: ",
                currentStock.get_three_month_change()[0] + " (" + currentStock.get_three_month_change()[1]
                        + ")");
    if (currentStock.get_six_month_change()[0] != null && currentStock.get_six_month_change()[0].contains("-"))
        data[14][1] = String.format("<html>%s<font color='red'>%s</font></html>", "6M Change: ",
                currentStock.get_six_month_change()[0] + " (" + currentStock.get_six_month_change()[1] + ")");
    else
        data[14][1] = String.format("<html>%s<font color='green'>%s</font></html>", "6M Change: ",
                currentStock.get_six_month_change()[0] + " (" + currentStock.get_six_month_change()[1] + ")");
    if (currentStock.get_ytd_change()[0] != null && currentStock.get_ytd_change()[0].contains("-"))
        data[15][1] = String.format("<html>%s<font color='red'>%s</font></html>", "YTD Change: ",
                currentStock.get_ytd_change()[0] + " (" + currentStock.get_ytd_change()[1] + ")");
    else
        data[15][1] = String.format("<html>%s<font color='green'>%s</font></html>", "YTD Change: ",
                currentStock.get_ytd_change()[0] + " (" + currentStock.get_ytd_change()[1] + ")");
    if (currentStock.get_one_year_change()[0] != null && currentStock.get_one_year_change()[0].contains("-"))
        data[16][1] = String.format("<html>%s<font color='red'>%s</font></html>", "1Y Change: ",
                currentStock.get_one_year_change()[0] + " (" + currentStock.get_one_year_change()[1] + ")");
    else
        data[16][1] = String.format("<html>%s<font color='green'>%s</font></html>", "1Y Change: ",
                currentStock.get_one_year_change()[0] + " (" + currentStock.get_one_year_change()[1] + ")");
    if (currentStock.get_five_year_change()[0] != null && currentStock.get_five_year_change()[0].contains("-"))
        data[17][1] = String.format("<html>%s<font color='red'>%s</font></html>", "5Y Change: ",
                currentStock.get_five_year_change()[0] + " (" + currentStock.get_five_year_change()[1] + ")");
    else
        data[17][1] = String.format("<html>%s<font color='green'>%s</font></html>", "5Y Change: ",
                currentStock.get_five_year_change()[0] + " (" + currentStock.get_five_year_change()[1] + ")");
    if (currentStock.get_ten_year_change()[0] != null && currentStock.get_ten_year_change()[0].contains("-"))
        data[18][1] = String.format("<html>%s<font color='red'>%s</font></html>", "10Y Change: ",
                currentStock.get_ten_year_change()[0] + " (" + currentStock.get_ten_year_change()[1] + ")");
    else
        data[18][1] = String.format("<html>%s<font color='green'>%s</font></html>", "10Y Change: ",
                currentStock.get_ten_year_change()[0] + " (" + currentStock.get_ten_year_change()[1] + ")");
    if (currentStock.get_max_year_change()[0] != null && currentStock.get_max_year_change()[0].contains("-"))
        data[19][1] = String.format("<html>%s<font color='red'>%s</font></html>", "Max Change: ",
                currentStock.get_max_year_change()[0] + " (" + currentStock.get_max_year_change()[1] + ")");
    else
        data[19][1] = String.format("<html>%s<font color='green'>%s</font></html>", "Max Change: ",
                currentStock.get_max_year_change()[0] + " (" + currentStock.get_max_year_change()[1] + ")");

    data[24][0] = "Earnings Est Next Quarter: " + currentStock.get_earnings_estimate_next_quarter();
    data[25][0] = "Earnings Est Current Year: " + currentStock.get_earnings_estimate_current_year();
    data[26][0] = "Earnings Est Next Year: " + currentStock.get_earnings_estimate_next_year();
    data[24][1] = "P/E Est Current Year: " + currentStock.get_price_eps_estimate_current_year();
    data[25][1] = "P/E Est Next Year: " + currentStock.get_price_eps_estimate_next_year();
    remainingInfoTable.setFont(new Font(remainingInfoTable.getName(), Font.PLAIN, 15));
    remainingInfoTable.packColumn(0, 4);
    remainingInfoTable.packColumn(1, 4);

    try {
        dataAndGraph.remove(chartPane);
        chartPane = new JTabbedPane();
        chartPane.setVisible(true);
        ChartPanel intradayChart = new IntradayChart(currentStock).getChartPanel();
        intradayChart.setPopupMenu(graphMenu);
        chartPane.addTab("1d", intradayChart);
        chartPane.setMnemonicAt(0, KeyEvent.VK_1);

        ChartPanel fivedayChart = new FiveDayChart(currentStock).getChartPanel();
        fivedayChart.setPopupMenu(graphMenu);
        chartPane.addTab("5d", fivedayChart);
        chartPane.setMnemonicAt(0, KeyEvent.VK_2);

        ChartPanel onemonthChart = new OneMonthChart(currentStock).getChartPanel();
        onemonthChart.setPopupMenu(graphMenu);
        chartPane.addTab("1m", onemonthChart);
        chartPane.setMnemonicAt(0, KeyEvent.VK_3);

        ChartPanel threemonthChart = new ThreeMonthChart(currentStock).getChartPanel();
        threemonthChart.setPopupMenu(graphMenu);
        chartPane.addTab("3m", threemonthChart);
        chartPane.setMnemonicAt(0, KeyEvent.VK_4);

        ChartPanel sixmonthChart = new SixMonthChart(currentStock).getChartPanel();
        sixmonthChart.setPopupMenu(graphMenu);
        chartPane.addTab("6m", sixmonthChart);
        chartPane.setMnemonicAt(0, KeyEvent.VK_5);

        ChartPanel ytdChart = new YTDChart(currentStock).getChartPanel();
        ytdChart.setPopupMenu(graphMenu);
        chartPane.addTab("ytd", ytdChart);
        chartPane.setMnemonicAt(0, KeyEvent.VK_6);

        ChartPanel oneyearChart = new OneYearChart(currentStock).getChartPanel();
        oneyearChart.setPopupMenu(graphMenu);
        chartPane.addTab("1y", oneyearChart);
        chartPane.setMnemonicAt(0, KeyEvent.VK_7);

        ChartPanel fiveyearChart = new FiveYearChart(currentStock).getChartPanel();
        fiveyearChart.setPopupMenu(graphMenu);
        chartPane.addTab("5y", fiveyearChart);
        chartPane.setMnemonicAt(0, KeyEvent.VK_8);

        ChartPanel tenyearChart = new TenYearChart(currentStock).getChartPanel();
        tenyearChart.setPopupMenu(graphMenu);
        chartPane.addTab("10y", tenyearChart);
        chartPane.setMnemonicAt(0, KeyEvent.VK_9);

        ChartPanel maxChart = new MaxChart(currentStock).getChartPanel();
        maxChart.setPopupMenu(graphMenu);
        chartPane.addTab("max", maxChart);
        chartPane.setMnemonicAt(0, KeyEvent.VK_0);

        c = new GridBagConstraints();
        c.anchor = GridBagConstraints.NORTHWEST;
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1;
        c.weighty = 0;
        c.gridheight = 2;
        //c.ipady = 20;
        c.gridx = 1;
        c.gridy = 0;
        dataAndGraph.add(chartPane, c);

    } catch (ParseException ex) {
        Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
    }
    chartPane.revalidate();
    chartPane.repaint();
    revalidate();
    repaint();
    finished = true;
}

From source file:FocusEventDemo.java

public FocusEventDemo() {
    super(new GridBagLayout());
    GridBagLayout gridbag = (GridBagLayout) getLayout();
    GridBagConstraints c = new GridBagConstraints();

    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1.0; //Make column as wide as possible.
    JTextField textField = new JTextField("A TextField");
    textField.setMargin(new Insets(0, 2, 0, 2));
    textField.addFocusListener(this);
    gridbag.setConstraints(textField, c);
    add(textField);//from   w  ww .j ava  2  s  .  c  om

    c.weightx = 0.1; //Widen every other column a bit, when possible.
    c.fill = GridBagConstraints.NONE;
    JLabel label = new JLabel("A Label");
    label.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
    label.addFocusListener(this);
    gridbag.setConstraints(label, c);
    add(label);

    String comboPrefix = "ComboBox Item #";
    final int numItems = 15;
    Vector vector = new Vector(numItems);
    for (int i = 0; i < numItems; i++) {
        vector.addElement(comboPrefix + i);
    }
    JComboBox comboBox = new JComboBox(vector);
    comboBox.addFocusListener(this);
    gridbag.setConstraints(comboBox, c);
    add(comboBox);

    c.gridwidth = GridBagConstraints.REMAINDER;
    JButton button = new JButton("A Button");
    button.addFocusListener(this);
    gridbag.setConstraints(button, c);
    add(button);

    c.weightx = 0.0;
    c.weighty = 0.1;
    c.fill = GridBagConstraints.BOTH;
    String listPrefix = "List Item #";
    Vector listVector = new Vector(numItems);
    for (int i = 0; i < numItems; i++) {
        listVector.addElement(listPrefix + i);
    }
    JList list = new JList(listVector);
    list.setSelectedIndex(1); //It's easier to see the focus change
    //if an item is selected.
    list.addFocusListener(this);
    JScrollPane listScrollPane = new JScrollPane(list);
    //We want to prevent the list's scroll bars
    //from getting the focus - even with the keyboard.
    //Note that in general we prefer setRequestFocusable
    //over setFocusable for reasons of accessibility,
    //but this is to work around bug #4866958.
    listScrollPane.getVerticalScrollBar().setFocusable(false);
    listScrollPane.getHorizontalScrollBar().setFocusable(false);
    gridbag.setConstraints(listScrollPane, c);
    add(listScrollPane);

    c.weighty = 1.0; //Make this row as tall as possible.
    c.gridheight = GridBagConstraints.REMAINDER;
    //Set up the area that reports focus-gained and focus-lost events.
    display = new JTextArea();
    display.setEditable(false);
    //The method setRequestFocusEnabled prevents a
    //component from being clickable, but it can still
    //get the focus through the keyboard - this ensures
    //user accessibility.
    display.setRequestFocusEnabled(false);
    display.addFocusListener(this);
    JScrollPane displayScrollPane = new JScrollPane(display);

    //Work around for bug #4866958.
    displayScrollPane.getHorizontalScrollBar().setFocusable(false);
    displayScrollPane.getVerticalScrollBar().setFocusable(false);
    gridbag.setConstraints(displayScrollPane, c);
    add(displayScrollPane);

    setPreferredSize(new Dimension(450, 450));
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}