• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

constructors

 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my main class implements ActionListener. in the constructor i have the following code:

fontType is a JComboBox. in actionPerformed() i have this code:

when the frame is displayed "Times New Roman" is the selected item, but the font type is not"Times New Roman"
if i open the combo box and click "Times New Roman", then the font changes to "Times New Roman"
it is my understanding that setSelectedItem() fires an action event.
is that correct?
is the problem that i am calling it from the constructor?
any ideas on how to solve this?
 
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

it is my understanding that setSelectedItem() fires an action event.



Add a System.out.println(...) statement to your ActionListener to see if it gets executed.

That is why you create a SSCCE. To test your assumptions. If it works the way you expect then great. If not then you have code do post on the forum

What part about posting a SSCCE do you not understand???
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, this seems pretty strange to me. actionPerformed() IS getting called from the constructor but it doesn't change the font. it gets called again when i click on "Times New Roman" and then it changes the font.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

well, this seems pretty strange to me. actionPerformed() IS getting called from the constructor but it doesn't change the font



Makes sense to me.

So where is your SSCCE? We can't tell what you are doing based on 5 lines of code, since the problem isn't with the code you posted, but the code you didn't post.
 
Ranch Hand
Posts: 62
5
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randall Twede wrote:when the frame is displayed "Times New Roman" is the selected item, but the font type is not"Times New Roman"
if i open the combo box and click "Times New Roman", then the font changes to "Times New Roman"
it is my understanding that setSelectedItem() fires an action event.
is that correct?
is the problem that i am calling it from the constructor?
any ideas on how to solve this?


The following is a small example, developed by me few minutes ago, for a minimal but effective "styled" editor where the user can change font name/size. Try to select something and change font name/size or, without selection, change name/size and type new characters.
Note: required at least Java 5.



P.S. The user interface is not "cool", I have used the quickest way to create the layout. The point is not the appearance but the setCharacterAttributes method of JTextPane!

P.S. 2: moving the caret, the combos do not reflect the style. This is wanted for the example. It's possible to do this with more code.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


here is a SSCCE.
as i expected, it behaves the same way as the real program.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the API docs for StyledEditorKit.FontSizeAction (emphasis mine)

An action to set the font size in the associated JEditorPane.

 
Rob Camick
Rancher
Posts: 3324
32
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

as i expected, it behaves the same way as the real program.



That is the point of a SSCCE. Either it will work, in which case you need to compare the working example with the real program to see what is different and then fix the difference.

Or, it still doesn't work and you have code to post that we can look at without caring about your 300 line program.

The problem with your code is that the Actions from the EditorKits work on the last text component to have focus. At the time your code is invoked no component has focus because the frame is not yet visible.

So to solve your problem you need to do three things in the following order:

1. make the frame visible
2. set focus on the text pane
3. select an item from the combo box

The basic code would be:


The usage of the invokeLater is a trick used to make sure the code is executed at the end of the Event Dispatch Thread. In this case it is needed to allow the text pane to receive focus so that font size Action will work.

Because you did not include a proper SSCCE with your original question Andrea wasted time creating their own SSCCE which did not address your concern, because your description of the problem was not as good as you think it was. I do not appreciate you wasting the time of anybody who reads your questions because you are too lazy to take 5-10 minutes to post a proper SSCCE with your questions.

Your future questions should include a SSCCE with the initial question. You should NOT wait for someone to ask for one.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, it is just like with the setLocationRelativeTo(null)
it depends on when you call it.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i marked this resolved too quickly. when i tried it, it still doesn't work
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i didn't read all of your reply. after you said 1,2,3 i tried it. i only have internet at places with free wifi. i am here right now without buying anything. i will mark this resolved again.
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You seemed to have missed out my earlier post with the hint of associating the FontSizeAction with the JEditorPane
Try this code to understand what I meant
 
Rob Camick
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You seemed to have missed out my earlier post with the hint of associating the FontSizeAction with the JEditorPane



That code still doesn't help. The problem is that the OP wants the text to be font size 72 as soon as the frame is made visible, without the user making any selection from the combo box.

All the Swing editor Actions extend from TextAction. The Action will only work on the last text component that had focus. This way you can do cut/copy/paste on any text component by simply creating menu items with the provided Action in the EditorKit.

Unless you use code like what I supplied, no text component has had focus when the combo box actionPerformed is invoked at GUI initialization, so the font size Action is simply ignored.
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:That code still doesn't help. The problem is that the OP wants the text to be font size 72 as soon as the frame is made visible, without the user making any selection from the combo box.


My bad. I seem to have missed out on that requirement. One consolation (for me) is that the code I posted does change the font size, but for all the text in the JTextPane, which I understand now, is not exactly what the OP wanted. Sorry about the confusion.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
don't worry Maneesh, you have 6 cows
that means people find your answers helpful

btw, it all works perfectly now
reply
    Bookmark Topic Watch Topic
  • New Topic