display text box after clicking button- java - java

I'm new to Java and I'm working on an airplane seat reservation program. I would like to know how to display a JTextfield text box and a JLabel after clicking a button that is already displayed in a JFrame window. I have tried to add the button after an if statement but it doesn't work. I have also tried button.setVisible(false) and then making it true later. I have a button.actionlistener(this) for each of the buttons used.
public void actionPerformed(ActionEvent event){
Container contentPane = null;
JButton clickedButton = (JButton) event.getSource();
if (event.getSource() == coach){
five.setForeground(Color.green);
eleven.setForeground(Color.green);
six.setForeground(Color.green);
two.setForeground(Color.black);
if (event.getSource() == five) {
inputLine = new JTextField();
inputLine.setBounds(110, 180, 185, 22);
contentPane.add(inputLine);
}
} else if (event.getSource() == firstClass){
two.setForeground(Color.green);
five.setForeground(Color.black);
eleven.setForeground(Color.black);
six.setForeground(Color.black);
}

Am not totally clear by your information, whether u mean to display the created textfields and labels on the created button or create the same on the created button.
But I have some code for you, see if it helps.
JTextField[] desc=new JTextField[20];
JButton more=new JButton("More");
Container c=getContentPane();
public void actionPerformed(ActionEvent a1)
{
String s1=a1.getActionCommand();
String s="";
for(i=0;i<cnt;i++)
s=s+desc[i].getText();
if(s1.equals("More"))
{
System.out.println("Created");
desc[i]=new JTextField();
desc[i].setBounds(50,y,150,30);
c.add(desc[i]);
}
}
Same way create for JLabel.

Related

changing attributes to a JButton created by a method without variable name

I create some JButtons with a method, but I don't give them a variable i can call them with. i was wondering if it is possible to change the text somehow, after the button is created from another method. I am aware of that i can get the action command when the button is pressed but i want to change the button text, without it being pressed. I can give the buttons names like but would prefer not to. since I am only going to call half of them, and then I don't think its a good idea. or is it?
JButton button1 = buttons(0,0,0);
public JButton buttons(int coord, int coord1, int number) {
JButton box = new JButton("");
box.setFont(new Font("Tahoma", Font.PLAIN, 60));
box.setBounds(coord, coord1, 100, 100);
contentPane.add(box);
box.addActionListener(this);
box.setActionCommand(Integer.toString(number));
return box;
}
public static void main(String[] args) {
buttons(0,0,0);
buttons(98,0,1);
buttons(196,0,2);
buttons(0,98,3);
addText();
}
public void addText() {
//help me guys
button.setText("Please fix me");
}
You can get pressed button from actionEvent
#Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();
}
Why not just have them in a public arraylist?
ArrayList<JButton> buttons = new ArrayList<JButton>();
public JButton buttons(int coord, int coord1, int number) {
JButton box = new JButton("");
box.setFont(new Font("Tahoma", Font.PLAIN, 60));
box.setBounds(coord, coord1, 100, 100);
contentPane.add(box);
box.addActionListener(this);
box.setActionCommand(Integer.toString(number));
buttons.add(box);//Add it here
return box;
}
This way you can loop through them later and change something if you want to,
for(JButton b : buttons){
if(/*Whatever*/){
b.setText("New name");
}
}
I can give the buttons names like but would prefer not to. since I am only going to call half of them, and then I don't think its a good idea. or is it?
You are creating problems for yourself. Even if you may only need the call a few/none of them, there is significant advantage for not keeping a reference for them.
If you have too many JButtons and you do not want to have a separate variable name for each of them, you can have an array/collection of JButtons.
JButton[] btns = new JButton[size];
//or
ArrayList<JButton> btns= new ArrayList<JButton>();
To change text for all buttons:
for(JButton b : btns)
btns.setText("whatever here");
To change text for a specific button:
btns[x].setText("whatever here"); //from array
btns.get(x).setText("whatever here"); //from list
Alternatively, if you do not keep a reference. You can get the list of buttons from the content pane:
Component[] components = myPanel.getComponents();
for(Component c : components)
if(c instanceof JButton)
((JButton)c).setText("whatever here");

How to update JTextfield after click SAVE button

I have a main frame : JFrame>contentFrame>ScrollPane>BigPanel>panel_1T
private JPanel contentPane;
private JPanel BigPanel;
private JPanel panel_1T;
In panel_1T, I have put a FOOD button WITH its actionListener:
JButton button_19 = new JButton("FOOD");
button_19.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
newFoodUI nf = new newFoodUI();//Open other class
nf.setVisible(true);
nf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
});
panel_1T.setLayout(new GridLayout(0, 2, 0, 0));
panel_1T.add(button_19);
When user click FOOD button, new JFrame in newFoodUI class will be shown.:
JFrame>contentPane>panel>tabbedPane>panel_3>panel_5
In panel_5, I put a JTextField:
public static JTextField textField_3;
textField_3 = new JTextField();
panel_5.add(textField_3, "9, 4, fill, default");
textField_3.setColumns(10);
User will write some text into textField_3. Then user click SAVE button in panel_3, it will perform this:
JButton button_4 = new JButton("SAVE");
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setContentPane(contentPane);
panel_3.revalidate();
panel_3.repaint();
panel_3.updateUI();
panel_5.revalidate();
panel_5.repaint();
panel_5.updateUI();
contentPane.revalidate();
contentPane.repaint();
JOptionPane.showMessageDialog(null, "Saved !");
}
});
button_4.setBounds(873, 396, 75, 33);
contentPane.add(button_4);
}
The result is, when I click SAVE button and close the Frame in newFoodUI, I will reopen back by click the FOOD button to check whether the text I wrote has been saved or not. But its not saving the text I wrote.
You have to save the value from the textfeld textField_3.getText() and set this value manually to textfeld when showing textField_3.setText(value). So you have to keep your value in your project or store persistent somewhere.
There are a couple of things to fix here and I will not give you complete code but I will point out some errors. First let's consider your button_19 listener
public void actionPerformed(ActionEvent ae) {
newFoodUI nf = new newFoodUI();//Open other class
nf.setVisible(true);
nf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
When this is performed, it creates a totally new object of newFoodUI and gives it no parameters. So how could this frame know about anything that happened before its creation if you give it nothing? Additionally, you explicitly say DISPOSE_ON_CLOSE, when you could use HIDE_ON_CLOSE if you wish to reuse it.
Then in your JButton button_4 = new JButton("SAVE"); listener you want to save the data in the text field, but your implementation does nothing to the text field. You should, for example, get the text from textField_3 and write it to a file or send back to the first JFrame.
Then there is the issue of using multiple JFrames in the first place.

Adding a button that reset all the text fields Netbeans Java

I'm trying to make a simple calculator and i want a reset button when pressed it clears all the text fields. I've already added an addActionListener and and ive already added the button panel just need to know the method on how do I do this. here is my code -
else if(choice.equals("c")) {
xValue = inputXTextField.getText();
yValue = inputYTextField.getText();
if(convertPreOperand(xValue) && convertPostOperand(yValue)) {
total = preOperand c postOperand;
outputTextField.setText(Double.toString(total));
}
}
You can use inputField.setText(""); to set the text of any field to nothing.
Your Code sniplet is quite short and I can't figure out, what it is there for, except giving the names of the Textfields?
Anyway, in genereal your program could look like this:
public class calculator implements ActionListener{
JTextField inputXTextField;
JTextField inputYTextField;
JButton bClear=new JButton("clear all");
JPanel panel;
private void init(){
bClear.addActionListener(this);
panel.add(bClear);
panel.add....
}
void actionPerformed(ActionEvent e){
Object obj=e.getSource();
if (obj=bClear){
inputXTextField=new JTextField(...);
or
inputXTextField.setText(""); //just to empty the field
.
.
}
}
JTextField textField1, textField2; //Or as many as you want;
JButton clear;
private void init(){
textField1 = new JTextField(); //Do this for all text fields.
clear=new JButton("C");
clear.addActionListener(this);
//Add controls to the panel/frame
}
void actionPerformed(ActionEvent e){
Object obj=e.getSource();
if (e.getSource==clear){
textField1.setText("");
textField2.setText("");
//... For all the textFields you have
}
}

Selecting radio buttons to make text appear

Using radio buttons displayed on a panel, is it possible to select the radio button and then display some text on the panel explaining what the user has selected?
So here is a list of radio buttons
public void RadioButtons() {
btLdap = new JRadioButton ("Ldap");
btLdap.setBounds(60,85,100,20);
panelHolder.add(btLdap);
btKerbegos = new JRadioButton ("Kerbegos");
btKerbegos.setBounds(60,115,100,20);
panelHolder.add(btKerbegos);
btSpnego =new JRadioButton("Spnego");
btSpnego.setBounds(60,145,100,20);
panelHolder.add(btSpnego);
btSaml2 = new JRadioButton("Saml2");
btSaml2.setBounds(60,175,100,20);
panelHolder.add(btSaml2);
}
User selects btLdap
btLdap.setSelected(true);
Now how do you make the text appear on the panel not a message box
If you want to display a text when a radio button is selected you could use ActionListener.
final JTextArea textArea = new JTextArea();
add(textArea);
JRadioButton radioButton = new JRadioButton();
add(radioButton);
radioButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
textArea.setText("Selected");
}
});
JRadioButton radioButton2 = new JRadioButton();
add(radioButton2);
radioButton2.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
textArea.setText("Selected 2");
}
});
radioButton.setSelected(true);
When the first is selected it will change the text of the JTextArea with The first is selected!, same the second radio button but with The second is selected.
As you said, radioButton.setSelected(true); setSelected is used to select/deselect a radio button.
In this example i used textArea, but you can use everything which have a method to change the text it contains (an image too!)
Official DOC, here.
Anyway, actionPerformed is not called when setSelected is used so i would go to something like a method
private void updateText(int index)
{
String text = null;
switch (index)
{
case 0:
text = "Selected";
break;
case 1:
text = "Selected 2";
break;
}
textArea.setText(text);
}
And then call updateText(0 or 1 etc.) when you want to select setSelected another radio button and update the text too.
All this is useful, if you want to show a "what happens if you press it" message, but if you just want to change the text of the area with the text of the radio button, just use
textArea.setText(e.getActionCommand());
Here is some Example how to use an ActionListener on a JRadioButton:
public class ListenerExample extends JFrame implements ActionListener {
private JRadioButton check = new JRadioButton("hello");
private JLabel label = new JLabel();
public ListenerExample() {
check.addActionListener(this);
add(check);
add(label);
setLayout(new FlowLayout());
setSize(800, 600);
setVisible(true);
}
public static void main(String[] args) {
new ListenerExample();
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JRadioButton) {
JRadioButton button = (JRadioButton) e.getSource();
label.setText(String.valueOf(check.isSelected()));
}
}
}
Using an anonymous inner class you will have something like:
yourRadioButton.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent event ) {
// here you will write the code that you want to
// be executed when your radio is clicked
yourLabel.setText( "Some text..." );
// if you want to exeute it onlye when it is selected
// you will need to do this
if ( yourRadioButton.isSelected() ) {
// some code here
}
}
});
In Java 8, recently released, you can use a lambda expression to register your listener. Something like:
yourRadioButton.addActionListener( event -> {
// here you will write the code that you want to
// be executed when your radio is clicked
yourLabel.setText( "Some text..." );
// if you want to exeute it onlye when it is selected
// you will need to do this
if ( yourRadioButton.isSelected() ) {
// some code here
}
});

Text Field disabling in NetBeans

I want to ask if there is a way to make the text field active and inactive according to the radio button.
For example, the textfield will be inactive and when the user click on the radio button, the textfield will be active.
I am using Java language and NetBeans program
You could have two radio buttons for representing the active/inactive state. Add an action listener to each and when the 'active' one is pressed you call setEditable(true) on the JTextField and when the 'inactive' JRadioButton is called you call setEditable(false).
JTextField textField = new JTextField();
JRadioButton activeButton = new JRadioButton("Active");
JRadioButton inactiveButton = new JRadioButton("Inactive");
activeButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
textField.setEditable(true);
}
});
inactiveButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
textField.setEditable(false);
}
});

Categories

Resources