Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

can you please make BOLD>ITALIC>UNDERLINE buttons acutlly perfome those actions

ID: 3813042 • Letter: C

Question

can you please make BOLD>ITALIC>UNDERLINE buttons acutlly perfome those actions

import java.awt.FlowLayout;
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.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Mail extends JFrame implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
private JLabel toLabel = new JLabel("To:");
private JTextField toField = new JTextField(41);
private JLabel subjectLabel = new JLabel("Subject:");
private JTextField subjectField = new JTextField(53);
private JLabel messageLabel = new JLabel("");
private JButton sendButton = new JButton("Send");
//button variable declaration
private JButton boldButton = new JButton("Bold");
private JButton italicButton = new JButton("Italic");
private JButton underlineButton = new JButton("Underline");
private JTextArea message = new JTextArea(10, 100);
FlowLayout flow = new FlowLayout(FlowLayout.LEFT);
JScrollPane scroll = new JScrollPane(message,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
public Mail()
{
super("WebBuy");
setSize(1450, 340);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(flow);
add(toLabel);
add(toField);
add(subjectLabel);
add(subjectField);
add(messageLabel);
message.setLineWrap(true);
message.setWrapStyleWord(true);
message.setText("Welcome to WebBuy");
add(scroll);
add(sendButton);
// adding buttons..
add(boldButton);
add(italicButton);
add(underlineButton);
sendButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(!messageLabel.getText().isEmpty() && !subjectField.getText().isEmpty() && !message.getText().isEmpty() ){
message.append(" Mail sent!");
  
}else{
message.append(" Either Subject , Message or Label is Empty");
}
  
}
});
}
public static void main(String[] arguments)
{
Mail email = new Mail();
email.setVisible(true);
}

public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
if(source == sendButton)
message.append(" Mail has been sent!");
}
}

Explanation / Answer

Its just working but not as it should. Remember this is not the right way of doing this. JTextArea is never used for styling text . The best way of doing this is using JTextPane or JEditorPane. Please comment if you want that.

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.Font;

import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.*;
public class Mail extends JFrame implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
private JLabel toLabel = new JLabel("To:");
private JTextField toField = new JTextField(41);
private JLabel subjectLabel = new JLabel("Subject:");
private JTextField subjectField = new JTextField(53);
private JLabel messageLabel = new JLabel("");
private JButton sendButton = new JButton("Send");
//button variable declaration
private JButton boldButton = new JButton("Bold");
private JButton italicButton = new JButton("Italic");
private JButton underlineButton = new JButton("Underline");
private JTextArea message = new JTextArea(10, 100);
FlowLayout flow = new FlowLayout(FlowLayout.LEFT);
JScrollPane scroll = new JScrollPane(message,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
boolean b,i,u;
public Mail()
{
super("WebBuy");
setSize(1450, 340);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(flow);
add(toLabel);
add(toField);
add(subjectLabel);
add(subjectField);
add(messageLabel);
message.setLineWrap(true);
message.setWrapStyleWord(true);
message.setText("Welcome to WebBuy");
add(scroll);
add(sendButton);
  
  
// adding buttons..
add(boldButton);
add(italicButton);
add(underlineButton);
  
b=i=u=false;
sendButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(!messageLabel.getText().isEmpty() && !subjectField.getText().isEmpty() && !message.getText().isEmpty() ){
message.append(" Mail sent!");

}
else
{
message.append(" Either Subject , Message or Label is Empty");
}

}
});

boldButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Font font = new Font(message.getText(), Font.BOLD,12);   
message.setFont(font);
  
  

}
});

italicButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Font font = new Font(message.getText(), Font.ITALIC,12);   
message.setFont(font);
  
  

}
});
  
}
public static void main(String[] arguments)
{
Mail email = new Mail();
email.setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
if(source == sendButton)
message.append(" Mail has been sent!");
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote