简体   繁体   中英

How do i display in JTextarea or JTable

So I have created two buttons and I want to do a specific task when the buttons are clicked. If button 1 (b1) is clicked using the ActionListener, I want to create an object of Van and display the instance variables in a JTextarea or JTable. For example if Van button is clicked then the action would be to create an object of Van and get the instance variable values and print them in a JTextArea/JTable. Below is my code so far:

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;


public class TestButton extends JFrame{

JTable table;

public TestButton (){

    setLayout(new FlowLayout());
}
static class ActionTwo implements ActionListener{

    @Override
    public void actionPerformed (ActionEvent evt){

        Vehicle sport = new Sportcar (200, 1500, 220);

    }
  }

  static class Action implements ActionListener{

    @Override
    public void actionPerformed (ActionEvent evt){

        Vehicle aVan = new Van(100,0.9,3500,160.4);


    }

   }

  public static void main (String [] args){

  JFrame frame = new JFrame ("Type of Vehicle");
  frame.setVisible(true);
  frame.setSize(400,200);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setLocationRelativeTo(null);
  JPanel panel = new JPanel();
  panel.setBackground(Color.black);
  JButton b1 = new JButton("Van");
  JButton b2 = new JButton("Sports Car");
  panel.add(b1);
  panel.add(b2);
  frame.add(panel);
  b1.addActionListener(new Action());
  b2.addActionListener(new ActionTwo());

  }




  }

Have a look at the Java tutorial on Action Listeners

This will do what you would like it to do, but you should read through that tutorial to get a full grasp of what's happening.

public static void main (String [] args){
    JTextField text = new JTextField();
    ActionListener textSetter = new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {
          JButton clicked = (JButton) e.getSource();
          text.setText(clicked.getText());
      }
    };

    JButton btnVan = new JButton("Van");
    btnVan.addActionListener(textSetter);
    JButton btnCar = new JButton("Sports Car");
    btnCar.addActionListener(textSetter);

    JPanel btnPanel = new JPanel();
    btnPanel.add(btnVan);
    btnPanel.add(btnCar);

    JPanel mainPanel = new JPanel(new BorderLayout());
    mainPanel.add(text, BorderLayout.NORTH);
    mainPanel.add(btnPanel, BorderLayout.SOUTH);

    JFrame frame = new JFrame ("Type of Vehicle");
    frame.add(mainPanel);
    frame.setSize(400,200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM