Results 1 to 3 of 3
  1. #1
    Java.child is offline Member
    Join Date
    Sep 2008
    Posts
    32
    Rep Power
    0

    Default Bold Italic function in Swing

    Hello Experts,
    How to write Bold and Italic function in Swing.
    What Libraries I need to import for that.
    Please help me.

    Thanks.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Please describe in more detail just what you mean by a "Bold and Italic function".

  3. #3
    manju_reddys is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default

    Copy past below code into Netbeans 6.5 and run, analyze code you'll get to know.

    import javax.swing.text.Element;
    import javax.swing.text.MutableAttributeSet;
    import javax.swing.text.SimpleAttributeSet;
    import javax.swing.text.StyleConstants;
    import javax.swing.text.StyledDocument;

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /*
    * NewJFrame.java
    *
    * Created on Feb 19, 2009, 1:28:32 PM
    */

    /**
    *
    * @author GangareddyM
    */
    public class NewJFrame extends javax.swing.JFrame {

    /** Creates new form NewJFrame */
    public NewJFrame() {
    initComponents();

    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    details = new javax.swing.JDialog();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    location = new javax.swing.JLabel();
    txtstyle = new javax.swing.JLabel();
    jScrollPane2 = new javax.swing.JScrollPane();
    pane1 = new javax.swing.JTextPane();
    copybutton = new javax.swing.JButton();
    boldbtn = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    pane2 = new javax.swing.JTextPane();

    jLabel1.setLabelFor(location);
    jLabel1.setText(" Location:");

    jLabel2.setLabelFor(txtstyle);
    jLabel2.setText("Text Style:");

    location.setText("jLabel3");

    txtstyle.setText("jLabel3");

    org.jdesktop.layout.GroupLayout detailsLayout = new org.jdesktop.layout.GroupLayout(details.getContent Pane());
    details.getContentPane().setLayout(detailsLayout);
    detailsLayout.setHorizontalGroup(
    detailsLayout.createParallelGroup(org.jdesktop.lay out.GroupLayout.LEADING)
    .add(detailsLayout.createSequentialGroup()
    .addContainerGap()
    .add(detailsLayout.createParallelGroup(org.jdeskto p.layout.GroupLayout.TRAILING)
    .add(jLabel2)
    .add(jLabel1))
    .addPreferredGap(org.jdesktop.layout.LayoutStyle.R ELATED)
    .add(detailsLayout.createParallelGroup(org.jdeskto p.layout.GroupLayout.LEADING)
    .add(location)
    .add(txtstyle))
    .addContainerGap(73, Short.MAX_VALUE))
    );
    detailsLayout.setVerticalGroup(
    detailsLayout.createParallelGroup(org.jdesktop.lay out.GroupLayout.LEADING)
    .add(detailsLayout.createSequentialGroup()
    .addContainerGap()
    .add(detailsLayout.createParallelGroup(org.jdeskto p.layout.GroupLayout.BASELINE)
    .add(jLabel1)
    .add(location))
    .addPreferredGap(org.jdesktop.layout.LayoutStyle.R ELATED)
    .add(detailsLayout.createParallelGroup(org.jdeskto p.layout.GroupLayout.BASELINE)
    .add(jLabel2)
    .add(txtstyle))
    .addContainerGap(org.jdesktop.layout.GroupLayout.D EFAULT_SIZE, Short.MAX_VALUE))
    );

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    pane1.setEditable(false);
    jScrollPane2.setViewportView(pane1);

    copybutton.setText("Copy");
    copybutton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    copybuttonActionPerformed(evt);
    }
    });

    boldbtn.setText("Bold");
    boldbtn.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    boldbtnActionPerformed(evt);
    }
    });

    pane2.addCaretListener(new javax.swing.event.CaretListener() {
    public void caretUpdate(javax.swing.event.CaretEvent evt) {
    pane2CaretUpdate(evt);
    }
    });
    jScrollPane1.setViewportView(pane2);

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
    .add(layout.createSequentialGroup()
    .addContainerGap()
    .add(layout.createParallelGroup(org.jdesktop.layou t.GroupLayout.LEADING)
    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE)
    .add(layout.createSequentialGroup()
    .add(boldbtn)
    .add(18, 18, 18)
    .add(copybutton)
    .add(126, 126, 126))
    .add(layout.createSequentialGroup()
    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE)
    .addContainerGap())))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
    .add(layout.createSequentialGroup()
    .addContainerGap()
    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 77, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(org.jdesktop.layout.LayoutStyle.R ELATED)
    .add(layout.createParallelGroup(org.jdesktop.layou t.GroupLayout.BASELINE)
    .add(copybutton)
    .add(boldbtn))
    .addPreferredGap(org.jdesktop.layout.LayoutStyle.R ELATED)
    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
    .addContainerGap())
    );

    pack();
    }// </editor-fold>

    private void boldbtnActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    StyledDocument doc = pane2.getStyledDocument();
    MutableAttributeSet attributeSet = new SimpleAttributeSet();
    StyleConstants.setBold(attributeSet, true);
    int offset = pane2.getSelectionStart();
    int end = pane2.getSelectionEnd();
    int length = end - offset;
    doc.setCharacterAttributes(offset, length, attributeSet, false);

    }

    private void copybuttonActionPerformed(java.awt.event.ActionEve nt evt) {
    // TODO add your handling code here:
    pane1.setText(pane2.getSelectedText());
    }

    private void pane2CaretUpdate(javax.swing.event.CaretEvent evt) {
    // TODO add your handling code here:

    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new NewJFrame().setVisible(true);
    }
    });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton boldbtn;
    private javax.swing.JButton copybutton;
    private javax.swing.JDialog details;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel location;
    private javax.swing.JTextPane pane1;
    private javax.swing.JTextPane pane2;
    private javax.swing.JLabel txtstyle;
    // End of variables declaration

    }

Similar Threads

  1. function
    By nanna in forum New To Java
    Replies: 1
    Last Post: 11-17-2008, 09:20 PM
  2. Need help with get function
    By calicocal in forum New To Java
    Replies: 10
    Last Post: 11-09-2008, 07:59 PM
  3. Need a little help with a function!
    By Nuluvius in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 11:33 PM
  4. Returning a string (Partly Bold)
    By TampaTechGuy in forum New To Java
    Replies: 3
    Last Post: 02-03-2008, 09:54 AM
  5. function name
    By osval in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 08:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •