• Post Reply Bookmark Topic Watch Topic
  • New Topic

inserting code for a JSlider  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hoping someone can help.

We have a class test next week and have been given some work to do beforehand. However, I am stuck on inserting code for a JSlider so that it will control the size of the font. The concept is the user inputs how many students got a grade and then it's published on screen with that amount e.g. the user puts 5 in the A box, AAAAA is displayed in the graph. I have managed to put the JSlider on the screen but don't know how to tie it into to control font size.

My code so far is:

 
Bartender
Posts: 572
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way is to add a ChangeListener to the slider, and have the stateChanged() method of the listener change the font size of the relevant components.
This works OK fif you are only changing the font of one or two components, and the listener has access to the component references.

A better way is to have the stateChanged() method issue a property change for the font size, and then just add a PorpertyChangeListener to each component whose font size you want to be modified. that way the slider's ChangeListener dioes not have to know about any of the components it will be controlling, and components can be easily added or removed from participating, at run time.
 
Ranch Hand
Posts: 2828
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am stuck on inserting code for a JSlider so that it will control the size of the font.


First you need to learn how to do custom painting. you should NOT be using the getGraphics() method of the panel.

The proper way to do custom painting is to extend the JPanel and override the paintComponent(...) method. Read the section from the Swing tutorial on Custom Painting for more information and examples to get you started.

The Graphics object passed to the paintComponent() method will already be set to the Font of the panel.

For you slider logic, all you need to do is reset the Font of the panel. So in the stateChanged event you might do something like:



This will update the Font of the panel so that the next time the panel is painted the new Font will be used. The repaint() will till the panel to paint itself now.
 
Ryan O'Mara
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
I am stuck on inserting code for a JSlider so that it will control the size of the font.


First you need to learn how to do custom painting. You should NOT be using the getGraphics() method of the panel.

The proper way to do custom painting is to extend the JPanel and override the paintComponent(...) method. Read the section from the Swing tutorial on Custom Painting for more information and examples to get you started.

The Graphics object passed to the paintComponent() method will already be set to the Font of the panel.

For you slider logic, all you need to do is reset the Font of the panel. So in the stateChanged event you might do something like:



This will update the Font of the panel so that the next time the panel is painted the new Font will be used. The repaint() will till the panel to paint itself now.


Hi and thanks for replying.

I have been coding for around 2 months and haven't been taught another way but will be looking at the paintComponent as I am always looking to learn new things especially if its a better. I'll also be trying the JSlider method, later on, tonight and will get back to you on how I go.

Thanks for the reply,
Ryan
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
Ryan O'Mara
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems that the JSlider still is not working.

The last part of this code "fontSize.addChangeListener( this );" is underlined in red but i am unsure how to remove it.



I also added this to the stateChanged but hasn't done anything.

 
Rob Camick
Ranch Hand
Posts: 2828
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems that the JSlider still is not working.


Start by reading the section from the Swing tutorial on How to Use Sliders for working examples of using a slider. Download the demo code and play with it.
 
Ryan O'Mara
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
Seems that the JSlider still is not working.


Start by reading the section from the Swing tutorial on How to Use Sliders for working examples of using a slider. Download the demo code and play with it.


I have read that and also been through my book recommended by the university and I still can't seem to find what is wrong.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what is going wrong?

I can see one thing seriously wrong with your code; you have got the display component implementing action listener, so you have a horrible long action performed method with lots of nasty if‑else statements. Get rid of that and create different action listeners for each button. This may be a stage beyond you at present, but now Java8 has arrived (and Java9 too) you can write a λ and write something like:-
openFileButton.addActionListener(evt -> openFile());
and all your file opening logic goes in the other method, which is probably where it belongs anyway.

You can remove the offending code with the red mark by deleting it with the delete key. You replace it with adding an instance of ChangeListener (or since changeListener has only one method) with a λ similar to the above. Make sure to pass the slider as an argument to whichever method you call. I think it would be inappropriate for me to give you much more code at the moment. You can write a private inner class implementing ChangeListener and add it like this:-
fontSlider.addChangeListener(new FontSizeChangeListener(fontSlider));
 
Rob Camick
Ranch Hand
Posts: 2828
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read that and also been through my book recommended by the university and I still can't seem to find what is wrong.


This is why a question should always be asked in the form of a SSCCE.

So what you need to do is create a frame with a JLabel (and some text) and a JSlider with the ChangeListener.

When the ChangeListener fires you change the font size of the label.

Once you understand the basic concept of writing the listener and updating the font size of a different component THEN you try to fix the problem in your code.

If you can't get the SSCCE to work then you have simple code to post in the forum. The SSCCE will be less then 20 lines of code so it will be easier for members of the forum to read the code.

The main point is to simplify the problem BEFORE asking a question because 990% of the code you posted has nothing to do with the stated problem.
 
Ryan O'Mara
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think I'll just leave it and go on to the next part as I have no clue what I am doing and it's p***ing me off.
 
Rob Camick
Ranch Hand
Posts: 2828
15
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think I'll just leave it and go on to the next part


Wrong approach.

You are trying to make you code bigger an more complicated with more functionality, which will make it harder to debug.

The better approach is to used building blocks. Get a simple part working first since it is easier to debug.

That is why you need to create the SSCCE

It should take like 5 minutes to create the SSCCE I described above.

Learn to walk before you run.
 
Author
Ranch Hand
Posts: 959
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan O'Mara wrote:
Rob Camick wrote:
First you need to learn how to do custom painting. You should NOT be using the getGraphics() method of the panel.

The proper way to do custom painting is to extend the JPanel and override the paintComponent(...) method.


Hi and thanks for replying.

I have been coding for around 2 months and haven't been taught another way


Oh dear. Does this mean that some kind of formal course has recommended using JComponent.getGraphics() like this? That would be unfortunate.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!