-
January 27th, 2012, 02:45 AM
#1
JTextField-Text Access
i'm making a program which needs the access to the text in JTextField......
i just want to know how to do it....
the first thing that program do is that it gives the current date.....
Code:
package date.update;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.*;
public class DateUpdate {
public static void main(String[] args) {
// TODO code application logic here
JFrame f = new JFrame("DATE-UPDATE");
f.setBackground(Color.LIGHT_GRAY);
JTextField TF = new JTextField("hello",50);
JTextField TF1 = new JTextField();
InputReader IR = new InputReader();
JTable T = new JTable();
String[] headers = {"MONDAY","TUESDAY"/*,"WEDNESDAY","THURSDAY","FRIDAY","SATURDAY"*/};
Object[][] data = {{"DS","M3"},{"M3","DS"}};
T = new JTable(data,headers);
JScrollPane scrollPane = new JScrollPane(T);
T.setFillsViewportHeight(true);
JButton B = new JButton("DATE");
JButtonListener JBL = new JButtonListener(TF1);
B.addActionListener(JBL);
f.getContentPane().add(B, BorderLayout.EAST);
f.getContentPane().add(TF,BorderLayout.NORTH);
f.getContentPane().add(TF1,BorderLayout.PAGE_END);
f.add(scrollPane);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
button listener class
Code:
public class JButtonListener implements ActionListener{
private JTextField tf1 = new JTextField();
JButtonListener()
{
}
JButtonListener(final JTextField tf)
{
tf1 = tf;
}
public void actionPerformed(final ActionEvent AE)
{
final Date date = new Date();
tf1.setText(""+date);
}
}
so i want to access the text of TF1 i.e the current date.....
-
January 27th, 2012, 10:46 AM
#2
Re: JTextField-Text Access
You are much more likely to understand the code you have written if you give your variables sensible names instead of one or two letters. Also you'll be much more likely to get help here if we can easily understand your code ie if you give things sensible meaningful names and you follow the Java naming conventions (for instance, variable names should start with lower case letters).
so i want to access the text of TF1 i.e the current date.....
Where do you want to access the contents of TF1?
-
January 28th, 2012, 02:12 AM
#3
Re: JTextField-Text Access
Where do you want to access the contents of TF1?
i want to take it as String and then compare with some other strings(time/date) defined by me....like if the time (date) matches it will return the "to-do thing" defined for that time......
-
January 28th, 2012, 07:15 AM
#4
Re: JTextField-Text Access
I didn't ask "what do you want to do with it", I asked "where do you want to access it". ie where in your code do you want to call TF1.getText().
like if the time (date) matches it will return the "to-do thing" defined for that time......
How do you want to trigger this comparison - is it when you click on another button or when you have finished entering the date and press enter or some other mechanism?
-
January 28th, 2012, 08:11 AM
#5
Re: JTextField-Text Access
How do you want to trigger this comparison - is it when you click on another button or when you have finished entering the date and press enter or some other mechanism?
actually i'm trying to make an app.....
in this when i press the date button and get the date, it will automatically check the date and time and accordingly show the "to-do thing" defined for the time.....
so it will much easier for me if you tell me how to get that date format as a string.....
and more question.... is it necessary to define a action listener class for every button separately or i can do it for all the buttons in one class only..........
-
January 28th, 2012, 08:17 AM
#6
Re: JTextField-Text Access
one more question....
in the above app if i add a button and pressing that button should take me to next window/JFrame....
so can you tell me how to implement it..........
-
January 28th, 2012, 08:33 AM
#7
Re: JTextField-Text Access
so it will much easier for me if you tell me how to get that date format as a string.....
I was asking where you wanted to do it for a good reason and that is the best way to do it depends on where you want to do it. ie:
The text you want is the same text you have put into the text field ie when you called setText(..). So if you are using the text in the listener you just need to use the text returned from the Date object's toString method. If you are using it elsewhere and the date object is not available then you have to get the text back from the JTextField so you need to call TF1.getText().
BTW you would be better off creating a java.text.DateFormat object to convert the Date object into text as you can control exactly how it is converted rather than relying on the Date object's toString() method.
is it necessary to define a action listener class for every button separately or i can do it for all the buttons in one class only
You can use either technique, however, if multiple components share the same action listener and each component requires a different action to be performed then your ActionListener has to work out which component triggered the action and hence which code to run which goes against the principles of OOP. Personally, I only share an ActionListener if the actions to be performed are either identical or are virtually identical and require very little special case handling.
-
January 28th, 2012, 10:04 AM
#8
Re: JTextField-Text Access
I was asking where you wanted to do it for a good reason and that is the best way to do
oh!!.... sorry i didn't get your earlier reply in the same way as you described it....and that's why i asked you the same question again and again....anyway your reply was very useful....
thanks....
-
January 28th, 2012, 10:28 AM
#9
Re: JTextField-Text Access
i have created another class which shows a JTable in a JFrame....
i want to access this JFrame from my earlier class where i have made the date stuff....
so i included one more button(WHOLE DAY) in the date-update class....
so can u guide me how to write the code for action listener class for accessing the new class....
i mean the WHOLE DAY button should show me the JTable....
-
January 28th, 2012, 11:49 AM
#10
Re: JTextField-Text Access
If you mean when the user clicks on the WHOLE DAY button a new JFrame should be displayed which has the table in it then for start it should probably be in a JDialog and not a JFrame. But to display it you create an instance of it and make it visible in the same way you did for the main GUI.
-
January 29th, 2012, 12:39 AM
#11
Re: JTextField-Text Access
the above stuff worked....
now i just wanna know if i can get the same JTable on the first JFrame only....
i mean the JTable should replace the content of the first JFrame....
-
January 29th, 2012, 12:22 PM
#12
Re: JTextField-Text Access
can u tell me how to move a JButton in a JFrame....i tried every possible thing known to me but its not working....
-
January 30th, 2012, 01:49 PM
#13
Re: JTextField-Text Access
now i just wanna know if i can get the same JTable on the first JFrame only....
i mean the JTable should replace the content of the first JFrame....
Remove all the added components from the content pane and add the JTable to it.
Alternatively use a Layout Manager like CardLayout to swap between displayed panels.
can u tell me how to move a JButton in a JFrame
That depends on how you are positioning it in the first place. If you are using a Layout Manager then it depends on the type of Layout Manager you are using.
-
January 30th, 2012, 10:24 PM
#14
Re: JTextField-Text Access
how would i implement if i'm not using any layout manager....
-
January 31st, 2012, 04:16 AM
#15
Re: JTextField-Text Access
Are you sure you are not using a Layout Manager ie have you explicitly set the Layout Manager to null?
If you aren't using a Layout Manager then it's up to you to position and size your components, but not using a Layout Manager is generally a really bad idea - you lose all the benefits they provide.
For how to do it read the tutorial.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|