Example usage for java.awt GridBagConstraints WEST

List of usage examples for java.awt GridBagConstraints WEST

Introduction

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

Prototype

int WEST

To view the source code for java.awt GridBagConstraints WEST.

Click Source Link

Document

Put the component on the left side of its display area, centered vertically.

Usage

From source file:pcgen.gui2.prefs.LookAndFeelPanel.java

/**
 * Instantiates a new look and feel panel.
 *///ww w . j av a2s  . c om
public LookAndFeelPanel(Dialog parent) {

    JLabel label;
    ButtonGroup exclusiveGroup;
    Border etched = null;
    TitledBorder title1 = BorderFactory.createTitledBorder(etched, IN_LOOK_AND_FEEL);

    title1.setTitleJustification(TitledBorder.LEFT);
    this.setBorder(title1);
    GridBagLayout gridbag = new GridBagLayout();
    this.setLayout(gridbag);
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.anchor = GridBagConstraints.WEST;
    c.insets = new Insets(2, 2, 2, 2);

    exclusiveGroup = new ButtonGroup();
    Action[] actions = LookAndFeelManager.getActions();
    laf = new JRadioButton[actions.length - 1];

    for (int i = 0; i < laf.length; ++i) {
        laf[i] = new JRadioButton(actions[i]);

        int whichChar = (laf[i].getText().charAt(0) == 'C') ? 1 : 0;
        laf[i].setMnemonic(laf[i].getText().charAt(whichChar));

        Utility.buildConstraints(c, 0, i, 3, 1, 0, 0);
        gridbag.setConstraints(laf[i], c);
        this.add(laf[i]);
        exclusiveGroup.add(laf[i]);
    }

    skinnedLookFeel.addActionListener(actions[actions.length - 1]);
    skinnedLookFeel.setText(IN_SKINNED_LAF + ": ");
    skinnedLookFeel.setToolTipText(LanguageBundle.getString("in_Prefs_skinnedLAFTooltip"));
    skinnedLookFeel.setMnemonic(LanguageBundle.getMnemonic("in_mn_Prefs_skinnedLAF"));
    Utility.buildConstraints(c, 0, laf.length, 3, 1, 0, 0);
    gridbag.setConstraints(skinnedLookFeel, c);
    this.add(skinnedLookFeel);
    exclusiveGroup.add(skinnedLookFeel);

    Utility.buildConstraints(c, 3, laf.length, 1, 1, 1, 0);
    themepackLabel = new JTextField(LookAndFeelManager.getCurrentThemePack());
    themepackLabel.setEditable(false);
    gridbag.setConstraints(themepackLabel, c);
    this.add(themepackLabel);
    Utility.buildConstraints(c, 4, laf.length, 1, 1, 0, 0);
    themepack = new JButton(IN_CHOOSE);
    themepack.setToolTipText(LanguageBundle.getString("in_Prefs_chooseSkinTooltip"));
    gridbag.setConstraints(themepack, c);
    this.add(themepack);
    themepack.addActionListener(prefsButtonHandler);

    Utility.buildConstraints(c, 0, laf.length + 1, 5, 1, 0, 0);
    label = new JLabel("");
    gridbag.setConstraints(label, c);
    this.add(label);

    Utility.buildConstraints(c, 0, laf.length + 2, 5, 1, 0, 0);
    label = new JLabel(LanguageBundle.getString("in_Prefs_restartInfo"));
    gridbag.setConstraints(label, c);
    this.add(label);

    Utility.buildConstraints(c, 0, 20, 5, 1, 1, 1);
    c.fill = GridBagConstraints.BOTH;
    label = new JLabel(" ");
    gridbag.setConstraints(label, c);
    this.add(label);
}

From source file:width.java

 public void init() {
     GridBagLayout gridBag = new GridBagLayout();
     GridBagConstraints c = new GridBagConstraints();

     setLayout(gridBag);/* www  . j  a  v  a  2s  .com*/

     Label receiverLabel = new Label("Receiver name:", 
                                     Label.RIGHT);
     gridBag.setConstraints(receiverLabel, c);
     add(receiverLabel);

     nameField = new TextField(getParameter("RECEIVERNAME"),
                                            10);
     c.fill = GridBagConstraints.HORIZONTAL;
     gridBag.setConstraints(nameField, c);
     add(nameField);
     nameField.addActionListener(this);

     Button button = new Button("Send message");
     c.gridwidth = GridBagConstraints.REMAINDER; //end row
     c.anchor = GridBagConstraints.WEST; //stick to the 
                                         //text field
     c.fill = GridBagConstraints.NONE; //keep the button 
                                       //small
     gridBag.setConstraints(button, c);
     add(button);
     button.addActionListener(this);

     status = new TextArea(5, 60);
     status.setEditable(false);
     c.anchor = GridBagConstraints.CENTER; //reset to the default
     c.fill = GridBagConstraints.BOTH; //make this big
     c.weightx = 1.0;
     c.weighty = 1.0;
     gridBag.setConstraints(status, c);
     add(status);

     myName = getParameter("NAME");
     Label senderLabel = new Label("(My name is " + myName + ".)",
                                   Label.CENTER);
     c.weightx = 0.0;
     c.weighty = 0.0;
     gridBag.setConstraints(senderLabel, c);
     add(senderLabel);

newline = System.getProperty("line.separator");
 }

From source file:cool.pandora.modeller.ui.jpanel.base.NewBagInPlaceFrame.java

/**
 * layoutSpacer.//from w w w . ja  v a  2 s .  c  o m
 *
 * @param contentPanel JPanel
 * @param row          int
 */
private static void layoutSpacer(final JPanel contentPanel, final int row) {
    GridBagConstraints glbc = new GridBagConstraints();
    glbc = LayoutUtil.buildGridBagConstraints(0, row, 1, 1, 1, 50, GridBagConstraints.NONE,
            GridBagConstraints.WEST);
    final JLabel spacerLabel = new JLabel("");
    contentPanel.add(spacerLabel, glbc);
}

From source file:SimpleAuthenticator.java

protected PasswordAuthentication getPasswordAuthentication() {

    // given a prompt?
    String prompt = getRequestingPrompt();
    if (prompt == null)
        prompt = "Please login...";

    // protocol/*from   w  w w  .  j  av  a  2 s  .  c o m*/
    String protocol = getRequestingProtocol();
    if (protocol == null)
        protocol = "Unknown protocol";

    // get the host
    String host = null;
    InetAddress inet = getRequestingSite();
    if (inet != null)
        host = inet.getHostName();
    if (host == null)
        host = "Unknown host";

    // port
    String port = "";
    int portnum = getRequestingPort();
    if (portnum != -1)
        port = ", port " + portnum + " ";

    // Build the info string
    String info = "Connecting to " + protocol + " mail service on host " + host + port;

    //JPanel d = new JPanel();
    // XXX - for some reason using a JPanel here causes JOptionPane
    // to display incorrectly, so we workaround the problem using
    // an anonymous JComponent.
    JComponent d = new JComponent() {
    };

    GridBagLayout gb = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    d.setLayout(gb);
    c.insets = new Insets(2, 2, 2, 2);

    c.anchor = GridBagConstraints.WEST;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.weightx = 0.0;
    d.add(constrain(new JLabel(info), gb, c));
    d.add(constrain(new JLabel(prompt), gb, c));

    c.gridwidth = 1;
    c.anchor = GridBagConstraints.EAST;
    c.fill = GridBagConstraints.NONE;
    c.weightx = 0.0;
    d.add(constrain(new JLabel("Username:"), gb, c));

    c.anchor = GridBagConstraints.EAST;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.weightx = 1.0;
    String user = getDefaultUserName();
    JTextField username = new JTextField(user, 20);
    d.add(constrain(username, gb, c));

    c.gridwidth = 1;
    c.fill = GridBagConstraints.NONE;
    c.anchor = GridBagConstraints.EAST;
    c.weightx = 0.0;
    d.add(constrain(new JLabel("Password:"), gb, c));

    c.anchor = GridBagConstraints.EAST;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.weightx = 1.0;
    JPasswordField password = new JPasswordField("", 20);
    d.add(constrain(password, gb, c));
    // XXX - following doesn't work
    if (user != null && user.length() > 0)
        password.requestFocus();
    else
        username.requestFocus();

    int result = JOptionPane.showConfirmDialog(frame, d, "Login", JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE);

    if (result == JOptionPane.OK_OPTION)
        return new PasswordAuthentication(username.getText(), password.getText());
    else
        return null;
}

From source file:Demo.ScatterGraph.java

public ScatterGraph(int sampleNb, List<String> paraType_list) {
    this.sampleNb = sampleNb;
    this.paraType_list = paraType_list;

    // widgets: charts(JFreeChart), parameter selectors (JList)
    DefaultListModel xModel = new DefaultListModel();
    xSelector = new JList(xModel);
    JScrollPane xSelPane = new JScrollPane();
    xSelPane.setViewportView(xSelector);

    DefaultListModel yModel = new DefaultListModel();
    ySelector = new JList(yModel);
    JScrollPane ySelPane = new JScrollPane();
    ySelPane.setViewportView(ySelector);

    charts = new JFreeChart[paraType_list.size()][paraType_list.size()];
    for (int i = 0; i < paraType_list.size(); i++) {
        String para = paraType_list.get(i);
        xModel.addElement(para);/*ww w  .j  a  v  a  2 s .co m*/
        yModel.addElement(para);

        for (int j = 0; j < paraType_list.size(); j++) {
            charts[i][j] = null;
        }
    }

    charts[0][0] = CreateChart(0, 0);
    chartPane = new ChartPanel(charts[0][0]);

    xSelector.setSelectedIndex(0);
    ySelector.setSelectedIndex(0);

    xSelector.addMouseListener(this);
    ySelector.addMouseListener(this);

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

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

    gbc.insets = new Insets(5, 20, 5, 5);
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.weightx = 20;
    gbc.weighty = 12;
    gbc.gridheight = 4;
    add(chartPane, gbc);

    gbc.insets = new Insets(5, 5, 5, 20);
    gbc.gridx = 1;
    gbc.weightx = 1;
    gbc.gridheight = 1;

    gbc.gridy = 0;
    gbc.weighty = 1;
    add(new JLabel("X :"), gbc);
    gbc.gridy = 2;
    add(new JLabel("Y :"), gbc);

    gbc.gridy = 1;
    gbc.weighty = 5;
    add(xSelPane, gbc);
    gbc.gridy = 3;
    add(ySelPane, gbc);
}

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;//from  w w  w. j a va 2  s. 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:de.codesourcery.jasm16.ide.ui.views.EmulationOptionsView.java

public EmulationOptionsView() {
    emulatorPanel.setLayout(new GridBagLayout());

    GridBagConstraints cnstrs = constraints(0, 0, false, false, GridBagConstraints.NONE);
    emulatorPanel.add(new JLabel("Emulation speed"), cnstrs);

    cnstrs = constraints(1, 0, true, true, GridBagConstraints.NONE);
    cnstrs.anchor = GridBagConstraints.WEST;
    emulatorPanel.setBorder(BorderFactory.createTitledBorder("General options"));

    speedBox.setRenderer(new DefaultListCellRenderer() {

        public Component getListCellRendererComponent(javax.swing.JList<?> list, Object value, int index,
                boolean isSelected, boolean cellHasFocus) {
            final java.awt.Component result = super.getListCellRendererComponent(list, value, index, isSelected,
                    cellHasFocus);/*from w  w  w  .j ava  2  s . c o m*/

            if (value != null) {
                switch ((EmulationSpeed) value) {
                case MAX_SPEED:
                    setText("Max.");
                    break;
                case REAL_SPEED:
                    setText("100 kHz");
                    break;
                default:
                    setText(value.toString());
                    break;
                }
            }
            return result;
        };
    });

    emulatorPanel.add(speedBox, cnstrs);

    // disk drive panel
    selectedFileField.setColumns(25);

    diskDrivePanel.setLayout(new GridBagLayout());
    cnstrs = constraints(0, 0, false, true, GridBagConstraints.NONE);
    cnstrs.anchor = GridBagConstraints.CENTER;

    diskDrivePanel.setBorder(BorderFactory.createTitledBorder("Disk drive"));
    diskDrivePanel.add(selectedFileField, cnstrs);

    cnstrs = constraints(1, 0, false, true, GridBagConstraints.NONE);
    cnstrs.anchor = GridBagConstraints.CENTER;
    diskDrivePanel.add(fileChooserButton, cnstrs);

    fileChooserButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            final JFileChooser chooser;
            if (getSelectedFile() != null) {
                chooser = new JFileChooser(getSelectedFile().getParentFile());
            } else {
                chooser = new JFileChooser();
            }
            final int result = chooser.showOpenDialog(null);
            if (result == JFileChooser.APPROVE_OPTION && chooser.getSelectedFile().isFile()) {
                selectedFileField.setText(chooser.getSelectedFile().getAbsolutePath());
            }
        }
    });

    cnstrs = constraints(2, 0, false, true, GridBagConstraints.NONE);
    cnstrs.anchor = GridBagConstraints.CENTER;
    diskDrivePanel.add(writeProtected, cnstrs);
}

From source file:org.zaproxy.zap.view.MainToolbarPanel.java

public void initialise() {
    setLayout(new java.awt.GridBagLayout());
    setPreferredSize(DisplayUtils.getScaledDimension(getMaximumSize().width, 25));
    setMaximumSize(DisplayUtils.getScaledDimension(getMaximumSize().width, 25));
    this.setBorder(BorderFactory.createEtchedBorder());

    expandButtons = new ButtonGroup();

    GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
    GridBagConstraints gridBagConstraints2 = new GridBagConstraints();

    gridBagConstraints1.gridx = 0;/* w w w.  j  av a 2  s  .  c  o  m*/
    gridBagConstraints1.gridy = 0;
    gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;

    gridBagConstraints2.gridx = 1;
    gridBagConstraints2.gridy = 0;
    gridBagConstraints2.weightx = 1.0;
    gridBagConstraints2.weighty = 1.0;
    gridBagConstraints2.anchor = java.awt.GridBagConstraints.EAST;
    gridBagConstraints2.fill = java.awt.GridBagConstraints.HORIZONTAL;

    JToolBar t1 = new JToolBar();
    t1.setEnabled(true);
    t1.setPreferredSize(new java.awt.Dimension(80000, 25));
    t1.setMaximumSize(new java.awt.Dimension(80000, 25));

    add(getToolbar(), gridBagConstraints1);
    add(t1, gridBagConstraints2);

    toolbar.add(getModeSelect());
    toolbar.add(getBtnNew());
    toolbar.add(getBtnOpen());
    toolbar.add(getBtnSave());
    toolbar.add(getBtnSnapshot());
    toolbar.add(getBtnSession());
    toolbar.add(getBtnOptions());

    toolbar.addSeparator();
    toolbar.add(getShowAllTabs());
    toolbar.add(getHideAllTabs());
    toolbar.add(getShowTabIconNames());
    toolbar.addSeparator();

    toolbar.add(getBtnExpandSites());
    toolbar.add(getBtnExpandReports());
    toolbar.add(getBtnExpandFull());

    toolbar.addSeparator();
}

From source file:EditorPaneExample12.java

public EditorPaneExample12() {
    super("JEditorPane Example 12");

    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;//from   w w  w .  j  av a2s .  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();
            loadNewPage(selection);
        }
    });

    // 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();
                loadingPage = false;
            }
        }
    });

    // 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) {
                    }
                }
            }
        }
    });

    // Listener for hypertext events
    pane.addHyperlinkListener(new HyperlinkListener() {
        public void hyperlinkUpdate(HyperlinkEvent evt) {
            // Ignore hyperlink events if the frame is busy
            if (loadingPage == true) {
                return;
            }
            if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                JEditorPane sp = (JEditorPane) evt.getSource();
                if (evt instanceof HTMLFrameHyperlinkEvent) {
                    HTMLDocument doc = (HTMLDocument) sp.getDocument();
                    doc.processHTMLFrameHyperlinkEvent((HTMLFrameHyperlinkEvent) evt);
                } else {
                    loadNewPage(evt.getURL());
                }
            } else if (evt.getEventType() == HyperlinkEvent.EventType.ENTERED) {
                pane.setCursor(handCursor);
            } else if (evt.getEventType() == HyperlinkEvent.EventType.EXITED) {
                pane.setCursor(defaultCursor);
            }
        }
    });
}

From source file:EditorPaneExample16.java

public EditorPaneExample16() {
    super("JEditorPane Example 16");

    pane = new JEditorPane();
    pane.setEditable(true); // Editable
    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 va 2s  .  c  om*/
    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.gridy = 5;
    c.gridwidth = 2;
    c.weightx = 1.0;
    c.anchor = GridBagConstraints.WEST;
    editableBox = new JCheckBox("Editable JEditorPane");
    panel.add(editableBox, c);
    editableBox.setSelected(true);
    editableBox.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();
            loadNewPage(selection);
        }
    });

    // Change editability based on the checkbox
    editableBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            pane.setEditable(editableBox.isSelected());
            pane.revalidate();
            pane.repaint();
        }
    });

    // 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();
                loadingPage = false;
            }
        }
    });

    // 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) {
                    }
                }
            }
        }
    });

    // Listener for hypertext events
    pane.addHyperlinkListener(new HyperlinkListener() {
        public void hyperlinkUpdate(HyperlinkEvent evt) {
            // Ignore hyperlink events if the frame is busy
            if (loadingPage == true) {
                return;
            }
            if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                JEditorPane sp = (JEditorPane) evt.getSource();
                if (evt instanceof HTMLFrameHyperlinkEvent) {
                    HTMLDocument doc = (HTMLDocument) sp.getDocument();
                    doc.processHTMLFrameHyperlinkEvent((HTMLFrameHyperlinkEvent) evt);
                } else {
                    loadNewPage(evt.getURL());
                }
            } else if (evt.getEventType() == HyperlinkEvent.EventType.ENTERED) {
                pane.setCursor(handCursor);
            } else if (evt.getEventType() == HyperlinkEvent.EventType.EXITED) {
                pane.setCursor(defaultCursor);
            }
        }
    });
}