Changing a String data with Jbuttons in Java - java

I have a String called s
I have four JButtons b1 b2 b3 b4
By default String s is Hello World
b1 Makes String s toUpperCase
b2 Makes String s toLowerCase
b3 Allows user to change String
b4 Resets String to Hello World
When I run the program I have s set to Hello World but when I click my buttons the String is not changing to its new attribute.
import java.awt.EventQueue;
import java.awt.event.*;
import javax.swing.*;
public class Driver {
String s = "Hell World";
String up;
String low;
private JFrame f;
private JPanel p;
JFrame frame = new JFrame("Add Some Text");
JButton b1 = new JButton("Uppercase");
JButton b2 = new JButton("Lowercase");
JButton b3 = new JButton("New");
JButton b4 = new JButton("Reset");
JLabel jl = new JLabel(s);
public Driver () {
gui();
}
public void gui() {
f = new JFrame("Hello World");
p = new JPanel();
f.add(p);
p.setLayout(null);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(jl);
b1.setLocation(27, 80);
b2.setLocation(300, 80);
b3.setLocation(27, 180);
b4.setLocation(300, 180);
jl.setLocation(240, 20);
b1.setSize(230, 80);
b2.setSize(230, 80);
b3.setSize(230, 80);
b4.setSize(230, 80);
jl.setSize(230, 20);
// pack the frame for better cross platform support
f.pack();
// Make it visible
f.setVisible(true);
f.setSize(560,300); // default size is 0,0
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
up = s.toUpperCase();
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
low = s.toLowerCase();
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
s = JOptionPane.showInputDialog(frame, "");
}
});
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
s = "Hello World";
}
});
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Driver();
}
});
} // End main Method
} // End class Driver

You have to set the text to the label!
Try this:
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
jl.setText(s.toUpperCase());
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
jl.setText(s.toLowerCase());
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
s = JOptionPane.showInputDialog(frame, "");
jl.setText(s);
}
});
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
jl.setText("Hello World");
}
});

Actually the String is changing, what happens here that you haven't update the JLabel with the new value for the String:
So your code should look like this:
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
s = JOptionPane.showInputDialog(frame, "");
jl.setText(s);
}
});
then add the following line in each Action ActionListener.
jl.setText(newValue);// newValue depends on what the ActionListener do.

Related

keyListener on jbutton not working

I would like to perform some action when 'Enter' key is press as soon as jbutton is clicked but it is not working, someone help
here is my code
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jButton.keyTyped(e);
}
key listener function
public void keyTyped(KeyEvent e) {
//action
}
You need to bind it - sample code below.
public class Test {
static JButton btnA = new JButton("A");
static JPanel jp = new JPanel();
static JFrame jf = new JFrame("test frame");
static ActionListener action = new ActionListener() {
public void actionPerformed(ActionEvent e) {
jl.setText(((JButton)e.getSource()).getText());
}
};
public static void main(String[] args) {
jf.setVisible(true);
jf.setSize(400, 400);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jp.add(btnA);
jf.add(jp);
btnA.addActionListener(action);
}
}

Setting Multiple buttons to disabled

I'm trying to make it so when the user clicks a button it will become disabled. I've used the following code to achieve this:
final JButton die1 = new JButton(theModel.letters.get(0));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn1){
die1.setEnabled(false);
}
});
boggleGrid.add(die1);
theModel.letters.get(0) is a string value and works validly. My problem is when I try to use this same code for other buttons within the same boggleGrid JPanel. I think it may have to do with the "final" statement, however it is necessary for the actionListener to function. Here is the rest of my code:
JPanel boggleGrid = new JPanel(new GridLayout(4,4));
theModel.randomLetters();
final JButton die1 = new JButton(theModel.letters.get(0));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn1){
die1.setEnabled(false);
}
});
boggleGrid.add(die1);
final JButton die2 = new JButton(theModel.letters.get(1));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn2){
die2.setEnabled(false);
}
});
boggleGrid.add(die2);
final JButton die3 = new JButton(theModel.letters.get(2));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn3){
die3.setEnabled(false);
}
});
boggleGrid.add(die3);
final JButton die4 = new JButton(theModel.letters.get(3));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn4){
die4.setEnabled(false);
}
});
boggleGrid.add(die4);
final JButton die5 = new JButton(theModel.letters.get(4));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn5){
die5.setEnabled(false);
}
});
boggleGrid.add(die5);
final JButton die6 = new JButton(theModel.letters.get(5));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn6){
die6.setEnabled(false);
}
});
boggleGrid.add(die6);
final JButton die7 = new JButton(theModel.letters.get(6));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn7){
die7.setEnabled(false);
}
});
boggleGrid.add(die7);
final JButton die8 = new JButton(theModel.letters.get(7));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn8){
die8.setEnabled(false);
}
});
boggleGrid.add(die8);
final JButton die9 = new JButton(theModel.letters.get(8));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn9){
die9.setEnabled(false);
}
});
boggleGrid.add(die9);
final JButton die10 = new JButton(theModel.letters.get(9));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn10){
die10.setEnabled(false);
}
});
boggleGrid.add(die10);
final JButton die11 = new JButton(theModel.letters.get(10));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn11){
die11.setEnabled(false);
}
});
boggleGrid.add(die11);
final JButton die12 = new JButton(theModel.letters.get(11));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn12){
die12.setEnabled(false);
}
});
boggleGrid.add(die12);
final JButton die13 = new JButton(theModel.letters.get(12));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn13){
die13.setEnabled(false);
}
});
boggleGrid.add(die13);
final JButton die14 = new JButton(theModel.letters.get(13));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn14){
die14.setEnabled(false);
}
});
boggleGrid.add(die14);
JButton die15 = new JButton(theModel.letters.get(14));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn15){
die15.setEnabled(false);
}
});
boggleGrid.add(die15);
JButton die16 = new JButton(theModel.letters.get(15));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn16){
die16.setEnabled(false);
}
});
boggleGrid.add(die16);
frame.add(boggleGrid, BorderLayout.CENTER);
Context: I am making a Boggle game (if that helps). Thanks
You're calling addActionListener on the same JButton multiple times, for example:
final JButton die4 = new JButton(theModel.letters.get(3));
die1.addActionListener(new ActionListener() {
You are creating a button called die4 and then adding an ActionListener to die1. You want the call to addActionListener to happen on die4 in this case.
final JButton die4 = new JButton(theModel.letters.get(3));
die4.addActionListener(new ActionListener() {
^^^^
Here is an implementation of your code that is a bit more concise and probably easier to maintain:
JPanel boggleGrid = new JPanel(new GridLayout(4, 4));
ActionListener disableButtonListener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent event)
{
// event.getSource() will return a reference to the control that
// fired the event. In this case, a JButton. We protect
// ourselves from a ClassCastException just in case though.
if (!(event.getSource() instanceof JButton)) {
return;
}
((JButton) event.getSource()).setEnabled(false);
}
};
theModel.randomLetters();
for (int i = 0; i < 16; i++) {
JButton button = new JButton(theModel.letters.get(i));
button.addActionListener(disableButtonListener);
boggleGrid.add(button);
}
frame.add(boggleGrid, BorderLayout.CENTER);

Java Smileys Replacing String with Smiley

I'm writing a Chat program. I designed a mock-up gui with smileys where when the user clicks on a smiley(jbutton) it prints it onto a textpane. I managed to add an advanced feature where when a user types in ":)" and sends it, it inserts the smiley instead of the string - using the insertIcon() method. The problem I have is that it only prints the smiley once rather than multiple times. So if I type "Hi :) My name is Jack :)" it only inserts the icon ONCE. Any suggestions?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SmileyTesterGUI extends JFrame {
JPanel main = new JPanel();
JPanel south = new JPanel();
JPanel messageCenter = new JPanel();
JPanel smileysNorth = new JPanel();
JTextField text;
JTextPane tPane;
Icon happy;
Icon smile;
Icon tongue;
Icon veryHappy;
Icon wink;
Icon laugh;
Icon sad;
Icon verySad;
Icon cry;
public SmileyTesterGUI() {
super("Smileys");
add(main);
main.setLayout(new BorderLayout());
main.add(south, BorderLayout.SOUTH);
south.setLayout(new BorderLayout());
south.add(messageCenter, BorderLayout.CENTER);
south.add(smileysNorth, BorderLayout.NORTH);
// textpane panel
tPane = new JTextPane();
JScrollPane sPane = new JScrollPane(tPane);
main.add(sPane);
tPane.setEditable(false);
// smileysPanel
smileysNorth.setLayout(new GridLayout(1, 0));
JButton smiley1 = new JButton();
JButton smiley2 = new JButton();
JButton smiley3 = new JButton();
JButton smiley4 = new JButton();
JButton smiley5 = new JButton();
JButton smiley6 = new JButton();
JButton smiley7 = new JButton();
JButton smiley8 = new JButton();
JButton smiley9 = new JButton();
smileysNorth.add(smiley1);
smileysNorth.add(smiley2);
smileysNorth.add(smiley3);
smileysNorth.add(smiley4);
smileysNorth.add(smiley5);
smileysNorth.add(smiley6);
smileysNorth.add(smiley7);
smileysNorth.add(smiley8);
smileysNorth.add(smiley9);
// set smileys(icon) to each button - pathed from personal directory
happy = new ImageIcon(getClass().getResource("smileys/Smile1.png"));
smiley1.setIcon(happy);
smile = new ImageIcon(getClass().getResource("smileys/Smile2.png"));
smiley2.setIcon(smile);
tongue = new ImageIcon(getClass().getResource("smileys/Smile3.png"));
smiley3.setIcon(tongue);
veryHappy = new ImageIcon(getClass().getResource("smileys/Smile4.png"));
smiley4.setIcon(veryHappy);
wink = new ImageIcon(getClass().getResource("smileys/Smile5.png"));
smiley5.setIcon(wink);
laugh = new ImageIcon(getClass().getResource("smileys/Smile6.png"));
smiley6.setIcon(laugh);
sad = new ImageIcon(getClass().getResource("smileys/Smile7.png"));
smiley7.setIcon(sad);
verySad = new ImageIcon(getClass().getResource("smileys/Smile8.png"));
smiley8.setIcon(verySad);
cry = new ImageIcon(getClass().getResource("smileys/Smile9.png"));
smiley9.setIcon(cry);
// smileys print on the textpane
smiley1.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile1.png")));
}
});
smiley2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile2.png")));
}
});
smiley3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile3.png")));
}
});
smiley4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile4.png")));
}
});
smiley5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile5.png")));
}
});
smiley6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile6.png")));
}
});
smiley7.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile7.png")));
}
});
smiley8.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile8.png")));
}
});
smiley9.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile9.png")));
}
});
// messagePanel
messageCenter.setLayout(new BorderLayout());
text = new JTextField();
JButton send = new JButton("Send");
messageCenter.add(text);
messageCenter.add(send, BorderLayout.EAST);
text.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendMessage();
}
});
send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendMessage();
}
});
setLocation(500, 0);
setSize(600, 250);
}
public void sendMessage() {
String a = text.getText();
// tPane.setText(a);
// tPane.getText();
if (a.equals(":D")) {
tPane.insertIcon(veryHappy);
} else if (a.equals(":)")) {
tPane.insertIcon(smile);
} else if (a.equals(":(")) {
tPane.insertIcon(sad);
} else if (a.equalsIgnoreCase(":P")) {
tPane.insertIcon(tongue);
} else if (a.equals(";)")) {
tPane.insertIcon(wink);
}
text.setText(null);
text.requestFocus();
}
public static void main(String[] args) {
new SmileyTesterGUI().setVisible(true);
}
}
insertIcon() method of JTextPane uses selection (caret position in simplest case). So in your case you always replace the icon just once.
Your sendMessage() doesn't check multiple occurences of ":)" in the message. Use while loop obtaining indexes of the ":)" and for each index make it selected and then use insertIcon()

Java insertIcon multiple times in a text pane

I'm writing a chat program in Java. I made a section for smileys. I managed to print the smileys on a text pane every time its clicked but when I click on the same smiley more than once it only prints it once. Any help?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SmileyTesterGUI extends JFrame {
JPanel main = new JPanel();
JPanel south = new JPanel();
JPanel messageCenter = new JPanel();
JPanel smileysNorth = new JPanel();
JTextField text;
JTextPane tPane;
Icon happy;
Icon smile;
Icon tongue;
Icon veryHappy;
Icon wink;
Icon laugh;
Icon sad;
Icon verySad;
Icon cry;
int number = 0;
boolean check = true;
public SmileyTesterGUI() {
super("Smileys");
add(main);
main.setLayout(new BorderLayout());
main.add(south, BorderLayout.SOUTH);
south.setLayout(new BorderLayout());
south.add(messageCenter, BorderLayout.CENTER);
south.add(smileysNorth, BorderLayout.NORTH);
// textpane panel
tPane = new JTextPane();
JScrollPane sPane = new JScrollPane(tPane);
main.add(sPane);
tPane.setEditable(false);
// smileysPanel
smileysNorth.setLayout(new GridLayout(1, 0));
JButton smiley1 = new JButton();
JButton smiley2 = new JButton();
JButton smiley3 = new JButton();
JButton smiley4 = new JButton();
JButton smiley5 = new JButton();
JButton smiley6 = new JButton();
JButton smiley7 = new JButton();
JButton smiley8 = new JButton();
JButton smiley9 = new JButton();
smileysNorth.add(smiley1);
smileysNorth.add(smiley2);
smileysNorth.add(smiley3);
smileysNorth.add(smiley4);
smileysNorth.add(smiley5);
smileysNorth.add(smiley6);
smileysNorth.add(smiley7);
smileysNorth.add(smiley8);
smileysNorth.add(smiley9);
// set smileys(icon) to each button - pathed from personal directory
happy = new ImageIcon(getClass().getResource("smileys/Smile1.png"));
smiley1.setIcon(happy);
smile = new ImageIcon(getClass().getResource("smileys/Smile2.png"));
smiley2.setIcon(smile);
tongue = new ImageIcon(getClass().getResource("smileys/Smile3.png"));
smiley3.setIcon(tongue);
veryHappy = new ImageIcon(getClass().getResource("smileys/Smile4.png"));
smiley4.setIcon(veryHappy);
wink = new ImageIcon(getClass().getResource("smileys/Smile5.png"));
smiley5.setIcon(wink);
laugh = new ImageIcon(getClass().getResource("smileys/Smile6.png"));
smiley6.setIcon(laugh);
sad = new ImageIcon(getClass().getResource("smileys/Smile7.png"));
smiley7.setIcon(sad);
verySad = new ImageIcon(getClass().getResource("smileys/Smile8.png"));
smiley8.setIcon(verySad);
cry = new ImageIcon(getClass().getResource("smileys/Smile9.png"));
smiley9.setIcon(cry);
// smileys print on the textpane
smiley1.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tPane.insertIcon(happy);
}
});
smiley2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(smile);
}
});
smiley3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(tongue);
}
});
smiley4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(veryHappy);
}
});
smiley5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(wink);
}
});
smiley6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(laugh);
}
});
smiley7.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tPane.insertIcon(sad);
}
});
smiley8.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tPane.insertIcon(verySad);
}
});
smiley9.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tPane.insertIcon(cry);
}
});
// messagePanel
messageCenter.setLayout(new BorderLayout());
text = new JTextField();
JButton send = new JButton("Send");
messageCenter.add(text);
messageCenter.add(send, BorderLayout.EAST);
text.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendMessage();
}
});
send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendMessage();
}
});
setLocation(500, 0);
setSize(600, 250);
}
public void sendMessage() {
String a = text.getText();
//
// if(a){
// tPane.setText(a);
// tPane.getText();
// }
if (a.contains(":D")) {
tPane.insertIcon(veryHappy);
} else if (a.contains(":)")) {
tPane.insertIcon(happy);
} else if (a.contains(":(")) {
tPane.insertIcon(sad);
}
// text.setText(null);
// text.requestFocus();
}
public static void main(String[] args) {
new SmileyTesterGUI().setVisible(true);
}
}
That is because you cannot add the same ImageIcon to the JTextPane more then once.
You can create the ImageIcons dynamically in the ActionListener, every time a smiley is clicked create a new ImageIcon
smiley2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource("smileys/Smile2.png")));
}
});

Changing the text in the title bar

I'm getting an error at the line: jFrame cannot be resolved
jFrame.setTitle(titleName.getText());
public void createOption(){
Option = new JPanel();
Option.setLayout( null );
JLabel TitleLabel = new JLabel("Change the company name");
TitleLabel.setBounds(140, 15, 200, 20);
Option.add(TitleLabel);
titleName = new JTextField();
titleName.setBounds(90,40,260,20);
Option.add(titleName);
JButton Change = new JButton("Change New Name");
Change.setBounds(90,80,150,20);
Change.addActionListener(this);
Change.setBackground(Color.white);
Option.add(Change);
JButton Exit = new JButton("Exit");
Exit.setBounds(270,80,80,20);
Exit.addActionListener(this);
Exit.setBackground(Color.white);
Option.add(Exit);
Change.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
jFrame.setTitle(titleName.getText());
}
});
}
You will have to have a reference to your JFrame. Assuming button and textBox for the names for the controls, you can do
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
jFrame.setTitle(textBox.getText());
}
});
EDIT:
Here's a full example
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class JFrameExample {
public static void main(String[] args) {
final JFrame jFrame = new JFrame("This is a test");
jFrame.setSize(400, 150);
Container content = jFrame.getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
final JTextField jTextField = new JTextField("TestTitle");
content.add(jTextField);
final JButton button = new JButton("Change");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
jFrame.setTitle(jTextField.getText());
}
});
content.add(button);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}

Categories

Resources