Question
Hello, I am trying to disable the JTextField 'textfield' when the JComboBox is set to 'item2'. When the combo box is set to 'item1' I
Hello,
I am trying to disable the JTextField 'textfield' when the JComboBox is set to 'item2'. When the combo box is set to 'item1' I want the JTextField to be enabled.
Also is there a way where I can highlight the borders of the JComboBox to yellow when the user hovers over them?
Below is the code.
Thanks
package test;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Image;
import java.awt.SystemColor;
import javax.swing.UIManager;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.border.EmptyBorder;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
import javax.swing.text.Highlighter.HighlightPainter;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class test {
private JFrame frmAaAutomation;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test window = new test();
window.frmAaAutomation.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public test() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmAaAutomation = new JFrame();
frmAaAutomation.getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
frmAaAutomation.getContentPane().setBackground(new Color(245, 245, 245));
frmAaAutomation.setBounds(100, 100, 650, 900);
frmAaAutomation.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmAaAutomation.getContentPane().setLayout(null);
JLabel lblBrowser = new JLabel("BROWSER");
lblBrowser.setBounds(148, 257, 103, 16);
lblBrowser.setForeground(Color.GRAY);
lblBrowser.setFont(new Font("Arial", Font.PLAIN, 14));
frmAaAutomation.getContentPane().add(lblBrowser);
JButton btnNewButton = new JButton("CONTINUE");
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
btnNewButton.setBounds(215, 667, 190, 37);
btnNewButton.setBackground(Color.RED);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
btnNewButton.setBounds(215, 663, 190, 37);
btnNewButton.setBackground(UIManager.getColor("CheckBoxMenuItem.selectionBackground"));
}
});
btnNewButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
btnNewButton.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
btnNewButton.setForeground(Color.WHITE);
btnNewButton.setBorder(new EmptyBorder(0, 0, 0, 0));
btnNewButton.setOpaque(true);
btnNewButton.setBackground(UIManager.getColor("CheckBoxMenuItem.selectionBackground"));
btnNewButton.setBounds(215, 663, 190, 37);
frmAaAutomation.getContentPane().add(btnNewButton);
textField = new JTextField();
textField.setBounds(148, 396, 80, 26);
frmAaAutomation.getContentPane().add(textField);
textField.setColumns(10);
//textField.setEnabled(false);
JComboBox comboBox_4 = new JComboBox();
comboBox_4.setEditable(true);
comboBox_4.setModel(new DefaultComboBoxModel(new String[] {"item1", "item2"}));
comboBox_4.setForeground(Color.DARK_GRAY);
comboBox_4.setBounds(148, 357, 343, 27);
frmAaAutomation.getContentPane().add(comboBox_4);
if(comboBox_4.toString().equals("item1"))
{
textField.setEnabled(true);
}else if(comboBox_4.toString().equals("item2"))
textField.setEnabled(false);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started