This question already has an answer here:
Is it possible to create programs in Java that create text to link in Chrome?
(1 answer)
Closed 10 years ago.
I'd like to make all links in JEditorPane clickable. I tried to use the code from this answer, but probably I've done something wrong because nothing happens when I click on the link. Here's my code:
JEditorPane news = new JEditorPane();
news.setSize(Size.L_NEWS);
news.setLocation(Position.L_NEWS);
news.setFocusable(false);
news.setBackground(new Color(255, 255, 255, 0));
news.setEditable(false);
news.setEnabled(false);
news.setOpaque(false);
news.setVisible(true);
news.setEditorKit(JEditorPane.createEditorKitForContentType("text/html"));
news.setText(getNewsHTML.getNewestNews());
try{
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e){
e.printStackTrace();
}
news.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
if(Desktop.isDesktopSupported()) {
try {
Desktop.getDesktop().browse(e.getURL().toURI());
}
catch (IOException | URISyntaxException e1) {
e1.printStackTrace();
}
}
}
}
}
);
login_form.add(news);
And here - my imports(maybe they are the problem):
import java.awt.Color;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
What is wrong? I don't have any output to Eclipse's console.
From my comments:
See my simple example here:
Your problem lies here:
news.setEnabled(false);
Dont set it to disabled or it wont be able to catch events like mouse click etc.
Also not sure why you have:
news.setFocusable(false);
news.setVisible(true);
The component does not need to be set visible just add to a container and make the container visible. Also dont make it unfocusable as this may cause problems later. Your setEditable(false) should be enough (as user wont be able to edit it regardless of focusabilty)
Related
This problem happens when I would like to use AudioSystem.getClip()
The problem is that, it cannot resolve the method getClip().
Here is my code
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.TextArea;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.TargetDataLine;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import java.awt.FlowLayout;
import javax.swing.JProgressBar;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
public void play1()
{
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("recod_1.wav").getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch(Exception ex) {
System.out.println("Error with playing sound.");
ex.printStackTrace();
}
}
Everything else has no error, just that one.
I realized that the getClip() method came since the version 1.5. I don't know this is the version of what but it is stated on this link that the getClip method has been affected. https://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/AudioSystem.html
I guess that if I update my javax jar file (because I am using an external jar file for javax.sound.sampled) I can fix the problem. But I don't find a newer version of this jar file anywhere else (tritonus-share-0.3.7-2.jar)
I am developing Automated tests on Selenium Java and following is the configuration of my laptop.
Operating System:Windows 7 Ultimate.
Java Version:Java Version details
Eclipse Version:Eclipse IDE details
I am facing a problem with awt Choice component in java, as
when I open the choice drop down and click on 1st item, then it is not selected
then i select any other item from the list, and that item selected,
now when I select the first item again it is being selected.
Why it is not being selected on first click.
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Label;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Image;
public class MainMenu1 extends Frame implements ItemListener{
Choice bl;
String[][] userselect = new String[5][4];
MainMenu1() {
setTitle("Automation Test");
setLayout(null);setBackground(Color.pink);setSize(800,400); setLocation(200,200);setVisible(true);
setResizable(false);
bl = new Choice();bl.add("IE");bl.add("Chrome");bl.add("FireFox");bl.add("Safari");bl.add("Opera");bl.setSize(90, 21);bl.setLocation(180, 90);
add(bl);
bl.addItemListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void itemStateChanged(ItemEvent i){
if(i.getSource()==bl)
{
System.out.println(bl.getSelectedItem());
userselect[0][1]=bl.getSelectedItem();
}
}
public static void main(String[] args){
new MainMenu1();
}
}
I been using my textbook to build a GUI but one thing has been plaging me. When I try to make a clear button with a removeAll method it does not work at all. The problem I have been having is the GetActionCommand() is undefined .
import acm.graphics.*;
import acm.program.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JSlider;
import acm.program.GraphicsProgram;
import javafx.event.ActionEvent;
public class GUI_Program extends GraphicsProgram{
public void init() {
setBackground(Color.GRAY);
add(Cleared, WEST);
addActionListeners();
sizeSlider = new JSlider(MIN_SIZE, MAX_SIZE, INITIAL_SIZE);
add(new JLabel(" small"), WEST);
add(sizeSlider, WEST);
add(new JLabel("Large "), WEST);
ColorBox();
add(colorBox, WEST);
addMouseListeners();
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Clear")) {
removeAll();
}
}
The problem is this line:
import javafx.event.ActionEvent;
There is more than one ActionEvent class, and this is the wrong one. Removing this line should fix the error. (The correct ActionEvent will then be imported by the line import java.awt.event.*; which you already have in your code.)
I'm trying to run the class, and it takes more than a minute to come to main() method.
I've imported around 140 jars, which is required to call my internal APIs. So, will it always takes more time, to load all the jars before coming to main method ?
Is it a way to minimize the execution time ?
This is how, my main class looks like, wherein I've imported most of the jars.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.jdesktop.swingx.JXDatePicker;
import org.w3c.dom.Document;
import com.thd.custom.util.XMLUtil;
import com.yantra.interop.japi.YIFApi;
import com.yantra.interop.japi.YIFClientFactory;
import com.yantra.ycp.core.YCPContext;
import com.yantra.yfc.dom.YFCDocument;
import com.yantra.yfc.dom.YFCElement;
import com.yantra.yfc.dom.YFCNodeList;
import com.yantra.yfs.core.YFSObject;
public class OrderStatCompUI extends JPanel {
private static final long serialVersionUID = 1L;
private static JTextField[] fields;
private JXDatePicker[] datepicker;
private static JCheckBox chkEcommOrder;
private static JCheckBox chkStoreOrder;
protected static JTable tblOrdStatus;
private JScrollPane spOrdStat;
protected static DefaultTableModel model;
private ArrayList<Boolean> setColor = new ArrayList<Boolean>();
private ArrayList<String> transferOrderNo = new ArrayList<String>();
private static JButton btnSlctRow;
private static JButton btnAllRow;
private JPanel btmPnl;
private YCPContext env;
private static JButton btnExport;
I've also tried to create 2 class, wherein main class, will have only UI part and other class has the behavior part. When I do so, main() method got executed, within a second, but when I do some action in the console, and when the execution goes to behavior class, it takes again a minute to load and populate the data.
Could anyone help me in this regard ?
Try to set the JVM to run in client mode which has much faster startup times. If it is running in server mode (possible if you have a system with big specs (memory etc) then the startup time is slower.
I am writing a java program for my OOP class, and i am trying to add some actionlisteners, but for some reason i keep getting this error "BattleshipUI.ExitListener is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener", the following code is how i have my actionlistener constructed.
public class ExitListener implements ActionListener {
public void actionPerformed(ActionEvent e){
int response = JOptionPane.showConfirmDialog(null,"Are you sure you want to exit?","Exit",JOptionPane.YES_NO_OPTION);
if (response == JOptionPane.YES_OPTION){
System.exit(0);
}
}
}
I have about 4 of these in my code and i am getting the same error on each one.
Any help would be much appreciated.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import javafx.event.ActionEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
Change javafx.event.ActionEvent to java.awt.event.ActionEvent in your import or change
public void actionPerformed(ActionEvent e){
int response = JOptionPane.showConfirmDialog(null,"Are you sure you want to exit?","Exit",JOptionPane.YES_NO_OPTION);
if (response == JOptionPane.YES_OPTION){
System.exit(0);
}
}
to
public void actionPerformed(java.awt.event.ActionEvent e){
int response = JOptionPane.showConfirmDialog(null,"Are you sure you want to exit?","Exit",JOptionPane.YES_NO_OPTION);
if (response == JOptionPane.YES_OPTION){
System.exit(0);
}
}
if you need to keep the javafx import.