How to collect Java input field value display into Jtable?

Ads
 

How to collect Java input field value display into Jtable?

<p>We would like to know how to collection input field data into Jtable with the following format. Please advise.</p>

<ol>
<li>text field - input amount</li>
<li>text year - input year</li>
<li>text rate - input interest rate</li>
<li>text Result - Display result into Jtable</li>
</ol>

<p>Jtable</p>

<h1>Amount Year  Rate  Interest  Total</h1>

<p>1  1,000   5    5.0%   250.0   1,250.0
2    500   8    4.0%   160.0     660.0
3  1,500   7    3.5%   367.5   1,867.5
4  2,000   4    6.5%   520.0   2,520.0</p>
View Answers

January 19, 2011 at 11:12 AM

Hi Friend,

Try this:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;

public class SimpleInterestTable extends JFrame {
  private DefaultTableModel model;

  private JTable table;
  private JScrollPane pane;
  public SimpleInterestTable() {
    setLayout(null);
    final JLabel lab1=new JLabel("Amount");
    final JLabel lab2=new JLabel("Year");
    final JLabel lab3=new JLabel("Rate");
    final JLabel result=new JLabel("Interest");
    final JTextField text1=new JTextField(20);
    final JTextField text2=new JTextField(20);
    final JTextField text3=new JTextField(20);
    final JTextField text=new JTextField(20);

    JButton b=new JButton("Calculate");
    model = new DefaultTableModel();
    model.addColumn("Amount");
    model.addColumn("Year");
    model.addColumn("Rate");
    model.addColumn("Interest");
    model.addColumn("Total");

     b.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
        pane.setVisible(true);
        double p=Double.parseDouble(text1.getText());
        double t=Double.parseDouble(text3.getText());
        double r=Double.parseDouble(text2.getText())/100;

        double si=p*r*t;
        text.setText(Double.toString(si));
        double tot=p+si;

    String[] data = {Double.toString(p),Double.toString(t),text3.getText()+"%",Double.toString(si),Double.toString(tot)};
    model.addRow(data);
    }
    });
    table = new JTable(model);
    pane=new JScrollPane(table);
    lab1.setBounds(10,10,100,20);
    text1.setBounds(120,10,100,20);
    lab2.setBounds(10,40,100,20);
    text2.setBounds(120,40,100,20);
    lab3.setBounds(10,70,100,20);
    text3.setBounds(120,70,100,20);
    result.setBounds(10,100,100,20);
    text.setBounds(120,100,100,20);
    b.setBounds(120,130,100,20);
    pane.setBounds(10,160,450,100);
    add(lab1);
    add(text1);
    add(lab2);
    add(text2);
    add(lab3);
    add(text3);
    add(result);
    add(text);
    add(pane);
    add(b);
    pane.setVisible(false);
    setVisible(true);
    setSize(500,300);
    } 
  public static void main(String args[]) {
    new SimpleInterestTable();
  }
}

Thanks


January 19, 2011 at 1:47 PM

Please advise in the GUI as the following information. - display the value in the JTable.

If in the GUI as per view of ![alt text][1]

[1]: https://docs.google.com/leaf?id=0B3b1RiEgJA39OGE1NzFmNjktNTcxNi00OTc1LThiOWYtNTZkM2MzNjYxZWJj&hl=en, and the created code as the following...

Program Name : BankLoadView.java

/* * BankLoanView.java */

package bankloan;

import java.awt.Color; import org.jdesktop.application.Action; import org.jdesktop.application.ResourceMap; import org.jdesktop.application.SingleFrameApplication; import org.jdesktop.application.FrameView; import org.jdesktop.application.TaskMonitor; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.Timer; import javax.swing.Icon; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JTable;

/** * The application's main frame. */ public class BankLoanView extends FrameView {

public BankLoanView(SingleFrameApplication app) { super(app);

    initComponents();

    // status bar initialization - message timeout, idle icon and busy animation, etc
    ResourceMap resourceMap = getResourceMap();
    int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
    messageTimer = new Timer(messageTimeout, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            statusMessageLabel.setText("");
        }
    });
    messageTimer.setRepeats(false);
    int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
    for (int i = 0; i < busyIcons.length; i++) {
        busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
    }
    busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
            statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
        }
    });
    idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
    statusAnimationLabel.setIcon(idleIcon);
    progressBar.setVisible(false);

    // connecting action tasks to status bar via TaskMonitor
    TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
    taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
        public void propertyChange(java.beans.PropertyChangeEvent evt) {
            String propertyName = evt.getPropertyName();
            if ("started".equals(propertyName)) {
                if (!busyIconTimer.isRunning()) {
                    statusAnimationLabel.setIcon(busyIcons[0]);
                    busyIconIndex = 0;
                    busyIconTimer.start();
                }
                progressBar.setVisible(true);
                progressBar.setIndeterminate(true);
            } else if ("done".equals(propertyName)) {
                busyIconTimer.stop();
                statusAnimationLabel.setIcon(idleIcon);
                progressBar.setVisible(false);
                progressBar.setValue(0);
            } else if ("message".equals(propertyName)) {
                String text = (String)(evt.getNewValue());
                statusMessageLabel.setText((text == null) ? "" : text);
                messageTimer.restart();
            } else if ("progress".equals(propertyName)) {
                int value = (Integer)(evt.getNewValue());
                progressBar.setVisible(true);
                progressBar.setIndeterminate(false);
                progressBar.setValue(value);
            }
        }
    });
}

@Action
public void showAboutBox() {
    if (aboutBox == null) {
        JFrame mainFrame = BankLoanApp.getApplication().getMainFrame();
        aboutBox = new BankLoanAboutBox(mainFrame);
        aboutBox.setLocationRelativeTo(mainFrame);
    }
    BankLoanApp.getApplication().show(aboutBox);
}

/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    mainPanel = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    txtAmount = new javax.swing.JTextField();
    txtYears = new javax.swing.JTextField();
    txtInterest = new javax.swing.JTextField();
    btnDisplay = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    javax.swing.JTable jTable = new javax.swing.JTable();
    txtResult = new java.awt.TextArea();
    menuBar = new javax.swing.JMenuBar();
    javax.swing.JMenu fileMenu = new javax.swing.JMenu();
    javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
    javax.swing.JMenu helpMenu = new javax.swing.JMenu();
    javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
    statusPanel = new javax.swing.JPanel();
    javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
    statusMessageLabel = new javax.swing.JLabel();
    statusAnimationLabel = new javax.swing.JLabel();
    progressBar = new javax.swing.JProgressBar();

    mainPanel.setName("mainPanel"); // NOI18N

    org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(bankloan.BankLoanApp.class).getContext().getResourceMap(BankLoanView.class);
    jLabel1.setFont(resourceMap.getFont("txtYears.font")); // NOI18N
    jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
    jLabel1.setName("jLabel1"); // NOI18N

    jLabel2.setFont(resourceMap.getFont("lblAmt.font")); // NOI18N
    jLabel2.setText(resourceMap.getString("lblAmt.text")); // NOI18N
    jLabel2.setName("lblAmt"); // NOI18N

    jLabel3.setFont(resourceMap.getFont("txtYears.font")); // NOI18N
    jLabel3.setText(resourceMap.getString("lblYears.text")); // NOI18N
    jLabel3.setName("lblYears"); // NOI18N

    jLabel4.setFont(resourceMap.getFont("txtYears.font")); // NOI18N
    jLabel4.setText(resourceMap.getString("lblRate.text")); // NOI18N
    jLabel4.setName("lblRate"); // NOI18N

    txtAmount.setFont(resourceMap.getFont("txtYears.font")); // NOI18N
    txtAmount.setText(resourceMap.getString("txtAmt.text")); // NOI18N
    txtAmount.setName("txtAmt"); // NOI18N

    txtYears.setFont(resourceMap.getFont("txtYears.font")); // NOI18N
    txtYears.setText(resourceMap.getString("txtYears.text")); // NOI18N
    txtYears.setName("txtYears"); // NOI18N

    txtInterest.setFont(resourceMap.getFont("txtYears.font")); // NOI18N
    txtInterest.setText(resourceMap.getString("txtInterest.text")); // NOI18N
    txtInterest.setName("txtInterest"); // NOI18N

    btnDisplay.setFont(resourceMap.getFont("txtYears.font")); // NOI18N
    btnDisplay.setText(resourceMap.getString("btnDisplay.text")); // NOI18N
    btnDisplay.setName("btnDisplay"); // NOI18N
    btnDisplay.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnDisplayActionPerformed(evt);
        }
    });

    jScrollPane1.setName("jScrollPane1"); // NOI18N

    jTable.setFont(resourceMap.getFont("jTable.font")); // NOI18N
    jTable.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null}
        },
        new String [] {
            "Payment#", "Interest", "Principal", "Balance"
        }
    ) {
        Class[] types = new Class [] {
            java.lang.Object.class, java.lang.Float.class, java.lang.Float.class, java.lang.Double.class
        };

        public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
        }
    });
    jTable.setName("jTable"); // NOI18N
    jScrollPane1.setViewportView(jTable);
    jTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("jTable.columnModel.title0")); // NOI18N
    jTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("jTable.columnModel.title1")); // NOI18N
    jTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("jTable.columnModel.title2")); // NOI18N
    jTable.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("jTable.columnModel.title3")); // NOI18N

    txtResult.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
    txtResult.setName("txtResult"); // NOI18N

    javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
    mainPanel.setLayout(mainPanelLayout);
    mainPanelLayout.setHorizontalGroup(
        mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(mainPanelLayout.createSequentialGroup()
            .addGap(18, 18, 18)
            .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel1)
                .addGroup(mainPanelLayout.createSequentialGroup()
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel4)
                        .addComponent(jLabel2)
                        .addComponent(jLabel3))
                    .addGap(13, 13, 13)
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(txtInterest, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                        .addComponent(txtYears, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                        .addComponent(txtAmount, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE))))
            .addGap(28, 28, 28)
            .addComponent(txtResult, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(btnDisplay)
            .addGap(27, 27, 27))
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 687, Short.MAX_VALUE)
    );
    mainPanelLayout.setVerticalGroup(
        mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(mainPanelLayout.createSequentialGroup()
            .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnDisplay)
                    .addGap(48, 48, 48))
                .addGroup(mainPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(mainPanelLayout.createSequentialGroup()
                            .addGap(4, 4, 4)
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel3)
                                .addComponent(txtYears, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel4)
                                .addComponent(txtInterest, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGap(10, 10, 10))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtResult, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(19, 19, 19)))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(312, Short.MAX_VALUE))
    );

    menuBar.setName("menuBar"); // NOI18N

    fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
    fileMenu.setName("fileMenu"); // NOI18N

    javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(bankloan.BankLoanApp.class).getContext().getActionMap(BankLoanView.class, this);
    exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
    exitMenuItem.setName("exitMenuItem"); // NOI18N
    fileMenu.add(exitMenuItem);

    menuBar.add(fileMenu);

    helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
    helpMenu.setName("helpMenu"); // NOI18N

    aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
    aboutMenuItem.setName("aboutMenuItem"); // NOI18N
    helpMenu.add(aboutMenuItem);

    menuBar.add(helpMenu);

    statusPanel.setName("statusPanel"); // NOI18N

    statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

    statusMessageLabel.setName("statusMessageLabel"); // NOI18N

    statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
    statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

    progressBar.setName("progressBar"); // NOI18N

    javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
    statusPanel.setLayout(statusPanelLayout);
    statusPanelLayout.setHorizontalGroup(
        statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 687, Short.MAX_VALUE)
        .addGroup(statusPanelLayout.createSequentialGroup()
            .addContainerGap()
            .addComponent(statusMessageLabel)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 517, Short.MAX_VALUE)
            .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(statusAnimationLabel)
            .addContainerGap())
    );
    statusPanelLayout.setVerticalGroup(
        statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(statusPanelLayout.createSequentialGroup()
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(statusMessageLabel)
                .addComponent(statusAnimationLabel)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(3, 3, 3))
    );

    setComponent(mainPanel);
    setMenuBar(menuBar);
    setStatusBar(statusPanel);
}// </editor-fold>

private void btnDisplayActionPerformed(java.awt.event.ActionEvent evt) {                                           
    float amount = Float.parseFloat(txtAmount.getText());
    int year = Integer.parseInt(txtYears.getText());
    float interest = Float.parseFloat(txtInterest.getText());
    BankAccount bk = new BankAccount(amount, year, interest);
    String a = "      ";

    for (int  i = 0; i < year; i++) {
    bk.Calculate(amount, i, interest);

     txtResult.append(String.valueOf(bk.getBalance()+"\n"));    //String.valueOf()  changes double value into a string
    }
     // txtResult.setText(null);
}                                          

// Variables declaration - do not modify
private javax.swing.JButton btnDisplay;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JPanel mainPanel;
private javax.swing.JMenuBar menuBar;
private javax.swing.JProgressBar progressBar;
private javax.swing.JLabel statusAnimationLabel;
private javax.swing.JLabel statusMessageLabel;
private javax.swing.JPanel statusPanel;
private javax.swing.JTextField txtAmount;
private javax.swing.JTextField txtInterest;
private java.awt.TextArea txtResult;
private javax.swing.JTextField txtYears;
// End of variables declaration

private final Timer messageTimer;
private final Timer busyIconTimer;
private final Icon idleIcon;
private final Icon[] busyIcons = new Icon[15];
private int busyIconIndex = 0;

private JDialog aboutBox;

}


In the Class Name: BankAccount.java package bankloan;

public class BankAccount {
    private float amount;
    private int year;
    private float interest;
    private double balance;
    public BankAccount() {}
    public BankAccount(float Amt, int yr, float interest) {
        float Amount = Amt;
        int Year = yr;
        float Interest = interest;
}
public void Calculate (float Amt, int yr, float interest ) {
    float in = 1 + interest / 100;
    balance = Amt*( Math.pow(in, yr));
}
public double getBalance() { return balance; }

}

*End of Code


January 19, 2011 at 1:51 PM

The GUI View print screen as the following link...

https://docs.google.com/leaf?id=0B3b1RiEgJA39OGE1NzFmNjktNTcxNi00OTc1LThiOWYtNTZkM2MzNjYxZWJj&sort=name&layout=list&num=50

Please advise....

Ads









Related Tutorials/Questions & Answers:
How to collect Java input field value display into Jtable?
how to display data from database according to entered value in search field
Advertisements
Can not input value into text field which use sx:datetimepicker
Can not input value into text field which use sx:datetimepicker
how to display the value of termcell name in label
how to display textbox value based on selected option value?
java script display value - Java Beginners
how to get the value of a label field of option tag in javascript?
How to add radio button value in a table for particular field?
How to get enum value in Java
how to use dropdown list in JSP and display value in same page
how to display each arraylist value on new page in jsp
How to get the unicode of japanese character input in java
javascript value to hidden input
this is my file upload page, here i am not getting tags input field value to my servlet,please suggest a solution?
To store value in session & display it
How to add key value pair in Java
How to retrieve and display image from database in Java?
I need jsp code for how to write text field value into property file.
How to convert EBCDIC format value into ASCII format value in java
how to display?
How to read value from xml using java?
File Upload in struts2 - Invalid field value for field
how to display the nearest palaces for the given latitude and longitude value in google map in android
How to display data fom MySQL DataBase-table in to JSP file by submitting a value in combobox.
How to check if an arraylist contains a value in Java
Display of value in Tabel cell
how to display one image on jsp through java
How to Display an alert message when nothing is selected in jspinner in java?
how to display?
how to display?

Ads