Updating value in jTextPane - java

I have a jTextPane set up and a panel with radioButtons. My jTextPane displays the contents of a text file I have chosen. The 3rd line, 4th index, displayed in my jTextPane specifies a value of type int. When I click my radioButton, I would like that value to increase by 1. So far, I have the following code. I tried to pane.setText(value + 1), but that doesn't seem to work. How do I update the value and display the updated value in jTextPane? My code,
private final ActionListener actionListen = new ActionListener() {
for(String line: pane.getText().split("\\n")){
String lineThree = line[3];
int value = lineThree.charAt(4);
if(radioButton.isSelected()){
pane.setText(value+1);
}
}
};

Related

How to set an array in Jtextfield using netbeans

I am creating a small application . In this application I have 5 words. I have one JtextField and JButton.The name of JtextField is set as name1 and JButton name set as next.
I want to set a default word in name1 from 1st word of string array ( from this code I am giving one example is need to see in JtextField is as "me") when application run I want to see the word from String array before the the next button click. how I can set the data to name1?
code is as follows:
String s[]={"me","and","my","friends","are"};
int i=0;
private void nextActionPerformed(java.awt.event.ActionEvent evt) {
if(i>=s.length)
i=0;
name1.setText(s[i]);
i++;
}
Call the method after you created your fields with null as argument as you don't use the argument.
nextActionPerformed(null);
This will simulate that the button is pressed when the program starts up.
Assuming you have a button which has action Listener attached. If not then it should be something like below:
nextButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
nextActionPerformed(e);
// Or move your code here from nextActionPerformed(e)
}
});
//And you can also initialize your text filed with first value of array:
textField.setText(s[0]);

JLabels in JPanel, on runtime new label contains previous labels

The problem is that on runtime when new label is created it displays on the JPanel but containing the label created before.
The code converts text to bits offprint, like "HI" converts to
but when another text is converted like "OK" the bits label shows both "HI" and "OK"
This is the code from the MouseHandler class in mouseClicked method
//Convert button is clicked.
if(event.getSource().equals(getButton1Tab2()))
{
//convert text to image.
TextOverlay textOverlay = new TextOverlay(getTextArea1Tab2().getText());
//save image bits in ArrayList.
for(int i=0; i<textOverlay.imageBits.length;i++)
{
//add new line after printing each line of bits (bit line length = image width)
if(i!=0 && (i%Control.valves==0)){setBitsString(getBitsString().append("<br />"));}
//add bit to ArrayList
setBitsString(getBitsString().append(textOverlay.imageBits[i]));
}
//add new label to ArrayList of labels, the new label is bits offprint of the text's image.
labelsArray.add(new JLabel("<html>"+getBitsString()+"</html>"));
labelsArray.get(labelsArray.size()-1).addMouseListener(this);
//show binary equivalent on screen
panel2Tab2.add(labelsArray.get(labelsArray.size()-1));
panel2Tab2.validate();
panel2Tab2.repaint();
}
Thanks,
Whatever getBitsString() returns, you always append to it, but you never clear it. You should clear it first when you want to change its content.
It is because of this line:
if(i!=0 && (i%Control.valves==0)){setBitsString(getBitsString().append("<br />")
You append the new bit string and then you add the result of getBitsString() to your label.

How to identify if the selected text in JTextPane are of different sizes?

I am developing a editor where I have a combobox for font size. User can use it to alter the text size.
I have also implemented the caret listener which tells me the size of selected text and updates the combobox accordingly. Now if the user selects the text of two different sizes I want to populate blank value in Combobox. But I am unable to do that with Caret listener as It gives me size of the first text.
Eg: If my text is "HI". Here H is of size 12 and I is of size 22. Now when I select "HI" caret listener gives me the value of either 12 or 22.
Here is the sample code:
StyledDocument doc = pane.getStyledDocument();
MutableAttributeSet fontSizeStyle = ((StyledEditorKit)pane.getEditorKit()).getInputAttributes();
int fontSize = StyleConstants.getFontSize(fontSizeStyle);
Below code would loop through the selected text in Jtextpane and get each character's size.
String selectedText = pane.getSelectedText();
int k = pane.getSelectionStart();
for(int i=0; i< selectedText.length(); i++) {
AttributeSet fontSize = doc.getCharacterElement(k).getAttributes();
System.out.println("fontSize:"+StyleConstants.getFontSize(fontSize));
k++;
}

Set the Horizontal Position of a cursor on JTextField

I have a Swing Application where a user has to Input Some information. I need the cursor By default to be at Position 10 of the JtextField: I have tried these Two Methods but none of them has worked for me:
JTextField text = new JTextField(" ", 50);
text.setHorizontalAlignment(10)
The other one I have tried is
JTextField text = new JTextField(" ", 50);
text.setCaretPosition(10)
Is there really a way to do what am trying?
Try this:
text.getCaret().setDot(10);
Doesn't the problem come from your JTextField containing an empty String ?
If you want the cursor to be at a set position, this position should be reachable, i.e having a String containing 10 blank spaces.
PS : I think setCaretPosition is the right method here.

How to print to a J text box the entire contents of a tree map?

Hi Everyone thanks for taking the time to look at my question.
I would like to use the JText field I have created to display the values of a tree map which contains all the employees: ID numbers (as the key in the map)as well as an Employee object which contains a to string method of all the employee details.
the system seems to be working fine because when I print to the console (CMD) it works fine and prints out all the values in the MAP but when I try print it to a JText box it only prints one object (one employee) from the entire list.
I believe the issue lies with my for loop i am using to access all the details.
the issue lies with this line of code:
writeStrings.setText(writeStrings.getText()+" "+dEmp);
This is the code in its entirety:
public void chooseEmpToAdd()
{
JFrame frameAllEmps = new JFrame();
frameAllEmps.setSize( 450, 140 );
frameAllEmps.pack();
frameAllEmps.setVisible(true);
int x = 0;
System.out.println("ALL Emps from the tree map");
for(int key:employeeMap.keySet())
{
JTextField writeStrings;
writeStrings = new JTextField(20);
Employee dEmp = employeeMap.get(key);
System.out.println("Employe no :" +x+": "+dEmp);
writeStrings.setText(writeStrings.getText()+" "+dEmp);
frameAllEmps.add(writeStrings);
x++;
}
}
writeStrings = new JTextField(20);
You create new JTextField component on every iteration and add it to container.
JFrame uses BorderLayout as a default layout.
This layout puts your JTextField component in the center (frameAllEmps.add(writeStrings)). So you lost previous added JTextField and see only last JTextField component.

Categories

Resources