I have a JCheckBox
with some text and a JLabel
with an ImageIcon
Requirement is by checking the JCheckBox
, the JCheckBox
text and JLabel
icon should be displayed in JEditorPane
I used
if (jCheckBox3.isSelected()) {
s1 = jCheckBox3.getText() + jLabel1.getIcon() ;
}
And
jEditorPane1.setText(s1);
output is
“checkbox text” file:/G:/myProject/build/classes/myproject/img.png
Instead of image I am getting image path
This is an example how you could do it:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class CheckBoxCheckingDisplayImagePanel extends JPanel implements ActionListener {
private JCheckBox checkBox;
private JLabel label;
private JEditorPane editorPane;
private JPanel editorPanel;
public CheckBoxCheckingDisplayImagePanel() {
try {
JPanel panel = new JPanel(new GridLayout(0, 2));
this.setLayout(new BorderLayout(10, 10));
this.add(panel,BorderLayout.NORTH);
ImageIcon icon = new ImageIcon(ImageIO.read(new URL("http://i.stack.imgur.com/lxthA.jpg")));
label = new JLabel(icon, SwingConstants.HORIZONTAL);
checkBox = new JCheckBox("Check me!");
checkBox.addActionListener(this);
panel.add(label);
panel.add(checkBox);
editorPane = new JEditorPane();
this.add(editorPane,BorderLayout.CENTER);
} catch (MalformedURLException ex) {
Logger.getLogger(CheckBoxCheckingDisplayImagePanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(CheckBoxCheckingDisplayImagePanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(checkBox)) {
editorPane.setContentType("text/html");
editorPane.setText("<img src=\"http://i.stack.imgur.com/lxthA.jpg\">");
}
}
}
Patrick
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.