enter image description here
I have a list JLabel. I want when click a label content display in JTextArea the same. Why when I click the label, the text area does not display?
The code:
jLabel0.setText(namelist.get(0));
jLabel1.setText(namelist.get(1));
jLabel2.setText(namelist.get(2));
jLabel3.setText(namelist.get(3));
jLabel4.setText(namelist.get(4));
jLabel5.setText(namelist.get(5));
//String b[]={"jLabel4","jLabel5","jLabel7","jLabel8","jLabel9","jLabel10"};
for (int i=0;i<k;i++){
String f=String.valueOf(i);
JLabel jlb = new JLabel("jLabel"+f);
String Af=file_list.get(i);
FileReader F=new FileReader(Af);
jlb.addMouseListener(new MouseListener(){
public void mouseReleased(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==1)
{
try {
jTextArea3.read(F,"");
} catch (IOException ex) {
Logger.getLogger(FAKENEWS.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
}
You can simply achieve it using JButton and just by making the button look like a label.
You will want to do the following once you have created the button:
setFocusPainted(false);
setMargin(new Insets(0, 0, 0, 0));
setContentAreaFilled(false);
setBorderPainted(false);
setOpaque(false);
You may want to exclude setFocusPainted(false) if you want it to actually paint the focus (e.g. dotted line border on Windows look and feel).
And after that you may use the button event handlers to do your desired action.
Related
I have problem, i dynamically create the buttons (users write text in JtextArea then is create the new button) and when user clicked in button this text is writing in JtextPane.
I dont know why doing this?
The buttons is created but when user clicked on button doing nothing.
DODAJNOWYButton.addMouseListener(new NewMouseListener(textPane1) {
#Override
public void mouseClicked(MouseEvent e) {
String text = textArea2.getText();
bar.add(new JButton(""+text));
bar.validate();
bar.repaint();
try{
doc.insertString(doc.getLength(),""+text,null);
} catch (BadLocationException e1) {
System.out.println(e);
}
super.mouseClicked(e);
}
});
You can use addClickHandler():
JButton yourButton = new JButton(""+text);
yourButton.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
//do something
}
}
I am learning Java and trying to implement a MouseListener for the first time. I have read the java doc
MouseListener but my code doesnt work, as in nothing happens when i press the button. Here is a jbutton with a pressed and released event. Can someone explain where i have gone wrong?
JButton upButton_1 = new JButton("Up");
upButton_1.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent pevt) {
upButtonPressPerformed(pevt);
}
public void mouseReleased(MouseEvent revt) {
upButtonReleasePerformed(revt);
}
public synchronized void upButtonPressPerformed(
MouseEvent pevt) {
resultsTextArea.setText("Up Button Activated, String: " + downString);
try{
//See Above comments for sending ASCII String
byte[] bytes = DatatypeConverter.parseHexBinary(upString);
TwoWaySerialComm.SerialWriter sw = new TwoWaySerialComm.SerialWriter(
twoWaySerCom.serialPort.getOutputStream());
sw.out.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
public synchronized void upButtonReleasePerformed(
MouseEvent revt) {
resultsTextArea.setText("Up Button released, String: " + downString);
try{
//See Above comments for sending ASCII String
byte[] bytes = DatatypeConverter.parseHexBinary(upString);
TwoWaySerialComm.SerialWriter sw = new TwoWaySerialComm.SerialWriter(
twoWaySerCom.serialPort.getOutputStream());
sw.out.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
});
ActionListener is what you are looking for if you want to work with buttons.
JButton button = new JButton("SomeButton");
button.addActionListener(this);
void ActionPerformed(ActionEvent e) {
if(e.getSource() == button) {
// do whatever you want if button is clicked
}
}
Or you can use anonymous inner class:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//do whatever you want
}
});
//or the Java8 version
button.addActionListener((e) -> {
//do whatever you want
});
Whit MouseListener you can listen to events like:
MouseClicked, MouseEntered, MouseExited, MousePresse, MouseReleased.
You could use these, but for button click its more logical to listen to your buttons not your mouse.
I've created some swing applications involving JButtons, and noticed whenever one is clicked, it turns white. Example here.
How would I change it so when, and only when, the button is clicked, it turns RED instead of the usual white, and when it is released, it goes back to its normal look? Is there a method for this?
Example code:
JButton b = new JButton("foo");
b.addMouseListener(new MouseAdapter(){
#Override
public void mousePressed(MouseEvent e) {
//turn red
}
#Override
public void mouseReleased(MouseEvent e) {
//go back to original state
}
});
change color of button text using setForeground method
like this
#Override
public void mousePressed(MouseEvent e) {
b.setForeground(Color.red); // button text color
// b.setBackground(Color.red); // button background color
}
#Override
public void mouseReleased(MouseEvent e) {
b.setForeground(Color.black); // button text color
}
JButton b = new JButton("foo");
b.addMouseListener(new MouseAdapter(){
#Override
public void mousePressed(MouseEvent e) {
b.setBackground(Color.red);
}
#Override
public void mouseReleased(MouseEvent e) {
//go back to original state
}
});
For more details look at this example
I set a name to my JTextArea component in Java. I can not create this text area component global in my design. I need to get the name of the text area to change the color of a button which is related text area. What should I do?
JTextArea jtfText1= new JTextArea() ;
jtfText1.setText("adsas");
jtfText1.setName("Q "+Integer.toString(a+1));
jtfText1.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
}
public void removeUpdate(DocumentEvent e) {
}
public void insertUpdate(DocumentEvent e) {
System.out.println(a);
JButton button =(JButton)bottom.getComponent(a-1);
button.setBackground(Color.GREEN);
button.setOpaque(true);
}
});
I need to know how to effectively add a mouse event to a JComboBox or any other approach that works. I found some possible solutions here and also different sites but I can't get it to work. It seems that mouseEvent is not appropriate to use on JComboBox as it is a compound component. I found a possible solution for a compound component but also doesn't work. So below is my code that works when I use a text field. Any ideas of which approach should I use? Thanks
private void updateReviewers() {
jComboBox_reviewer.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {
System.out.println("clicked");
}
#Override
public void mousePressed(MouseEvent e) {
System.out.println("pressed");
}
#Override
public void mouseReleased(MouseEvent e) {
System.out.println("released");
}
#Override
public void mouseEntered(MouseEvent e) {
System.out.println("entered");
}
#Override
public void mouseExited(MouseEvent e) {
System.out.println("exited");
}
}
);
}
You ought to be able to use addActionListener(ActionEvent e) on the JComboBox itself. Once any item is selected you may perform any sort of validation within the action listener.
jcomboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
JComboBox comboBox = (JComboBox) event.getSource();
Object o = comboBox.getSelectedItem();
//Any extra code
}
});
Ofcourse, Object may be cast to your desired Object type.
Oracle Documentation for Event handling with JComboBox
It is a program to make a JComboBox and make a sting array and use those array items and make the ComboBox's list items. Then link each item with an image. Then we start the Action Listener and provide an action to each of the list item. Note that you must save the images in the source folder of the project and the class folder.
package JComboBox;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*<applet code="JComboBoxDemo" width=200 height=120></applet>
*/
public class JComboBoxDemo extends JApplet
{
JLabel jlab;
ImageIcon hourglass, digital, analog, stopwatch;
JComboBox <String> jcb;
String timepieces[] = {"Digital", "Analog", "Hourglass", "Stopwatch"};
String s;
public void init()
{
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
makeGUI();
}
});
}
catch(Exception exc)
{
System.out.println("Program can't run because of "+exc);
}
}
private void makeGUI()
{
setLayout(new FlowLayout());
jcb = new JComboBox<String>(timepieces);
add(jcb);
jcb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
s = (String) jcb.getSelectedItem();
jlab.setIcon(new ImageIcon(s + ".jpg"));
}
});
jlab = new JLabel(new ImageIcon());
add(jlab);
}
}