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();
}
}
Related
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.
How can I modify the window menu of a JFrame in Java ? That's the one (in Windows) at the top left, behind the application icon, that has items such as 'Restore', 'Move', 'Minimize', 'Resize'...
Unfortunately I've only found a way to do this with the "metal decoration" (with that I mean doing JFrame.setDefaultLookAndFeelDecorated(true);). I will of course update the answer if I find one with the system LaF, but I think this is still worth an answer.
Output:
Code:
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.io.BufferedReader;
import java.io.UnsupportedEncodingException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class Example {
public Example() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame();
JMenu systemMenu = getSystemMenu(frame);
systemMenu.add(new JMenuItem("New JMenuItem"), 0);
for (Component component : systemMenu.getPopupMenu().getComponents()) {
if (component.toString().contains("JMenu")) {
((JMenuItem) component).setForeground(Color.RED);
}
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JMenu getSystemMenu(JFrame frame) {
for (Component c1 : frame.getLayeredPane().getComponents()) {
if (c1.toString().contains("MetalTitlePane")) {
for (Component c2 : ((Container) c1).getComponents()) {
if (c2.toString().contains("SystemMenuBar")) {
return (JMenu) ((Container) c2).getComponent(0);
}
}
}
}
return null;
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Example();
}
});
}
}
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.
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)