Dispay non-String variable to JLabel - java

I wonder if JLabel really can't display a value beside String and Icon. For example for displaying Clock variable..
Clock clock = Clock.systemDefaultZone();
And the JLabel setText() method as following..
label.setText(clock);
I'm aware that setText() method only works with String parameter. But since clock variable can't be converted to String, i really have no idea how to display it on the label. Or what should i use instead of JLabel to display Clock variables.
I tried label.setText(Clock.valueOf(clock));, until later i realized that only works for primitive data (or no?). I really stuck at this moment.

I'm going to assume you're using java.time.Clock. You can convert the current time into an ISO-8601 string by doing clock.instant().toString().
Therefore, you can put the clock in the label using:
label.setText(clock.instant().toString());
If you want the label to show the clock updating, you may need to use something like a Timer to reset the label text every second.
You can change the formatting if you don't want ISO-8601 with this to set your own format.

JLabel will set the text to an object
if you have overridden the toString() method
and call to it..
As
System.out.println(clock); //this would automatically call the toString() method.
For this.. You would have to directly call it.
label.setText(clock.toString());

Related

get the value in the JTextfield after setText set the value into the Textfield JAVA

The situation is like this, first I set the text using the setText() function into the JTextField, then is it any way to retrieve the text that set into the TextField, I have already tried the getText() method, but the function get NOTHING from the TextField. Because my situation is complicated, so I must get the Text that set into the Textfield.
JTextField Jtf= new JTextField();
Jtf.setText("some value..");
String get = Jtf.getText();
I can't get the value that I set into the TextField, is there any solution for this? TQ.....
your code is right. text field should be instantiate in constructor check if not instantiate than do it, also check System.out.println(get), may you are doing wrong something.

JPanel check if values changed

Lets say I have 2 JPanels.
I create both of them while program is loading and set 2nd to visible(false). 2nd JPanel uses information entered in 1st JPanel. I want to use that information.
However since both panels are created at the same time the value from 1st Panel will be null.
I need something like repaint(), but it should repaint all JTextFields with new variable values. I can't find anything useful in java api.
Is there something like this in java?
If not, what are my options?
Create a method in the second JPanel that sets all the values up.
Pass a reference to the second JPanel into the first.
When the first is ready it can call the method in the second to set all the values.

Can I reuse a JLabel component on the different parts of my program?

For example, I have a calendar. Is it possible to reuse the JLabel that contains the date number "1" to all the months in my calendar? or do I have to make another JLabel in all of it? I did a full year calendar but my code has 7k lines of code...
The answer is: yes you could reuse a GUI component, but then the prior component would be unusable since the JLabel can be displayed on only one container at a time, the container that it was most recently added to.
But why would you want to? Re-use an ImageIcon if your JLabel is displaying it. Otherwise you're just displaying text with it. Your question suggests that your program's design could be off, that you might want to re-think the structure of your program. Most importantly, don't confuse your program's model with it's view.
Your recent comments:'
Would it be possible if I don't use an ImageIcon? and just use pure JLabel text? I am using the Netbeans GUI creator btw.
Again why would you want to? It just holds text. I advise you not to do this, and again I fear that your program design is bad. Maybe what you really want to use is a JTable.
Hi! thanks for the fast reply! My JLabel dates are within a JPanel (month panels), and all the panels are within a JFrame. And when I click a JButton, it hides the current panel and shows the next one. Each Panel(12month panels) has many JLabels. Should I make another JFrame for each year to avoid lags?
There should be no lag. You will want to swap JPanels by using a CardLayout.
What you likely want to do is create a class for a general Month object and then create instances of this object for each specific month. Again, think re-design. Also, learn to code without generated code since this will enhance your understanding of the library that you're using.
Edit More Recommendations:
First create a non-GUI model class, Month:
Give it a String field for the month name
Give it a day of the week (possibly an enum) for the day of the week of the 1st of this month
Give it an int field for number of days of the week.
You might want to give it ArrayList of DayEvent objects to hold possible events to put on the calendar such as birthdays, holidays, etc. as well as setter and getter methods for this.
Then for your GUI portion, create a GUI class to represent the month, say called MonthView
Create it by hand not by NetBeans generated code
Give it a Month field so it can hold and use its model, an instance of the Month class above.
Give it a BorderLayout
Give it a title JLabel that displays the Month name, and add this BorderLayout.NORTH.
Give it a JPanel that uses a GridLayout(0, 7) -- for seven columns, and a variable number of rows. Add this BorderLayout.CENTER
Populate this GridLayout with JLabels.
Have logic available that will place your date strings in the correct JLabel grid cells.
Now your dates will be represented by code that has at most 35 JLabels. Sure there will be 365 JLabels for your dates made, but there will only need to be code for 35 (7 * 5) JLabels to allow you to create all your JLabels.
You should create a new JLabel if you're going to draw it concurrently with earlier JLabels.
To get a high quality answer, you should provide a clear, simple, self contained code example that demonstrates your problem.

Java: How to set value of one JDateChooser to another JDateChooser?

I have two JDateChoosers, One with label 'Start Date' & another with 'End Date'.I have two radio buttons 'single Day' & 'Multiple Day'.If I select 'Single Day' I want to display same date to 'End Date' which selected in 'Start Date'. And I also want to clear these JDateChooser fileds on CLEAR_BUTTON_CLICK.How do I write this? I am using this control first time..
Plz,help me..
Thanks in advance..
I'm assuming that you are talking about JDateChooser from JCalendar. Am I correct? The JDateChooser fires a PropertyChangeEvent when its date is changed. So, to set the date of another JDateChooser, you need to add an event handler to the "source" component to deal with the change event. When it is fired, you take the date of the component using getDate() method and set it to the target component, using setDate() method. As you are using a component suite that I don't have installed here, it is difficult to implement a correct solution to you.
Take a look in the docs: http://www.toedter.com/en/jcalendar/api/com/toedter/calendar/JDateChooser.html
I think that reading this you will be able to do what you want.
Edit: Here is some code. Try to use it. I really don't have certain that it will works since I didn't test it.
// sourceDateChooser and targetDateChooser MUST be final,
// since they will be accessed inside a anonymous inner class
sourceDateChooser.addPropertyChangeListener( new PropertyChangeListener(){
#Override
public void propertyChange(PropertyChangeEvent evt) {
// the docs of JDateChooser says that when the date is modified, a "date" property change is fired
if ( evt.getPropertyName().equals( "date" ) ) {
targetDateChooser.setDate( sourceDateChooser.getDate() );
}
}
});

Java - Custom JSlider will not update

I have a custom component based on the JSlider. It's essentially the same thing only it has 2 thumbs, which I named a DualSlider.
I need to change the maximum value of the slider once in a while, so every time I do, I call updateUI to reflect this.
public void updateUI() {
this.setUI(new DualSliderUI(this));
this.updateLabelUIs();
}
However, the maximum value of the DualSlider when I try to use it is still set at the original value no matter how many times I try to change it while using my program. I can confirm with a few println statements that a new DualSliderUI is being made with the slider that has the new max value, but for whatever reason the original DualSliderUI I initialized the slider with is the one that is in use.
What other things do I have to make sure I do when I update a property so I can avoid this?
1) I can't see reason for usage updateUI() this should be done once time, only when you built this JSlider, never do that repeately
2) you have look at BoundedRangeModel, maybe

Categories

Resources