how can i add text "shutdown " to textfield1
and exit frame5 when i press button22 in frame4 ????
JButton jButton22 = new JButton();
JButton jButton23 = new JButton();
JButton jButton24 = new JButton();
public Frame4() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
titledBorder1 = new TitledBorder("");
border1 = BorderFactory.createLineBorder(new Color(164, 225, 164),2);
border2 = BorderFactory.createLineBorder(new Color(164, 225, 164),2);
border3 = BorderFactory.createLineBorder(new Color(94, 85, 50),2);
this.getContentPane().setBackground(new Color(63, 138, 232));
this.setSize(new Dimension(400, 345));
this.setLocation(150,150);
this.getContentPane().setLayout(null);
jPanel1.setBackground(new Color(211, 229, 250));
jPanel1.setBorder(border3);
jPanel1.setBounds(new Rectangle(4, 47, 389, 193));
jPanel1.setLayout(null);
jLabel1.setFont(new java.awt.Font("Dialog", 0, 30));
jLabel1.setForeground(new Color(255, 137, 27));
jLabel1.setAlignmentY((float) 0.5);
jLabel1.setText(" server side ®™");
jLabel1.setBounds(new Rectangle(-44, 0, 314, 54));
jLabel2.setFont(new java.awt.Font("Dialog", 1, 15));
jLabel2.setForeground(new Color(60, 193, 60));
jLabel2.setText("status is connected");
jLabel2.setBounds(new Rectangle(107, 11, 165, 31));
jLabel4.setBounds(new Rectangle(252, 160, 124, 42));
jButton20.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton20_actionPerformed(e);
}
});
jButton22.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton22_actionPerformed(e);
}
});
jButton23.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton23_actionPerformed(e);
}
});
jButton24.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton24_actionPerformed(e);
}
});
void jButton20_actionPerformed(ActionEvent e) {
new Frame3().setVisible(true);
this.setVisible(false);
}
void jButton21_actionPerformed(ActionEvent e) {
System.exit(0);
}
void jButton22_actionPerformed(ActionEvent e) {
new Frame5().setVisible(true);
this.setVisible(false);
}
void jButton23_actionPerformed(ActionEvent e) {
new Frame5().setVisible(true);
this.setVisible(false);
}
void jButton24_actionPerformed(ActionEvent e) {
new Frame5().setVisible(true);
this.setVisible(false);
}
}
Your question is little bit confusing.
I think your question is to change the text of a textfield of another frame from the current frame.
If it is so, then you can do it simply by using functions. For example,
If you have two frames frame4 and frame5, the frame4 contains a button (say button4) and frame5 contains a textfield (say jtextfield).
You want to change the text of jtextfield in frame5 when you click the button4 in frame4.
Write a function in frame5 like,
public void change(String text) {
jtextfield.setText(text)
}
Then for changing the text from frame4, just call that function "change" .ie,
frame5 f=new frame5();
f.change("Shutdown");
where "shutdown" is the text you want to add.
Related
I am writing a music player that can be played in background while users do their thing. The problem is when I exit the music player panel into other panel, the music can't be stopped anymore.
Here is my code
JButton btnAddMusic = new JButton("add music");
btnAddMusic.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
returnValue=browser.showOpenDialog(main);
if(returnValue==browser.APPROVE_OPTION){
selectedFile=browser.getSelectedFile();
String filepath=selectedFile.toString();
String filename=selectedFile.getName();
main.getController().storeMusic(filename, filepath);
populateJComboBox();
}
}
});
btnAddMusic.setBounds(15, 187, 115, 29);
add(btnAddMusic);
JButton btnlogout = new JButton("");
btnlogout.setFont(new Font("Tw Cen MT Condensed Extra Bold", Font.PLAIN, 19));
btnlogout.setBackground(UIManager.getColor("Button.background"));
Image aimg= new ImageIcon(this.getClass().getResource("/logot.png")).getImage();
Image aImage = aimg.getScaledInstance(55, 55, Image.SCALE_DEFAULT);
btnlogout.setIcon(new ImageIcon(aImage));
btnlogout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
main.showRegisterPanel();
}
});
btnlogout.setForeground(UIManager.getColor("Button.foreground"));
btnlogout.setBackground(Color.WHITE);
btnlogout.setOpaque(true);
btnlogout.setBorderPainted(false);
btnlogout.setBounds(411, 0, 64, 53);
add(btnlogout);
JLabel icon = new JLabel("");
icon.setBounds(15, 16, 93, 81);
Image img= new ImageIcon(this.getClass().getResource("/tplogo.png")).getImage();
Image newImage = img.getScaledInstance(70, 70, Image.SCALE_DEFAULT);
icon.setIcon(new ImageIcon(newImage));
add(icon);
JLabel lblNewLabel = new JLabel("Music Player");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 20));
lblNewLabel.setBounds(155, 29, 276, 53);
add(lblNewLabel);
this.btnhome = new JButton("");
btnhome.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
check();
}
});
btnhome.setBackground(Color.WHITE);
btnhome.setBounds(180, 232, 75, 63);
Image home= new ImageIcon(this.getClass().getResource("/home.png")).getImage();
Image ahome = home.getScaledInstance(55, 55, Image.SCALE_DEFAULT);
btnhome.setIcon(new ImageIcon(ahome));
btnhome.setForeground(UIManager.getColor("Button.foreground"));
btnhome.setBackground(Color.WHITE);
btnhome.setOpaque(true);
btnhome.setBorderPainted(false);
add(btnhome);
JButton btnPlay = new JButton("play");
btnPlay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
for(int i=0;i<1024;i++){
try{
if(comboBox.getSelectedIndex()==i){
sound=new File(music[comboBox.getSelectedIndex()]);
ais=AudioSystem.getAudioInputStream(sound);
clip=AudioSystem.getClip();
clip.open(ais);
clip.start();
}
}catch(Exception e){
}
}
}});
btnPlay.setBounds(170, 187, 115, 29);
add(btnPlay);
JButton btnStop = new JButton("stop");
btnStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clip.stop();
}
});
btnStop.setBounds(320, 187, 115, 29);
add(btnStop);
browser.setFileFilter(filter);
this.comboBox = new JComboBox();
comboBox.setBounds(77, 111, 262, 26);
add(comboBox);
populateJComboBox();
}
private void populateJComboBox() {
this.mu = this.main.getController().getAllMusic(); //get hardcoded cluster details
DefaultComboBoxModel model = new DefaultComboBoxModel();
for(int i = 0; i<mu.length; i++) // for loop to add the cluster details into the comboBox model
{
Music clu = mu[i];
model.addElement(clu.getMname());
music[i]=clu.getMc();
}
this.comboBox.setModel(model); //setting the comboBox model with the model with cluster details added
}
private void check(){
this.user = this.main.getController().getAllUsers();
for(int i = 0; i<user.length;i++)
{
User use = user[i];
String aa = use.getType();
if(aa=="Student"){
main.showSthomepanel();
}
else if(aa=="Staff"){
main.showshomePanel();
}
else{
btnhome.setEnabled(false);
}
}
The initialise is done, but I can't post because of this weird code system.
This problem is caused by the focus being taken away from the panel. The easiest way to fix this is to use a JWindow instead of a JPanel.
Can I call a method from an extended class by pressing a JButton?
I know how to display messages etc when pressing a JButton, but I want to know if it is possible to do this?
I want to call methods from Activate_and_Deactivate class to use as action performed by pressing radiobuttons ON and OF. I can directly place the statements in the actionevent argument but the teacher said I have to use inheritance atleast for something.
this is my code.
package Calculators;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Button;
import java.awt.Color;
import javax.swing.JLabel;
import com.jgoodies.forms.factories.DefaultComponentFactory;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EtchedBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.JRadioButton;
public class UIFrame {
Activate_and_Deactivate act = new Activate_and_Deactivate();
JButton button0 = new JButton("0");
JButton button2 = new JButton("2");
JButton button1 = new JButton("1");
JButton button3 = new JButton("3");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
JButton button6 = new JButton("6");
JButton button7 = new JButton("7");
JButton button8 = new JButton("8");
JButton button9 = new JButton("9");
JButton button_Div = new JButton("/");
JButton button_C = new JButton("C");
JButton button_Mul = new JButton("*");
JButton button_Add = new JButton("+");
JButton button_Sub = new JButton("-");
JButton button_Dot = new JButton(".");
JButton button_rec = new JButton("1/x");
JButton button_Sqr = new JButton("\u221A");
JButton button_neg = new JButton("±");
JButton button_per = new JButton("%");
JButton button_equal = new JButton("=");
JButton button_undo = new JButton("\u2190");
JButton btnExit = new JButton("exit");
JRadioButton rdbtnOn = new JRadioButton("On");
JRadioButton rdbtnOff = new JRadioButton("Off");
private JFrame frmCalculator;
private JTextField screen;
private JTextField value1;
private JTextField value2;
private JLabel operation;
double fNumber = 0;
double sNumber = 0;
double result = 0;
String ope;
String answer;
public static void main(String[] args) {
try
{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); //Motif, and Windows packages that it requires are shipped with the Java SDK this way
}
catch(Exception e){}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UIFrame window = new UIFrame();
window.frmCalculator.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public UIFrame() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmCalculator = new JFrame();
frmCalculator.setTitle("Java calculator ver 3.1.6 by Muhammad Haroon");
frmCalculator.getContentPane().setBackground(new Color(0, 0, 0));
frmCalculator.getContentPane().setForeground(Color.DARK_GRAY);
frmCalculator.setBounds(100, 100, 326, 422);
frmCalculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmCalculator.getContentPane().setLayout(null);
value1 = new JTextField();
value1.setForeground(Color.DARK_GRAY);
value1.setFont(new Font("Tahoma", Font.PLAIN, 19));
value1.setHorizontalAlignment(SwingConstants.RIGHT);
value1.setBorder(null);
value1.setBounds(151, 52, 141, 31);
frmCalculator.getContentPane().add(value1);
value1.setColumns(10);
value2 = new JTextField();
value2.setFont(new Font("Tahoma", Font.PLAIN, 24));
value2.setHorizontalAlignment(SwingConstants.RIGHT);
value2.setBorder(null);
value2.setBounds(71, 90, 218, 37);
frmCalculator.getContentPane().add(value2);
value2.setColumns(10);
operation = new JLabel("");
operation.setForeground(new Color(144, 238, 144));
operation.setHorizontalAlignment(SwingConstants.LEFT);
operation.setFont(new Font("Tahoma", Font.PLAIN, 20));
operation.setBounds(20, 97, 50, 31);
frmCalculator.getContentPane().add(operation);
screen = new JTextField();
screen.setBorder(new CompoundBorder(new EtchedBorder(EtchedBorder.RAISED, null, null), new EtchedBorder(EtchedBorder.LOWERED, null, null)));
screen.setEditable(false);
screen.setHorizontalAlignment(SwingConstants.RIGHT);
screen.setBounds(10, 48, 289, 82);
frmCalculator.getContentPane().add(screen);
screen.setColumns(10);
rdbtnOn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
act.activate();
}
});
rdbtnOn.setFont(new Font("Cambria Math", Font.PLAIN, 12));
rdbtnOn.setForeground(new Color(255, 255, 255));
rdbtnOn.setBounds(11, 18, 38, 23);
frmCalculator.getContentPane().add(rdbtnOn);
rdbtnOff.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
act.deactivate();
}
});
rdbtnOff.setFont(new Font("Cambria Math", Font.PLAIN, 12));
rdbtnOff.setForeground(new Color(255, 255, 255));
rdbtnOff.setBounds(51, 18, 38, 23);
frmCalculator.getContentPane().add(rdbtnOff);
button0.setBackground(new Color(135, 206, 250));
button0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = value2.getText() + button0.getText(); //value.setText(value.getText()+Button)
value2.setText(num);
}
});
button0.setFont(new Font("Cambria Math", Font.PLAIN, 18));
button0.setBounds(11, 306, 110, 31);
frmCalculator.getContentPane().add(button0);
button2.setBackground(new Color(135, 206, 250));
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = value2.getText() + button2.getText(); //value.setText(value.getText()+Button)
value2.setText(num);
}
});
button1.setBackground(new Color(135, 206, 250));
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = value2.getText() + button1.getText(); //value.setText(value.getText()+Button)
value2.setText(num);
}
});
button1.setFont(new Font("Cambria Math", Font.PLAIN, 18));
button1.setBounds(11, 264, 50, 31);
frmCalculator.getContentPane().add(button1);
button2.setFont(new Font("Cambria Math", Font.PLAIN, 18));
button2.setBounds(71, 264, 50, 31);
frmCalculator.getContentPane().add(button2);
button3.setBackground(new Color(135, 206, 250));
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = value2.getText() + button3.getText(); //value.setText(value.getText()+Button)
value2.setText(num);
}
});
button3.setFont(new Font("Cambria Math", Font.PLAIN, 18));
button3.setBounds(131, 264, 50, 31);
frmCalculator.getContentPane().add(button3);
button4.setBackground(new Color(135, 206, 250));
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = value2.getText() + button4.getText(); //value.setText(value.getText()+Button)
value2.setText(num);
}
});
button4.setFont(new Font("Cambria Math", Font.PLAIN, 18));
button4.setBounds(11, 222, 50, 31);
frmCalculator.getContentPane().add(button4);
button5.setBackground(new Color(135, 206, 250));
button5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = value2.getText() + button5.getText(); //value.setText(value.getText()+Button)
value2.setText(num);
}
});
button5.setFont(new Font("Cambria Math", Font.PLAIN, 18));
button5.setBounds(71, 222, 50, 31);
frmCalculator.getContentPane().add(button5);
button6.setBackground(new Color(135, 206, 250));
button6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = value2.getText() + button6.getText(); //value.setText(value.getText()+Button)
value2.setText(num);
}
});
button6.setFont(new Font("Cambria Math", Font.PLAIN, 18));
button6.setBounds(131, 222, 50, 31);
frmCalculator.getContentPane().add(button6);
button7.setBackground(new Color(135, 206, 250));
button7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String num = value2.getText() + button7.getText(); //value.setText(value.getText()+Button)
value2.setText(num);
}
});
button7.setFont(new Font("Cambria Math", Font.PLAIN, 18));
button7.setBounds(11, 180, 50, 31);
frmCalculator.getContentPane().add(button7);
button8.setBackground(new Color(135, 206, 250));
button8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = value2.getText() + button8.getText(); //value.setText(value.getText()+Button)
value2.setText(num);
}
});
button8.setFont(new Font("Cambria Math", Font.PLAIN, 18));
button8.setBounds(71, 180, 50, 31);
frmCalculator.getContentPane().add(button8);
button9.setBackground(new Color(135, 206, 250));
button9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = value2.getText() + button9.getText(); //value.setText(value.getText()+Button)
value2.setText(num);
}
});
button9.setFont(new Font("Cambria Math", Font.PLAIN, 18));
button9.setBounds(131, 180, 50, 31);
frmCalculator.getContentPane().add(button9);
button_Div.setBackground(new Color(192, 192, 192));
button_Div.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value2.getText());
operation.setText("/");
value2.setText(null);
}
});
button_C.setBackground(new Color(60, 179, 113));
button_C.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value2.setText(null);
value1.setText(null);
operation.setText(null);
}
});
button_C.setFont(new Font("Cambria Math", Font.PLAIN, 18));
button_C.setBounds(71, 138, 50, 31);
frmCalculator.getContentPane().add(button_C);
button_Div.setFont(new Font("Cambria Math", Font.PLAIN, 18));
button_Div.setBounds(191, 180, 50, 31);
frmCalculator.getContentPane().add(button_Div);
button_Mul.setBackground(new Color(192, 192, 192));
button_Mul.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value2.getText());
operation.setText("*");
value2.setText(null);
}
});
button_Mul.setFont(new Font("Cambria Math", Font.PLAIN, 18));
button_Mul.setBounds(191, 222, 50, 31);
frmCalculator.getContentPane().add(button_Mul);
button_Add.setBackground(new Color(192, 192, 192));
button_Add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value2.getText());
operation.setText("+");
ope = "+";
value2.setText(null);
}
});
button_Add.setFont(new Font("Cambria Math", Font.PLAIN, 18));
button_Add.setBounds(191, 306, 50, 31);
frmCalculator.getContentPane().add(button_Add);
button_Sub.setBackground(new Color(192, 192, 192));
button_Sub.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value2.getText());
operation.setText("-");
value2.setText(null);
}
});
button_Sub.setFont(new Font("Cambria Math", Font.PLAIN, 18));
button_Sub.setBounds(191, 264, 50, 31);
frmCalculator.getContentPane().add(button_Sub);
button_Dot.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = value2.getText() + button_Dot.getText(); //value.setText(value.getText()+Button)
value2.setText(num);
}
});
button_Dot.setFont(new Font("Cambria Math", Font.PLAIN, 18));
button_Dot.setBounds(131, 306, 50, 31);
frmCalculator.getContentPane().add(button_Dot);
button_rec.setBackground(new Color(192, 192, 192));
button_rec.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double ops1 = Double.parseDouble(String.valueOf(value2.getText()));
double ops2 = Double.parseDouble(String.valueOf(value2.getText()));
ops1 = 1/ops2;
value2.setText(String.valueOf(ops1));
value1.setText("reciproc("+String.valueOf(ops2)+")");
}
});
//ALT+251
button_Sqr.setBackground(new Color(192, 192, 192));
button_Sqr.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
double ops = Double.parseDouble(String.valueOf(value2.getText()));
ops = Math.sqrt(ops);
value2.setText(String.valueOf(ops));
value1.setText("sqrt("+String.valueOf(ops*ops)+")");
}
});
button_Sqr.setFont(new Font("Cambria Math", Font.PLAIN, 13));
button_Sqr.setBounds(131, 138, 50, 31);
frmCalculator.getContentPane().add(button_Sqr);
button_rec.setFont(new Font("Cambria Math", Font.PLAIN, 11));
button_rec.setBounds(249, 222, 50, 31);
frmCalculator.getContentPane().add(button_rec);
//ALT+0177
button_neg.setBackground(new Color(192, 192, 192));
button_neg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double ops = Double.parseDouble(String.valueOf(value2.getText()));
ops = ops * (-1);
value2.setText(String.valueOf(ops));
value1.setText("negate("+String.valueOf(ops*-1)+")");
}
});
button_neg.setFont(new Font("Cambria Math", Font.PLAIN, 18));
button_neg.setBounds(191, 138, 50, 31);
frmCalculator.getContentPane().add(button_neg);
button_per.setBackground(new Color(192, 192, 192));
button_per.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
double ops = Double.parseDouble(String.valueOf(value2.getText()));
ops = ops * (100);
value2.setText(String.valueOf(ops));
value1.setText(String.valueOf(ops));
}
});
button_per.setBounds(249, 180, 50, 31);
frmCalculator.getContentPane().add(button_per);
button_equal.setBackground(new Color(60, 179, 113));
button_equal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fNumber = Double.parseDouble(value1.getText());
sNumber = Double.parseDouble(value2.getText());
if(operation.getText().equals("+"))
{
result = fNumber + sNumber ;
value1.setText(null);
value2.setText(String.valueOf(result));
}
else if(operation.getText().equals("-"))
{
result = fNumber - sNumber ;
value1.setText(null);
value2.setText(String.valueOf(result));
}
else if(operation.getText().equals("*"))
{
result = fNumber * sNumber ;
value1.setText(null);
value2.setText(String.valueOf(result));
}
else if(operation.getText().equals("/"))
{
result = fNumber / sNumber ;
value1.setText(null);
value2.setText(String.valueOf(result));
}
}
});
//ALT+8592
button_undo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String bkspace = null;
if(value2.getText().length() > 0)
{
StringBuilder strB = new StringBuilder(value2.getText());
strB.deleteCharAt(value2.getText().length() - 1);
bkspace = strB.toString();
value2.setText(bkspace);
}
}
});
button_undo.setFont(new Font("Cambria Math", Font.PLAIN, 19));
button_undo.setBounds(10, 138, 50, 31);
frmCalculator.getContentPane().add(button_undo);
button_equal.setFont(new Font("Cambria Math", Font.PLAIN, 18));
button_equal.setBounds(249, 264, 50, 73);
frmCalculator.getContentPane().add(button_equal);
btnExit.setBackground(new Color(255, 99, 71));
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
btnExit.setFont(new Font("Cambria Math", Font.PLAIN, 12));
btnExit.setBounds(11, 348, 51, 23);
frmCalculator.getContentPane().add(btnExit);
}
/**
* #return the activate_and_Deactivate
*/
public Object getActivate_and_Deactivate() {
return Activate_and_Deactivate;
}
/**
* #param activate_and_Deactivate the activate_and_Deactivate to set
*/
public void setActivate_and_Deactivate(Object activate_and_Deactivate) {
Activate_and_Deactivate = activate_and_Deactivate;
}
}
package Calculators;
import javax.swing.JButton;
import javax.swing.JRadioButton;
public class Activate_and_Deactivate extends UIFrame
{
public void activate()
{
button0.setEnabled(true);
button1.setEnabled(true);
button2.setEnabled(true);
button3.setEnabled(true);
button4.setEnabled(true);
button5.setEnabled(true);
button6.setEnabled(true);
button7.setEnabled(true);
button8.setEnabled(true);
button9.setEnabled(true);
button_Div .setEnabled(true);
button_C .setEnabled(true);
button_Mul.setEnabled(true);
button_Add.setEnabled(true);
button_Sub.setEnabled(true);
button_Dot.setEnabled(true);
button_rec.setEnabled(true);
button_Sqr.setEnabled(true);
button_neg.setEnabled(true);
button_per .setEnabled(true);
button_equal.setEnabled(true);
button_undo .setEnabled(true);
btnExit .setEnabled(true);
rdbtnOff.setEnabled(true);
rdbtnOn.setEnabled(false);
}
public void deactivate()
{
button0.setEnabled(false);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
button4.setEnabled(false);
button5.setEnabled(false);
button6.setEnabled(false);
button7.setEnabled(false);
button8.setEnabled(false);
button9.setEnabled(false);
button_Div .setEnabled(false);
button_C .setEnabled(false);
button_Mul.setEnabled(false);
button_Add.setEnabled(false);
button_Sub.setEnabled(false);
button_Dot.setEnabled(false);
button_rec.setEnabled(false);
button_Sqr.setEnabled(false);
button_neg.setEnabled(false);
button_per .setEnabled(false);
button_equal.setEnabled(false);
button_undo .setEnabled(false);
btnExit .setEnabled(false);
rdbtnOn.setEnabled(true);
rdbtnOff.setEnabled(false);
}
}
Its not working. Can someone help me.
Many thanks.
As far as I know, you can not use your base class as an instance of your derived class.
However, you can store an instance of the derived class in your base class and then cast it back to the derived class when you need to. See the ActionListener on the button for an example
Base
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
#SuppressWarnings("serial")
public class SimpleClass extends JFrame {
public SimpleClass(String title) {
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout());
setUp();
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public SimpleClass() {
}
public void setUp() {
JPanel panel = new JPanel();
ExtendedClass e = new ExtendedClass();
SimpleClass s = new ExtendedClass();
SimpleClass thisInstance = this;
JButton button = new JButton("Press Me");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
System.out.println(e instanceof ExtendedClass);
e.printLn();
System.out.println(s instanceof ExtendedClass);
((ExtendedClass)s).printLn();
System.out.println(thisInstance instanceof ExtendedClass);
//Returns an exception as it is not an instance of ExtendedClass
((ExtendedClass)thisInstance).printLn();
}
});
panel.add(button);
getContentPane().add(panel);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new SimpleClass("Title"));
}
}
Extended
#SuppressWarnings("serial")
public class ExtendedClass extends SimpleClass {
public void printLn() {
System.out.println("Printed A Line To The Console :)");
}
}
i have two jframes in my app, Addnmember.java and Frame1.java
Frame1 is the main Jframe,
i want when someone presses the Add button, the Addmember will be shown, i have done this with this code, and every thing is fine:
AddB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
AddMember addmem = new AddMember();
addmem.setVisible(true);
}
});
But in the new Jframe, i want to come back again when Done Bt is selected, i have done this whit this code:
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//dispose();
Frame1 addmem = new Frame1();
addmem.setVisible(true);
}
});
but this doesent work! mean the Jframe Loads but there is no content in it! look:
why this happens? here is my codes:
AddMember.java:
public class AddMember extends JFrame {
private JFrame frame;
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JTextField textField_6;
private JButton btnNewButton;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AddMember frame = new AddMember();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public AddMember() {
setTitle("\u0627\u0641\u0632\u0648\u062F\u0646 \u0639\u0636\u0648 \u062C\u062F\u06CC\u062F");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 331);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("\u0646\u0627\u0645:");
label.setFont(new Font("Tahoma", Font.BOLD, 11));
label.setBounds(332, 60, 46, 14);
contentPane.add(label);
textField = new JTextField();
textField.setToolTipText("\u0646\u0627\u0645");
textField.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
textField.setText("");
}
});
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setText("\u0646\u0627\u0645");
textField.setBounds(236, 57, 86, 20);
contentPane.add(textField);
textField.setColumns(10);
JLabel label_1 = new JLabel("\u0634.\u062F\u0627\u0646\u0634\u062C\u0648\u06CC\u06CC:");
label_1.setFont(new Font("Tahoma", Font.BOLD, 11));
label_1.setBounds(328, 103, 85, 14);
contentPane.add(label_1);
textField_1 = new JTextField();
textField_1.setToolTipText("\u0634\u0645\u0627\u0631\u0647 \u062F\u0627\u0646\u0634\u062C\u0648\u06CC\u06CC");
textField_1.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField_1.setText("");
}
});
textField_1.setText("\u0634\u0645\u0627\u0631\u0647 \u062F\u0627\u0646\u0634\u062C\u0648\u06CC\u06CC");
textField_1.setHorizontalAlignment(SwingConstants.RIGHT);
textField_1.setColumns(10);
textField_1.setBounds(236, 100, 86, 20);
contentPane.add(textField_1);
JLabel label_2 = new JLabel("\u0631\u0634\u062A\u0647:");
label_2.setFont(new Font("Tahoma", Font.BOLD, 11));
label_2.setBounds(332, 145, 46, 14);
contentPane.add(label_2);
textField_2 = new JTextField();
textField_2.setToolTipText("\u0631\u0634\u062A\u0647 \u062A\u062D\u0635\u06CC\u0644\u06CC");
textField_2.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField_2.setText("");
}
});
textField_2.setText("\u0631\u0634\u062A\u0647 \u062A\u062D\u0635\u06CC\u0644\u06CC");
textField_2.setHorizontalAlignment(SwingConstants.RIGHT);
textField_2.setColumns(10);
textField_2.setBounds(236, 142, 86, 20);
contentPane.add(textField_2);
JLabel label_3 = new JLabel("\u0646\u0627\u0645 \u062E\u0627\u0646\u0648\u0627\u062F\u06AF\u06CC:");
label_3.setFont(new Font("Tahoma", Font.BOLD, 11));
label_3.setBounds(144, 61, 82, 14);
contentPane.add(label_3);
textField_3 = new JTextField();
textField_3.setToolTipText("\u0646\u0627\u0645 \u062E\u0627\u0646\u0648\u0627\u062F\u06AF\u06CC");
textField_3.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField_3.setText("");
}
});
textField_3.setText("\u0646\u0627\u0645 \u062E\u0627\u0646\u0648\u0627\u062F\u06AF\u06CC");
textField_3.setHorizontalAlignment(SwingConstants.RIGHT);
textField_3.setColumns(10);
textField_3.setBounds(48, 58, 86, 20);
contentPane.add(textField_3);
JLabel label_4 = new JLabel("\u0634. \u062A\u0645\u0627\u0633:");
label_4.setFont(new Font("Tahoma", Font.BOLD, 11));
label_4.setBounds(144, 104, 69, 14);
contentPane.add(label_4);
textField_4 = new JTextField();
textField_4.setToolTipText("\u0634\u0645\u0627\u0631\u0647 \u062A\u0645\u0627\u0633");
textField_4.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField_4.setText("");
}
});
textField_4.setText("\u0634\u0645\u0627\u0631\u0647 \u062A\u0645\u0627\u0633");
textField_4.setHorizontalAlignment(SwingConstants.RIGHT);
textField_4.setColumns(10);
textField_4.setBounds(48, 101, 86, 20);
contentPane.add(textField_4);
textField_5 = new JTextField();
textField_5.setToolTipText("\u0633\u0627\u0644 \u0648\u0631\u0648\u062F");
textField_5.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField_5.setText("");
}
});
textField_5.setText("\u0633\u0627\u0644 \u0648\u0631\u0648\u062F");
textField_5.setHorizontalAlignment(SwingConstants.RIGHT);
textField_5.setColumns(10);
textField_5.setBounds(48, 140, 86, 20);
contentPane.add(textField_5);
JLabel label_5 = new JLabel("\u0648\u0631\u0648\u062F\u06CC:");
label_5.setFont(new Font("Tahoma", Font.BOLD, 11));
label_5.setBounds(144, 143, 46, 14);
contentPane.add(label_5);
textField_6 = new JTextField();
textField_6.setToolTipText("\u0636\u0631\u0648\u0631\u06CC \u0646\u06CC\u0633\u062A");
textField_6.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField_6.setText("");
}
});
textField_6.setHorizontalAlignment(SwingConstants.RIGHT);
textField_6.setText("\u0636\u0631\u0648\u0631\u06CC \u0646\u06CC\u0633\u062A");
textField_6.setColumns(10);
textField_6.setBounds(131, 187, 86, 20);
contentPane.add(textField_6);
JLabel label_6 = new JLabel("\u0627\u06CC\u0645\u06CC\u0644:");
label_6.setFont(new Font("Tahoma", Font.BOLD, 11));
label_6.setBounds(227, 190, 46, 14);
contentPane.add(label_6);
btnNewButton = new JButton("Done");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//dispose();
Frame1 addmem = new Frame1();
addmem.setVisible(true);
}
});
btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 11));
btnNewButton.setBounds(171, 227, 89, 23);
contentPane.add(btnNewButton);
}
private static void addPopup(Component component, final JPopupMenu popup) {
component.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
showMenu(e);
}
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
showMenu(e);
}
}
private void showMenu(MouseEvent e) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
});
}
}
And Frame1.java:
public class Frame1 extends JFrame {
Connection connection = null;
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Frame1 window = new Frame1();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
private JTable table;
public Frame1() {
JOptionPane.showMessageDialog(null, "sazande :|");
this.initialize();
connection = DataBConect.dbConnect();
try {
String query = "select * from Users";
PreparedStatement pst = connection.prepareStatement(query);
ResultSet rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
JOptionPane.showMessageDialog(null, "intialize :|");
frame.setTitle("App");
frame.setBounds(100, 100, 725, 465);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton AddB = new JButton("Add");
AddB.setBackground(Color.WHITE);
AddB.setFont(new Font("Tahoma", Font.BOLD, 11));
AddB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
AddMember addmem = new AddMember();
addmem.setVisible(true);
}
});
AddB.setBounds(302, 392, 135, 23);
frame.getContentPane().add(AddB);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(22, 26, 658, 359);
frame.getContentPane().add(scrollPane);
table = new JTable();
scrollPane.setViewportView(table);
}
}
thanks.
As to why your second Frame1 shows no data, I'm not 100% sure, but perhaps it has something to do with your failure to close your database connection. Regardless, it seems wasteful and even potentially dangerous to want to gather the database data twice when you've already obtained it once, and to create your Frame1 GUI twice when you've already created it once.
You should re-design your GUI. The main JFrame GUI should remain visible, and the Frame1 window should be a modal JDialog. As a modal dialog, it will prevent users from being able to interact with the parent JFrame until the dialog is no longer visible.
Note that to get started requires only a few changes:
in Frame1.java
AddB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// frame.dispose();
AddMember addmem = new AddMember(Frame1.this); // !!
addmem.setVisible(true);
}
});
in AddMember.java
class AddMember extends JDialog {
public AddMember(JFrame frame) {
super(frame, "", ModalityType.APPLICATION_MODAL);
setTitle("\u0627\u0641\u0632\u0648\u062F\u0646 \u0639\u0636\u0648 \u062C\u062F\u06CC\u062F");
// !! setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// dispose();
dispose(); // !!
// !! Frame1 addmem = new Frame1();
// addmem.setVisible(true);
}
});
Also, you'll want to avoid null layouts and setBounds(...) as this will lead to very rigid, hard to debug and improve programs.
I know this is ill advised but I am using absolute layout and trying to go from panel to panel and was curious how to do this.
public class Mainscreen extends JFrame {
private JPanel Home;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Mainscreen frame = new Mainscreen();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Mainscreen() {
final Dataentrylog DEL = new Dataentrylog();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setBounds(100, 100, 618, 373);
Home = new JPanel();
Home.setBackground(new Color(255, 250, 250));
Home.setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
setContentPane(Home);
Home.setLayout(null);
JButton btnNewButton = new JButton("Data Entry login");
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 14));
btnNewButton.setForeground(new Color(0, 0, 0));
btnNewButton.setBackground(UIManager.getColor("Menu.selectionBackground"));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Home.setVisible(false);
setContentPane(DEL);
setLayout(null);
}
});
btnNewButton.setBounds(44, 214, 204, 61);
Home.add(btnNewButton);
}
}
Calling this JPanel which works
import java.awt.EventQueue;
public class Dataentrylog extends JPanel {
public Dataentrylog() {
setBounds(100, 100, 618, 373);
setBackground(new Color(255, 250, 250));
setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
setLayout(null);
DEadmin DEA = new Deadmin();
final JButton btnSignIn = new JButton("Sign in");
btnSignIn.setFont(new Font("Tahoma", Font.PLAIN, 14));
btnSignIn.setBackground(UIManager.getColor("EditorPane.selectionBackground"));
btnSignIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnSignIn.setBounds(226, 282, 153, 52);
add(btnSignIn);
}
}
While this works if I try an call another JPanel from Dataentry log the JFrame is blank. What can I do to call another JPanel? Any help would be great. Also, I know that layoutmanagers are the norm but for what I had to do I wasn't able to find anything that worked so I choose to use null despite my best judgement. THANKS!
Intialization of Dataentrylog should be like this
final Dataentrylog DEL = new Dataentrylog(this);
we have to pass the parent jframe.
public class Dataentrylog extends JPanel {
public Dataentrylog(final JFrame parent ) {
setBounds(100, 100, 618, 373);
setBackground(new Color(255, 250, 250));
setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
final JButton btnSignIn = new JButton("Sign in");
btnSignIn.setFont(new Font("Tahoma", Font.PLAIN, 14));
btnSignIn.setBackground(UIManager.getColor("EditorPane.selectionBackground"));
btnSignIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
JPanel panel = new JPanel();
panel.add(new JButton("Solved"));
parent.setContentPane(panel);
}
});
btnSignIn.setBounds(226, 282, 153, 52);
add(btnSignIn);
}
}
I want to change the input in the textfield which will instantly update to the display, instead of press ENTER button to update it.
Here is my code.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyProgram01 extends JFrame
{
private JTextField text1;
private JCheckBox check1;
private JCheckBox check2;
private String message;
private JLabel label1;
private JLabel label2;
private Font font;
public MyProgram01(String title)
{
super(title);
check1 = new JCheckBox("Bold");
check2 = new JCheckBox("Italics");
label1 = new JLabel("Text : ");
label2 = new JLabel("Style : ");
message = "Good Morning...";
text1 = new JTextField(message, 100);
font = new Font("Times New Roman", Font.PLAIN, 36);
setBounds(0, 0, 600, 300);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(0, 0, 600, 120);
panel.setBackground(Color.ORANGE);
label1.setFont(new Font("Times New Roman", Font.PLAIN, 36));
label1.setBounds(15, 15, 100, 36);
panel.add(label1);
text1.setBounds(120, 15, 400, 36);
panel.add(text1);
label2.setFont(new Font("Times New Roman", Font.PLAIN, 36));
label2.setBounds(15, 65, 100, 36);
panel.add(label2);
check1.setBounds(120, 65, 100, 36);
check2.setBounds(220, 65, 100, 36);
panel.add(check1);
panel.add(check2);
check1.addActionListener(new CheckBoxListener());
check2.addActionListener(new CheckBoxListener());
text1.addActionListener(new TextFieldListener());
setLayout(null);
add(panel);
}
public void paint(Graphics g)
{
super.paint(g);
g.setFont(font);
g.drawString(message, 15, 255);
}
private class CheckBoxListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(check1.isSelected() && check2.isSelected())
{
font = new Font("Times New Roman", Font.BOLD + Font.ITALIC, 36);
repaint();
}
else if(check1.isSelected())
{
font = new Font("Times New Roman", Font.BOLD, 36);
repaint();
}
else if(check2.isSelected())
{
font = new Font("Times New Roman", Font.ITALIC, 36);
repaint();
}
else
{
font = new Font("Times New Roman", Font.PLAIN, 36);
repaint();
}
}
}
private class TextFieldListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
message = text1.getText();
repaint();
}
}
public static void main(String[] args)
{
JFrame frame = new MyProgram01("My Program 01");
frame.setVisible(true);
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
How can I change the code to instantly update to the display?
EDIT :
It's work with the keyListener, but my program will only start display after second key is pressed.
For example, I key in abc, the program will start show a when I press b, and when I pressed c, it displays ab and c is missing, unless I press ENTER.
Here the part of the code :
text1.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
message = text1.getText();
repaint();
}
});
Add a KeyListener to your textfield.
You can do that like this:
textField.addKeyListener(new KeyAdapter(){
#Override
public void keyPressed(KeyEvent e){
message = textField.getText();
repaint();
}
});
OR
Add a DocumentListener to your textfield's Document.
You can do that like this:
private JFrame getFrame(){
return this;
}
...
textField.getDocument().addDocumentListener(new DocumentListener(){
#Override
public void insertUpdate(DocumentEvent e) {
message = textField.getText();
getFrame().repaint();
}
#Override
public void removeUpdate(DocumentEvent e) {
message = textField.getText();
getFrame().repaint();
}
#Override
public void changedUpdate(DocumentEvent e) {
// on change
}
});
Instead of using ActionListener for the class TextFieldListener, use KeyListener interface and use the keyTyped(KeyEvent e) method. When ever the event arises you can use getText() of texfield and repaint it.