मेरे पास एक JTextArea है जिसके लिए मैंने वर्ड-रैप और रैप-स्टाइल-शब्द को सत्य पर सेट किया है। मैं JTextArea को निर्दिष्ट चौड़ाई के अनुसार न्यूनतम संभव ऊंचाई पर "पैक" करना चाहता हूं।रैपिंग के कारण जेटीक्स्टएरिया में लाइनों की संख्या को कैसे गिनना है?
ऐसा करने के लिए, मैं का उपयोग कर फॉन्ट की ऊंचाई की गणना के लिए योजना बना रहा हूँ ...
Font font = jTextArea.getFont();
FontMetrics fontMetrics = jTextArea.getFontMetrics(font);
int lineHeight = fontMetrics.getAscent() + fontMetrics.getDescent();
... और फिर JTextArea में इस्तेमाल किया लाइनों की संख्या से गुणा। समस्या यह है कि JTextArea.getLineCount() लिपटे लाइनों को अनदेखा करते हुए लाइन रिटर्न की संख्या की गणना करता है।
मैं JTextArea में उपयोग की जाने वाली लाइनों की संख्या को कैसे गिन सकता हूं, जिनमें शब्द रैप के कारण होते हैं?
इस समस्या को आसान बनाने के लिए यहां कुछ डेमो कोड दिया गया है। मेरे पास एक श्रोता है जो खिड़की का आकार बदलने पर प्रत्येक बार लाइनों की संख्या को प्रिंट करता है। फिलहाल, यह हमेशा 1 प्रिंट करता है, लेकिन मैं शब्द लपेटने के लिए क्षतिपूर्ति करना चाहता हूं और प्रिंट करना चाहता हूं कि वास्तव में कितनी लाइनों का उपयोग किया जा रहा है।
संपादित करें: मैंने नीचे दिए गए कोड में समस्या का समाधान शामिल किया है। स्थैतिक गिनती विधि समाधान प्रदान करती है।
package components;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.text.*;
import javax.swing.*;
public class JTextAreaLineCountDemo extends JPanel {
JTextArea textArea;
public JTextAreaLineCountDemo() {
super(new GridBagLayout());
String inputStr = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmo";
textArea = new JTextArea(inputStr);
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
// Add Components to this panel.
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add(textArea, c);
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent ce) {
System.out.println("Line count: " + countLines(textArea));
}
});
}
private static int countLines(JTextArea textArea) {
AttributedString text = new AttributedString(textArea.getText());
FontRenderContext frc = textArea.getFontMetrics(textArea.getFont())
.getFontRenderContext();
AttributedCharacterIterator charIt = text.getIterator();
LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(charIt, frc);
float formatWidth = (float) textArea.getSize().width;
lineMeasurer.setPosition(charIt.getBeginIndex());
int noLines = 0;
while (lineMeasurer.getPosition() < charIt.getEndIndex()) {
lineMeasurer.nextLayout(formatWidth);
noLines++;
}
return noLines;
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("JTextAreaLineCountDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JTextAreaLineCountDemo());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
भी [कैसे पंक्तियों ... एक JTextArea में की संख्या की गणना करने के लिए?] देखें (http://stackoverflow.com/questions/5979795)। – trashgod
इस समाधान में, यदि पाठ खाली है तो त्रुटि फेंक जाएगी: 'java.lang.IllegalArgumentException: पाठ में कम से कम एक वर्ण होना चाहिए' मैं _try catch_ 'new LineBreakMeasurer (charIt, frc) ' – CarlosRos
के साथ घूमने का सुझाव देता हूं, यह विधि '\ n' वर्ण को नई पंक्ति के रूप में नहीं लेती है। – CarlosRos