Results 1 to 3 of 3
Thread: Bold Italic function in Swing
- 12-17-2008, 06:06 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 32
- Rep Power
- 0
-
Please describe in more detail just what you mean by a "Bold and Italic function".
- 02-19-2009, 04:08 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 2
- Rep Power
- 0
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
-
function
By nanna in forum New To JavaReplies: 1Last Post: 11-17-2008, 10:20 PM -
Need help with get function
By calicocal in forum New To JavaReplies: 10Last Post: 11-09-2008, 08:59 PM -
Need a little help with a function!
By Nuluvius in forum New To JavaReplies: 3Last Post: 02-08-2008, 12:33 AM -
Returning a string (Partly Bold)
By TampaTechGuy in forum New To JavaReplies: 3Last Post: 02-03-2008, 10:54 AM -
function name
By osval in forum Advanced JavaReplies: 1Last Post: 08-06-2007, 09:56 PM
Bookmarks