Im writing the content of text field to a text file:
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingUtilities;
import java.io.FileNotFoundException;
import java.util.Formatter;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class Payroll extends JFrame implements ActionListener{
private JButton btn1;
private JButton btn2;
private JButton btnadd;
// initialize the lbl with caption name is employee information.
JLabel lbl = new JLabel("Nilai University Payroll System");
public Payroll(){
super("Nilai University Payroll System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
JLabel lblid,lblname,lblrank,lblclass,lbltype,lblpay;
JTextField txtid,txtname,txtrank,txtclass,txtEmployeeClass,txtEmployeeType;
JRadioButton rb1,rb2,rb3,rb4,rb5;
lbl.setBounds(200,50,500,100);
lbl.setHorizontalAlignment(lbl.CENTER );
lblid = new JLabel("Employee ID: ");
lblname = new JLabel("Employee Name: ");
lblrank = new JLabel("Employee Rank: ");
lblclass = new JLabel("Employee Class: ");
lbltype = new JLabel("Class Type: ");
lblpay = new JLabel("Employee Pay Rate: ");
// initialize all the label which are declared in the example above with its caption name
lblid.setBounds(300,140,100,20);
lblname.setBounds(300,180,100,20);
lblrank.setBounds(300,220,100,20);
lblclass.setBounds(300,260,100,20);
lbltype.setBounds(300,300,100,20);
lblpay.setBounds(300,340,100,20);
// add all the label on the frame
add(lblid);
add(lblname);
add(lblrank);
add(lblclass);
add(lbltype);
add(lblpay);
// initialize the text field with size
txtid=new JTextField(15);
txtname=new JTextField(15);
txtclass=new JTextField(15);
txtEmployeeClass=new JTextField(15);
txtEmployeeType=new JTextField(15);
// set a particular position on a screen with set bounds constructor
txtid.setBounds(400,140,100,20);
txtname.setBounds(400,180,100,20);
txtclass.setBounds(400,220,100,20);
txtEmployeeClass.setBounds(400,260,100,20);
txtEmployeeType.setBounds(400,300,100,20);
// add text field on a Frame
add(txtid);
add(txtname);
add(txtclass);
add(txtEmployeeClass);
add(txtEmployeeClass);
// initialize radio button with its caption
rb1 = new JRadioButton("1. Excellent");
rb2 = new JRadioButton("2. Good");
rb3 = new JRadioButton("3. Average");
rb4 = new JRadioButton("4. Fair");
rb5 = new JRadioButton("5. Poor");
// set a particular position on a Frame
rb1.setBounds(400,220,100,20);
rb2.setBounds(500,220,100,20);
rb3.setBounds(600,220,100,20);
rb4.setBounds(700,220,100,20);
rb5.setBounds(800,220,100,20);
// add button on a frame
add(rb1);
add(rb2);
add(rb3);
add(rb4);
add(rb5);
btnadd = new JButton("Add Employee");
btnadd.setToolTipText("Click this button to add employee details to a text file.");
btnadd.setBounds(400,320,150,20);
add(btnadd);
btnadd.addActionListener(this);
btnadd.setActionCommand("Add");
}
private BufferedWriter output;
public void actionPerformed(ActionEvent e){
String cmd = e.getActionCommand();
if(cmd.equals("Add"))
{
output.write("ID: "+txtid.getNume()+"\n");
}
}
public void WriteFile(){
try {
output = new BufferedWriter(new FileWriter("E:/EC3307/eclipse/Payroll.txt",true));
output.newLine();
output.close();
}
catch(IOException e)
{
System.out.println("There was a problem:" + e);
}
}
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run()
{
Payroll f1=new Payroll();
// set frame size
f1.setSize(1000,600);
// set frame visible true
f1.setVisible(true);
}
});
}
}
The problem is that txtid is a local variable of your Payroll constructor and not a member of the Payroll class. Simply declare txtid as variable of your Payroll class and this will allow to access it in your actionPerformed method.
Declare txtid outside of Payroll() constructor
Related
I have done all the details I have done, and I don't get an error but what I want to do doesn't work.
I tried to check on google, chatgpt, and friends. I was expected to a window with wait for input from the user, and the user writes a task and he enters "submit". Then, a window of DeadLine comes with an import calendar. So, the user chooses a DeadLine and he presses "OK". and then I created a constructor with the selected time and the task (to print the input) therefore, I called to the constructor and its doesn't call. Please help me. ):
The code:
import javax.swing.*;
import com.toedter.calendar.JCalendar;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.Date;
public class List extends JFrame {
String todo = "TO DO: ";
ImageIcon icon1;
JButton button;
public static String task;
static JLabel label,label2,label3;
static JLabel titletaskLabel,titletaskLabel2,titletaskLabel3,titletaskLabel4;
static JFrame frame;
public static JTextField text;
static Font myFont = new Font("Ink Free", Font.BOLD, 30);
static Font yourFont = new Font("Ink Free", Font.HANGING_BASELINE, 20);
static JCalendar calendar;
static Date selectedDate;
List() {
frame = new JFrame("List");
icon1 = new ImageIcon("list.png");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 900);
frame.setIconImage(icon1.getImage());
frame.setLayout(null);
label = new JLabel("To Do List: ");
label.setBounds(50, 50, 200, 20);
label2 = new JLabel("Enter a task: ");
label2.setFont(myFont);
label2.setBounds(70, 460, 200, 40);
button = new JButton("Submit");
button.setBounds(470, 500, 80, 50);
button.setBackground(Color.green);
button.addActionListener(new ActionListener1());
text = new JTextField();
text.setBounds(70, 500, 370, 50);
text.setFont(myFont);
frame.add(text);
frame.add(button);
frame.add(label);
frame.add(label2);
frame.setVisible(true);
}
static void addDeadline() {
JDialog dialog = new JDialog(frame, "Choose a Due Date", true);
dialog.setSize(400, 400);
dialog.setLocationRelativeTo(frame);
JPanel calendarPanel = new JPanel();
calendarPanel.setBounds(70, 600, 370, 200);
calendar = new JCalendar();
calendarPanel.add(calendar);
JButton okButton = new JButton("OK");
okButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// Get the selected date
Calendar selectedCalendar = calendar.getCalendar();
selectedDate = selectedCalendar.getTime();
task = text.getText();
System.out.println(selectedDate);
dialog.setVisible(false);
tasks(); // add the new task to the frame
}
});
calendarPanel.add(okButton);
dialog.add(calendarPanel);
dialog.setVisible(true);
}
static void tasks() {
titletaskLabel = new JLabel(task + "The DeadLine is: " + selectedDate);
titletaskLabel.setBounds(50, 70, 200, 50);
frame.add(titletaskLabel);
}
static class ActionListener1 implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
addDeadline();
}
}
}
public class Main extends List{
public static void main(String[] args) {
new List();
}
}
I'm trying to get a user to input a name in one panel on CardLayout and for the input to then define a JLabels text in the next panel that it switches to. I used System.out.println(userName); to double check that it was picking up the text and it definitely is but I don't know how to get the 'user' text to be that of the JTextFields input.
Full working code below:
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class gameDataPanel {
private JFrame frame = new JFrame("Game Data Input");
private JPanel mainPanel, loginPanel, gameDataPanel;
private JLabel playerName, user;
private JTextField playerNameInput;
private JButton submit;
private CardLayout gameDataLayout = new CardLayout();
private String userName;
public gameDataPanel() {
mainPanel = new JPanel();
mainPanel.setLayout(gameDataLayout);
playerName = new JLabel("Enter Player Name: ");
playerNameInput = new JTextField(30);
submit = new JButton("Submit");
loginPanel = new JPanel();
loginPanel.add(playerName);
loginPanel.add(playerNameInput);
loginPanel.add(submit);
gameDataPanel = new JPanel();
user = new JLabel();
user.setText(userName);
gameDataPanel.add(user);
mainPanel.add(loginPanel, "1");
mainPanel.add(gameDataPanel, "2");
gameDataLayout.show(mainPanel, "1");
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
userName = playerNameInput.getText();
gameDataLayout.show(mainPanel, "2");
System.out.println(userName);
}
});
playerNameInput.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
userName = playerNameInput.getText();
gameDataLayout.show(mainPanel, "2");
System.out.println(userName);
}
});
frame.add(mainPanel);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
}
public static void main(String[] args) {
gameDataPanel gd = new gameDataPanel();
}
}
Thanks
I am not 100% sure, whether i understood you correctly: You want to display the users name, which is entered in "playerNameInput" to be displayed in the JLabel "user" ? In this case, you have to update your JLabel object in your event listener AFTER the user entered something and pressed the push button. Try something like this:
playerNameInput.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
userName = playerNameInput.getText();
user.setText(userName);
gameDataLayout.show(mainPanel, "2");
System.out.println(userName);
}
});
I hope this helps :-)
I crated a Frame which have a panel inside it and the panel have a textarea inside it. Now i created a constructor which makes the frame visible for some time and after that it is set as invisible. Time for which it is visible it shows some massage.
When i run the constructor code inside the main method of outputDisplay class it shows the text massage
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.Timer;
public class OutputDisplay {
static JFrame frame;
JPanel panel;
JTextArea area;
Font font;
public void timer(int time){
Timer timer = new Timer(time, new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
}
});
timer.start();
timer.setRepeats(false);
}
OutputDisplay(String ip,int time) throws InterruptedException{
frame = new JFrame("Warning");
frame.setLocation(400, 220);
panel = new JPanel();
area = new JTextArea();
font = new Font("Aharoni", Font.BOLD, 16);
area.setFont(font);
area.setForeground(Color.RED);
area.setSize(200, 200);
int j=0;
String[] t = {ip};
for(int i=0;i<t.length;i++){
area.append(t[i]+"\n");
}//for
panel.add(area);
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.pack();
frame.setSize(600, 200);
frame.setVisible(true);
Thread.sleep(time);
j++;
if(j==1){
frame.setVisible(false);
}//if
frame.setResizable(false);
}//constructor
OutputDisplay(String dialogue,String path,int time) throws InterruptedException{
frame = new JFrame("Warning");
frame.setLocation(400, 220);
panel = new JPanel();
area = new JTextArea();
font = new Font("Aharoni", Font.BOLD, 16);
area.setFont(font);
area.setForeground(Color.MAGENTA);
area.setSize(200, 200);
int j=0;
String[] t = {dialogue};
for(int i=0;i<t.length;i++){
area.append(t[i]+"\n");
}//for
panel.add(area);
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.pack();
frame.setSize(500, 200);
frame.setVisible(true);
Thread.sleep(time);
j++;
if(j==1){
frame.setVisible(false);
}//if
frame.setResizable(false);
}//constructor
OutputDisplay(String dialogue) throws InterruptedException{
frame = new JFrame("Report");
frame.setLocation(400, 220);
panel = new JPanel();
area = new JTextArea();
font = new Font("Aharoni", Font.BOLD, 16);
area.setFont(font);
area.setForeground(Color.BLUE);
area.setSize(200, 200);
String[] t = {dialogue};
for(int i=0;i<t.length;i++){
area.append(t[i]+"\n");
}//for
panel.add(area);
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.pack();
frame.setSize(600, 600);
frame.setVisible(true);
frame.setResizable(false);
}//constructor
public OutputDisplay() {
}//no arg
public void dialogue (String massage) throws InterruptedException{
frame = new JFrame("Massage");
frame.setLocation(400, 220);
JPanel panel = new JPanel();
JTextArea area = new JTextArea();
Font font = new Font("Aharoni", Font.BOLD, 16);
area.setFont(font);
area.setForeground(Color.GREEN);
area.setSize(200, 200);
int j=0;
String[] t = {massage};
for(int i=0;i<t.length;i++){
area.append(t[i]+"\n");
}//for
panel.add(area);
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.pack();
frame.setSize(400, 100);
frame.setVisible(true);
Thread.sleep(8*1000);
j++;
if(j==1){
frame.setVisible(false);
}//if
frame.setResizable(false);
}//dialogue
static void warningPopUp (String massage){
JOptionPane.showMessageDialog(null, massage);
}//dialogue
public static void main(String[] args){
new OutputDisplay("We are checking for account validation"+"\n"
+ "If user id and password matches then we will goto websites for updation. " +"\n"
+ "You will get a complete report once we are done", 4*1000);
}//main
}//OutputDisplay
Don't call Thread.sleep(...) on the Swing EDT as that just puts your whole GUI to sleep.
Do use a Swing Timer instead.
Do Google and search this site for similar questions.
Please check out Lesson: Concurrency in Swing
Also check out the Swing Tutorials
Edit
You're still using Thread.sleep, again you shouldn't use it, but instead should use a Timer. Also, never set a JTextArea's size or preferred size ever. Do this and the scrollbars of your JScrollPane won't work. Instead, set its rows and columns.
import java.awt.Color;
import java.awt.Font;
import java.awt.Dialog.ModalityType;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class TimeLimitedDialogTester {
private static final int EB_GAP = 25;
private JPanel mainPanel = new JPanel();
public TimeLimitedDialogTester() {
mainPanel.add(new JButton(new ShowDialogAction("Show Dialog")));
mainPanel.setBorder(BorderFactory.createEmptyBorder(EB_GAP, 2 * EB_GAP, 2 * EB_GAP, 2 * EB_GAP));
}
public JPanel getMainPanel() {
return mainPanel;
}
#SuppressWarnings("serial")
private class ShowDialogAction extends AbstractAction {
public ShowDialogAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
#Override
public void actionPerformed(ActionEvent e) {
Window mainWin = SwingUtilities.getWindowAncestor(mainPanel);
int seconds = 5;
String dialogTitle = "WARNING";
String text = "We are checking for account validation. If user id and password matches then we"
+ " will go to websites for updating. You will get a complete report once we are done.";
TimeLimitedDialog timeLimitedDialog = new TimeLimitedDialog(mainWin, seconds, dialogTitle, text);
timeLimitedDialog.dialogShow();
}
}
private static void createAndShowGui() {
TimeLimitedDialogTester timeLimitedDialog = new TimeLimitedDialogTester();
JFrame frame = new JFrame("TimeLimitedDialog");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(timeLimitedDialog.getMainPanel());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class TimeLimitedDialog {
private static final int ROWS = 6;
private static final int COLS = 30;
private static final Color FG = Color.green.darker().darker();
private JDialog dialog;
private JTextArea displayArea = new JTextArea(ROWS, COLS);
private int seconds;
public TimeLimitedDialog(Window mainWin, int seconds, String dialogTitle, String text) {
displayArea.setWrapStyleWord(true);
displayArea.setLineWrap(true);
displayArea.setFocusable(false);
displayArea.setText(text);
displayArea.setForeground(FG);
displayArea.setFont(displayArea.getFont().deriveFont(Font.BOLD));
this.seconds = seconds;
dialog = new JDialog(mainWin, dialogTitle, ModalityType.APPLICATION_MODAL);
dialog.add(new JScrollPane(displayArea));
dialog.pack();
dialog.setLocationRelativeTo(mainWin);
}
public void dialogShow() {
new Timer(seconds * 1000, new TimerListener()).start();
dialog.setVisible(true);
}
private class TimerListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
if (dialog != null && dialog.isVisible()) {
dialog.dispose();
}
((Timer) e.getSource()).stop();
}
}
}
I've to JTables. I drag a row from the first table and drop it to the second.
The DnD operation works fine so far, but how can easily refresh the second table after
dropping operation? I've implemented a TableModelListener, but it works only when I
double click on a line of a table.
My question: which event listener do I need to solve my problem? Any solutions or examples?
btw: the DnD operation is performing with the tranferHandler
ok here's some code:
TableExample for creating the to tables
ListHandler1 for DnD operations
SearchRenderer for updating row heights after DnD or table changings
please keep in mind that i wrote quickly. ..
Thanks and regards!
TableExample:
package GUI_Examples;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.*;
public class TableExample implements TableModelListener{
String [] title = new String [] {"Title A", "Title B"};
String [] title2 = new String [] {"Title C", "Title D"};
Object [][] data = new String [][] {{"aaaaaaaaaaaa aaaaaa aaaaaaa", "bbbbbbbb bbbb bbbbbb bbbbbb"},
{"cccccccccc cccccccc ccccccc", "ddddddd ddd dddddddd dddddd"},
{"eeeeeeeeee eeeeeeee eeeeeee", "fffffff ffff ffffff fffffff"}};
Object [][] data2 = new String [][] {{"",""}};
private JTable table;
private JTable table2;
private JFrame frame;
private DefaultTableModel model;
private DefaultTableModel model2;
private JScrollPane pane1;
private JScrollPane pane2;
private SearchRenderer1 myRenderer;
private SearchRenderer1 myRenderer2;
TableExample() {} //constructor
public JPanel createTable() {
JPanel panel = new JPanel();
//creating tables and table models
model = new DefaultTableModel(data, title);
model2 = new DefaultTableModel(data2, title2);
table = new JTable(model);
table2 = new JTable(model2);
//setting renderers
myRenderer = new SearchRenderer1();
table.setDefaultRenderer(String.class, myRenderer);
table.getColumnModel().getColumn(0).setCellRenderer(myRenderer);
table.getColumnModel().getColumn(1).setCellRenderer(myRenderer);
myRenderer2 = new SearchRenderer1();
table2.setDefaultRenderer(String.class, myRenderer2);
table2.getColumnModel().getColumn(0).setCellRenderer(myRenderer2);
table2.getColumnModel().getColumn(1).setCellRenderer(myRenderer2);
//setting sizes
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.getColumnModel().getColumn(0).setPreferredWidth(60);
table.getColumnModel().getColumn(1).setPreferredWidth(60);
table2.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table2.getColumnModel().getColumn(0).setPreferredWidth(60);
table2.getColumnModel().getColumn(1).setPreferredWidth(60);
//Drag&Drop operations
table.setDragEnabled(true);
table2.setDropMode(DropMode.INSERT);
table2.setTransferHandler(new ListHandler1(model2));
pane1 = new JScrollPane(table);
pane2 = new JScrollPane(table2);
pane1.setPreferredSize(new Dimension(150,300));
pane2.setPreferredSize(new Dimension(150,300));
updateRowHeights();
updateRowHeights2();
panel.add(pane1);
panel.add(pane2);
return panel;
}
void showTable() {
//create and show frame
JPanel testPanel = createTable();
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(testPanel);
frame.pack();
frame.setVisible(true);
}//showTable
void updateRowHeights() {
for (int row = 0; row < table.getRowCount(); row++) {
int rowHeight = table.getRowHeight();
Component comp = table.prepareRenderer(table.getCellRenderer(row, 1), row, 1);
rowHeight = Math.max(rowHeight, comp.getPreferredSize().height);
table.setRowHeight(row, rowHeight);
}
}
void updateRowHeights2() {
for (int row = 0; row < table2.getRowCount(); row++) {
int rowHeight = table2.getRowHeight();
Component comp2 = table2.prepareRenderer(table2.getCellRenderer(row, 1), row, 1);
rowHeight = Math.max(rowHeight, comp2.getPreferredSize().height);
table2.setRowHeight(row, rowHeight);
}
}
public static void main(String[] args) {
TableExample example = new TableExample();
example.showTable();
}//main
#Override
public void tableChanged(TableModelEvent e) {
updateRowHeights();
updateRowHeights2();
}
}//TableExample
ListHandler
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import javax.swing.TransferHandler;
import javax.swing.table.DefaultTableModel;
public class ListHandler1 extends TransferHandler {
DefaultTableModel model = new DefaultTableModel();
ListHandler1(DefaultTableModel tableModel) {
this.model=tableModel;
} //constructor
/**
*
*/
private static final long serialVersionUID = 1L;
/* canImport: This method tests suitability of a drop operation. Here we filter out the clipboard paste operations
* and allow only String drop operations. If the method returns false, the drop operation is cancelled.(non-Javadoc)
* #see javax.swing.TransferHandler#canImport(javax.swing.TransferHandler.TransferSupport)
*/
public boolean canImport(TransferSupport support) {
if (!support.isDrop()) {
return false;
}
return support.isDataFlavorSupported(DataFlavor.stringFlavor);
}
/*The importData() method transfers the data from the clipboard or from the drag and drop operation to the drop location.
* (non-Javadoc)
* #see javax.swing.TransferHandler#importData(javax.swing.TransferHandler.TransferSupport)
*/
public boolean importData(TransferSupport support) {
if (!canImport(support)) {
return false;
}
Transferable transferable = support.getTransferable(); //The Transferable is the class, where the data is bundled.
String line;
try {
line = (String) transferable.getTransferData(DataFlavor.stringFlavor); //We retrieve our data.
} catch (Exception e) {
return false;
}
String item [] = line.split("\t");
model.addRow(new Object[]{item[0],item[1]});
return true;
}
}
SearchRenderer:
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class SearchRenderer1 extends JTextArea implements TableCellRenderer {
private static final long serialVersionUID = 1L;
public SearchRenderer1() {
setLineWrap(true);
setWrapStyleWord(true);
} //constructor
public Component getTableCellRendererComponent (JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column ) {
this.setText((String)value);
//for update row height
this.setSize(table.getColumnModel().getColumn(column).getWidth(),Short.MAX_VALUE);
if (isSelected) {
this.setBackground(new java.awt.Color(255, 240, 200));
}
else {
this.setBackground(new java.awt.Color(255, 255, 255));
}
return this;
}//getTableCellRendererComponent
}
I want to perform some calculation by clicking in convert button and the answer will be displayed in the number text field.
also, I have an Exception , I did not know how to solve it
this is the code:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyEvent implements ActionListener{
public void actionPerformed(ActionEvent e){
System.out.println("Button Clicked ");
System.exit(0);
}
}
class Clacluting{
public static void main(String ar[]) throws IOException{
Frame frm = new Frame("TASK # 3");
TextField txt[] = { new TextField(15), new TextField(9),
};
Label lab[] ={new Label("number"), new Label("Amount")};
Button b = new Button("Convert");
Button b2 = new Button("Reset");
frm.setSize(200,250);
frm.setLayout(new FlowLayout());
double calculat=0;
calculat= Double.parseDouble(txt[0].getText()) * 0.381793;
txt[1].setText(String.valueOf(calculat));
b.addActionListener(new MyEvent());
b2.addActionListener(new MyEvent());
frm.setSize(200,250);
frm.add( lab[0]);
frm.add(txt[0]);
frm.add( lab[1]);
frm.add( txt[1]);
frm.add(b);
frm.add(b2);
frm.setVisible(true);
}
}