I want to convert my project that extends JFrame in an applet in order to run it on a web page. Can someone help me and tell me how I can do this?
Post here a fragment code of the project:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import CLIPSJNI.Environment;
import CLIPSJNI.PrimitiveValue;
import javax.swing.JTextPane;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.util.StringTokenizer;
import java.awt.Color;
import javax.swing.SwingConstants;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Progetto extends JFrame {
static Object ProfileData[] = new Object[30];
static Environment clips=new Environment();
static PrimitiveValue rv;
static String a="?*send*";
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
clips.load("prova.clp");
clips.reset();
clips.run();
Progetto frame = new Progetto();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
* #throws Exception
*/
public Progetto() throws Exception {
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 30, 662, 671);
contentPane = new JPanel();
contentPane.setBackground(Color.LIGHT_GRAY);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
rv=clips.eval(a);
ProfileData=(Object[]) Progetto.codificaCodice(rv.stringValue());
System.out.println((String) Progetto.ProfileData[0]);
JLabel lblTravel = new JLabel("Travel Recommender");
lblTravel.setForeground(new Color(204, 0, 0));
lblTravel.setFont(new Font("Trebuchet MS", Font.BOLD | Font.ITALIC, 30));
lblTravel.setHorizontalAlignment(SwingConstants.CENTER);
lblTravel.setBounds(-29, 11, 636, 49);
contentPane.add(lblTravel);
JButton range1 = new JButton("0 - 18");
range1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
range1.setForeground(Color.BLACK);
range1.setFont(new Font("Trebuchet MS", Font.PLAIN, 15));
range1.setBackground(Color.CYAN);
range1.setBounds(176, 221, 89, 31);
contentPane.add(range1);
JTextPane txtpnFsd = new JTextPane();
txtpnFsd.setToolTipText("");
contentPane.add(txtpnFsd);
txtpnFsd.setForeground(Color.BLACK);
txtpnFsd.setBackground(Color.LIGHT_GRAY);
txtpnFsd.setFont(new Font("Trebuchet MS", Font.BOLD | Font.ITALIC, 15));
txtpnFsd.setEditable(false);
txtpnFsd.setBounds(213, 134, 139, 76);
txtpnFsd.setText((String) ProfileData[0]);
range1.addMouseListener(new java.awt.event.MouseAdapter(){
public void mouseClicked(MouseEvent e){
Progetto.clips.eval((String) Progetto.ProfileData[1]); //assert 0-18
Progetto.clips.run();
Progetto.rv=Progetto.clips.eval(a);
try {
Progetto.ProfileData=(Object[]) Progetto.codificaCodice(Progetto.rv.stringValue());
System.out.println((String) Progetto.ProfileData[0]);
} catch (Exception e1) {
e1.printStackTrace();
}
Lavoro frame = null;
try {
frame= new Lavoro();
} catch (Exception e1) {
e1.printStackTrace();
}
frame.setVisible(true);
dispose();
}
});
......
public static Object codificaCodice(String message) {
Object token = null;
int count = 0;
Object ProfileData[] = new Object[30];
for(StringTokenizer st = new StringTokenizer(message, "%"); st.hasMoreTokens(); count++)
{
token = st.nextToken();
ProfileData[count] = (String) token;
}
return ProfileData;
}
}
Related
I am currently trying to write a simple program that will display musical notes at a given interval in BPM. I have most of the code written, but I can't figure out how to get the labels containing the note and sharp/flat to update every interval given. This is my code so far. The NoteGenerator object is of my own creation and is just used to generate a random note/sharp/flat and keep track of tempo. I have already tested the NoteGenerator class and it works exactly how I expect it to. Thanks in advance!
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JSpinner;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.SpinnerNumberModel;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class NotePractice extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
NotePractice frame = new NotePractice();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public NotePractice() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
NoteGenerator noteGen = new NoteGenerator();
JSpinner spinner = new JSpinner();
spinner.setModel(new SpinnerNumberModel(new Integer(100), new Integer(20), null, new Integer(5)));
spinner.setBounds(211, 196, 60, 20);
contentPane.add(spinner);
JLabel lblBpm = new JLabel("BPM");
lblBpm.setFont(new Font("Arial", Font.BOLD, 12));
lblBpm.setBounds(175, 198, 26, 14);
contentPane.add(lblBpm);
JLabel lblNote = new JLabel(noteGen.getNote());
lblNote.setFont(new Font("Arial", Font.BOLD, 99));
lblNote.setBounds(175, 60, 82, 104);
contentPane.add(lblNote);
JLabel lblStep = new JLabel(noteGen.getStep());
lblStep.setFont(new Font("Arial", Font.BOLD, 70));
lblStep.setBounds(258, 93, 60, 58);
contentPane.add(lblStep);
JButton btnUpdateTempo = new JButton("Update Tempo");
btnUpdateTempo.setFont(new Font("Arial", Font.PLAIN, 11));
btnUpdateTempo.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
noteGen.setBPM(spinner.getComponentCount());
Timer t = new Timer(noteGen.getBPM(), new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
lblNote.setText(noteGen.getNote());
lblStep.setText(noteGen.getStep());
contentPane.updateUI();
}
});
}
});
btnUpdateTempo.setBounds(168, 227, 103, 23);
contentPane.add(btnUpdateTempo);
}
}
I create a java application and now I have to reload a web page. I open the web page with this code:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import javax.swing.JProgressBar;
import java.io.*;
import java.util.*;
public class views extends JFrame {
private JPanel contentPane;
private JTextField txtHttpswwwyoutubecom;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
views frame = new views();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public views() {
setTitle("Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 707, 485);
setResizable(false);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblPutTheUrl = new JLabel("Put the url of the video");
lblPutTheUrl.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblPutTheUrl.setBounds(40, 93, 159, 24);
contentPane.add(lblPutTheUrl);
JLabel lblSelectTheNumber = new JLabel("Select the number");
lblSelectTheNumber.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblSelectTheNumber.setBounds(40, 200, 203, 24);
contentPane.add(lblSelectTheNumber);
final JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[] {"5", "10", "15", "20"}));
comboBox.setFont(new Font("Tahoma", Font.PLAIN, 15));
comboBox.setBounds(322, 202, 121, 20);
contentPane.add(comboBox);
txtHttpswwwyoutubecom = new JTextField();
txtHttpswwwyoutubecom.setText("https://www.google.com/");
txtHttpswwwyoutubecom.setFont(new Font("Tahoma", Font.PLAIN, 15));
txtHttpswwwyoutubecom.setBounds(322, 93, 332, 24);
contentPane.add(txtHttpswwwyoutubecom);
txtHttpswwwyoutubecom.setColumns(10);
JLabel lblPressOk = new JLabel("Press Ok ");
lblPressOk.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblPressOk.setBounds(40, 298, 71, 24);
contentPane.add(lblPressOk);
JButton btnOk = new JButton("Ok");
btnOk.addActionListener(new ActionListener() {
private String views;
private int intViews;
public void actionPerformed(ActionEvent arg0) {
//trasformare il numero delle views in una stringa
views = comboBox.getSelectedItem().toString();
//trasformo stringa in int
intViews = Integer.parseInt(views);
System.out.println(intViews);
//campo url
String urls = txtHttpswwwyoutubecom.getText();
//apertura url
int cont=0;
int cont1=0;
//cont=intViews;
//System.out.println("cont");
//System.out.println(intViews);
/*
try {
Thread.sleep(millisecondi);
}
catch (Exception e) {}
*/
while(intViews>cont){
cont++;
cont1++;
String URL = urls;
//String URL = "https://www.google.com/";
try {
java.awt.Desktop.getDesktop().browse(java.net.URI.create(URL));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//System.exit(0);
}
if(cont1==5){
cont1=0;
try {
Thread.sleep(5500);
}
catch (Exception e1) {}
}
}
}
});
btnOk.setFont(new Font("Tahoma", Font.PLAIN, 15));
btnOk.setBounds(322, 299, 121, 23);
contentPane.add(btnOk);
JLabel lblDevelopedByRiccardo = new JLabel("Developed by Riccardo Vecchiato");
lblDevelopedByRiccardo.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblDevelopedByRiccardo.setBounds(464, 421, 237, 24);
contentPane.add(lblDevelopedByRiccardo);
JLabel lblAlpha = new JLabel("Alpha 1.0");
lblAlpha.setBounds(10, 428, 46, 14);
contentPane.add(lblAlpha);
}
}
Every 5 seconds I open five new web page. I open that web page using this code:
String URL = "https://www.google.com/";
try {
java.awt.Desktop.getDesktop().browse(java.net.URI.create(URL));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
There is a command that reload all the web page that I create with this code?
Do not use Thread.sleep() as it will freeze your Swing application.
Instead you should use a javax.swing.Timer.
See the Java tutorial How to Use Swing Timers and Lesson: Concurrency in Swing for more information and examples.
Issues:
When I keep contentType of textpane as text/html, then the text typed moves to the right, for ex : if type "asd", then 'Message:' appears on the left and 'asd' appears on the right of the textpane
When adding a file, how do I write a HyperLinkListener for this. I've commented 'Save' and 'Cancel' in the code. How do I get the name of what I click (Cancel or Save), so that I can write appropriate code for these two links?
Code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import java.awt.FlowLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import javax.swing.event.HyperlinkListener;
import javax.swing.event.HyperlinkEvent;
public class SampleTextPane extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextPane textPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SampleTextPane frame = new SampleTextPane();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public SampleTextPane() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(new BorderLayout(0, 0));
JPanel panel_1 = new JPanel();
panel.add(panel_1, BorderLayout.CENTER);
panel_1.setLayout(new BorderLayout(0, 0));
textPane = new JTextPane();
textPane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent arg0) {
// Cancel
// To cancel the progress
// Save
// To save the file
}
});
textPane.setEditable(false);
textPane.setContentType("text/html");
panel_1.add(textPane);
JPanel panel_2 = new JPanel();
panel.add(panel_2, BorderLayout.SOUTH);
panel_2.setLayout(new BorderLayout(0, 0));
textField = new JTextField();
textField.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent key) {
if(key.getKeyCode()==KeyEvent.VK_ENTER){
if(!textField.getText().trim().isEmpty()){
sendMessage(textField.getText());
textField.setText("");
}
}
}
});
panel_2.add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("+");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser fileChoose = new JFileChooser();
int returnVal = fileChoose.showOpenDialog(getParent());
if(returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChoose.getSelectedFile();
String desc = file.getName().trim();
sendFileToUser(file, desc);
}
}
});
panel_2.add(btnNewButton, BorderLayout.EAST);
}
protected void sendFileToUser(File file, String desc) {
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet set = new SimpleAttributeSet();
StyleConstants.setForeground(set, Color.BLACK);
StyleConstants.setBold(set, true);
StyleConstants.setAlignment(set, StyleConstants.ALIGN_LEFT);
try {
doc.insertString(doc.getLength(), " File : "+desc, set);
HTMLEditorKit editorKit = (HTMLEditorKit)textPane.getEditorKit();
HTMLDocument doc1 = (HTMLDocument)textPane.getDocument();
try {
editorKit.insertHTML(doc1, doc.getLength(), "Save Cancel\n", 0, 0, null);
} catch (BadLocationException | IOException e) {
e.printStackTrace();
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
protected void sendMessage(String text) {
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet set = new SimpleAttributeSet();
StyleConstants.setForeground(set, Color.BLACK);
StyleConstants.setBold(set, true);
StyleConstants.setAlignment(set, StyleConstants.ALIGN_LEFT);
try {
doc.insertString(doc.getLength(), " Message : ", set);
doc.insertString(doc.getLength(), text+"\n", textPane.getStyle(StyleContext.DEFAULT_STYLE));
} catch (BadLocationException e) {
e.printStackTrace();
}
}
}
How do I get the name of what I click (Cancel or Save),
Did you specify the URL for the Hyperlink?
The JEditorPane API show an example of how to access the URL from the HyperlinkEvent.
So for class I'm suppose to make a Celsius to Fahrenheit converter GUI. With that said, it's a pretty simple and easy program to create. I want to do more with it though. What I want to do is have one window that changes depending on which radio button is selected in a buttongroup. I want the selected radiobutton for "To Fahrenheit" or "To Celsius" to update the labels for the input and output. I have tried using an actionlistener for when one is selected, but it doesn't change the labels. Am I using the wrong listener? What's the correct way to do this?
Here's my code:
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
public class Converter extends JFrame {
private JPanel contentPane;
private JTextField tfNumber;
private JLabel lblCelsius;
private JLabel lblFahrenheit;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Converter frame = new Converter();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Converter() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
setTitle("Celsius Converter");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 308, 145);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
tfNumber = new JTextField();
tfNumber.setText("0");
tfNumber.setBounds(10, 11, 123, 20);
contentPane.add(tfNumber);
tfNumber.setColumns(10);
lblCelsius = new JLabel("Celsius");
lblCelsius.setFont(new Font("Tahoma", Font.BOLD, 15));
lblCelsius.setBounds(143, 12, 127, 14);
contentPane.add(lblCelsius);
lblFahrenheit = new JLabel("Fahrenheit");
lblFahrenheit.setBounds(187, 46, 95, 14);
contentPane.add(lblFahrenheit);
final JLabel lblNum = new JLabel("32.0");
lblNum.setBounds(143, 46, 43, 14);
contentPane.add(lblNum);
final JRadioButton rdbtnF = new JRadioButton("to Fahrenheit");
rdbtnF.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(rdbtnF.isSelected()) {
lblCelsius.setText("Celsius");
lblFahrenheit.setText("Fahrenheit");
}
}
});
rdbtnF.setSelected(true);
rdbtnF.setBounds(10, 72, 109, 23);
contentPane.add(rdbtnF);
final JRadioButton rdbtnC = new JRadioButton("to Celsius");
rdbtnC.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(rdbtnF.isSelected()) {
lblCelsius.setText("Fahrenheit");
lblFahrenheit.setText("Celsius");
}
}
});
rdbtnC.setBounds(121, 72, 109, 23);
contentPane.add(rdbtnC);
ButtonGroup bg = new ButtonGroup();
bg.add(rdbtnF);
bg.add(rdbtnC);
JButton btnConvert = new JButton("Convert");
btnConvert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(rdbtnF.isSelected()) {
String num = String.valueOf(convertToF(tfNumber.getText()));
lblNum.setText(num);
}
if(rdbtnC.isSelected()) {
String num = String.valueOf(convertToC(tfNumber.getText()));
lblNum.setText(num);
}
}
});
btnConvert.setBounds(10, 42, 123, 23);
contentPane.add(btnConvert);
}
private double convertToF(String celsius) {
double c = Double.parseDouble(celsius);
double fahren = c * (9/5) + 32;
return fahren;
}
private double convertToC(String fahren) {
double f = Double.parseDouble(fahren);
double celsius = (f - 32) * 5 / 9;
return celsius;
}
}
I am trying to program an 'Inch-to-Centimeter Calculator'. I've got a problem with the method umrechnen(). The label lblCenti cannot be resolved. My Code is equal to the Solutioncode. I am grateful for every answer or tip I get.
I don't know what I should add to my Description, but StackOverflow forces me to write more, so I'm writing this.
package gui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.text.DecimalFormat;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class ZollZentimeter extends JFrame {
private JPanel contentPane;
private JTextField tfInch;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ZollZentimeter frame = new ZollZentimeter();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ZollZentimeter() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 359, 157);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnCalculate = new JButton("Umrechnen");
btnCalculate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
umrechnen();
}
});
btnCalculate.setBounds(12, 77, 116, 25);
contentPane.add(btnCalculate);
JButton btnEnde = new JButton("Ende");
btnEnde.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnEnde.setBounds(214, 77, 116, 25);
contentPane.add(btnEnde);
JLabel lblZoll = new JLabel("Zoll");
lblZoll.setBounds(12, 13, 56, 16);
contentPane.add(lblZoll);
tfInch = new JTextField();
tfInch.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER){
umrechnen();
}
}
});
JLabel lblCenti = new JLabel("");
lblCenti.setBounds(214, 42, 116, 22);
contentPane.add(lblCenti);
tfInch.setBounds(12, 42, 116, 22);
contentPane.add(tfInch);
tfInch.setColumns(10);
}
private void umrechnen(){
DecimalFormat f=new DecimalFormat("#0.00");
double z, cm;
z=Double.parseDouble(tfInch.getText());
cm=z*2.54;
lblCenti.setText(f.format(cm+" cm"));
tfInch.requestFocus();
tfInch.selectAll();
}
}
JLabel lblCenti = new JLabel("");
"lblCenti" is defined as a local variable so it can only be accessible in the method/constructor where you define it.
It you want to access the label in another method you need to define it as an instance variable, the same way you do with the "tfInch" variable.