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.)
Related
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 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.
I've been trying to set fixed column width in the following code for ages...The thing is that when I add a label to the panel on the left, then the width is incremented automatically...and i Would like the column width to be fixed...
Could anyone help me please??
This is the code:
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import java.io.IOException;
public class CurrentItem extends JPanel{
private static JFrame currentScreen;
public CurrentItem(JFrame p) {
GridBagConstraints c = new GridBagConstraints();
//Borramos todo lo que haya en la pantalla
this.removeAll();
this.revalidate();
currentScreen = p;
this.setBackground(Color.LIGHT_GRAY);
this.setLayout(new GridBagLayout());
currentScreen.add(this);
// --->
Panel leftPanel = new Panel();
leftPanel.setBackground(Color.BLACK);
c.weightx = 1.5;
c.weighty = 1;
//c.fill = GridBagConstraints.BOTH;
this.add(leftPanel, c);
// <---
// --->
Panel secondPanel = new Panel();
secondPanel.setBackground(Color.green);
c.weightx = 0.25;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
this.add(secondPanel, c);
// <---
// --->
Panel rightPanel = new Panel();
rightPanel.setBackground(Color.CYAN);
c.weightx = 1.5;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
this.add(rightPanel, c);
// <---
currentScreen.setVisible(true);
this.requestFocusInWindow();
}
}
GridBagLayout places components in rectangles (cells) in a grid, and then uses the components' preferred sizes to determine how big the cells should be.
Weights are used to determine how to distribute space among columns (weightx) and among rows (weighty); this is important for specifying resizing behavior.
Generally weights are specified with 0.0 and 1.0 as the extremes: the numbers in between are used as necessary.
Read more How to Use GridBagLayout
Sample code: (below code set fixed width column with 100% vertical size.)
Panel leftPanel = new Panel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(..., ...);
}
};
Remove weightx for first column and override getPreferredSize() method.
leftPanel.setBackground(Color.BLACK);
c.weighty = 1;
c.fill = GridBagConstraints.VERTICAL;
this.add(leftPanel, c);
If you know ahead of time the labels that you'll be adding, and how wide each one is:
You can call label.getPreferredSize().width on each label to determine the width of the largest label.
Then, add an invisible component (Box.createHorizontalStrut(width)) to the column to force it to be that wide at all times.
Then, when you add all of your labels, the width of that column of the panel will not change.
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)
I am using a DAO Factory to get my Data from the database. When i am running my program the data is showing in the printline so he is getting it out of the database. But i want it to show in my JTable but this one is empty and i dont know how to fill it.
Code JFrame:
package View;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;
import javax.swing.table.TableModel;
import Controller.VerwijderController;
import Model.OefeningenListModel;
import Model.OefeningenTableModel;
public class VerwijderenHome extends JFrame {
private JList LijstOefening;
private JScrollPane jScrollPane1;
private Container window = getContentPane();
private JButton delete;
private VerwijderController Controller;
private JTable tabel;
public VerwijderenHome()
{
initGUI();
}
public void addDeleteListener(ActionListener a){
delete.addActionListener(a);
}
private void initGUI() {
setPreferredSize(new Dimension(800, 600));
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(null);
setVisible(true);
JTable table = new JTable(new OefeningenTableModel());
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(50, 50, 300, 60);
window.add(scrollPane);
delete = new JButton("Delete");
delete.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
delete.setBounds(50, 265, 100, 30);
window.add(delete);
pack();
Controller = new VerwijderController();
addDeleteListener(Controller);
}
public JButton getDelete(){
return delete;
}
public JList getLijst()
{
return LijstOefening;
}
}
Code DefaultTableModel?? (Dont know if i must use this one)
package Model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import datapackage.DAOFactory;
public class OefeningenTableModel extends DefaultTableModel {
ArrayList<Oefening> oefeningen;
public OefeningenTableModel(){
oefeningen = DAOFactory.getFactory(0).getIDAOOefening().load();
}
}
The DefaultTableModel is backed by a Vector representing the columns and rows of the table model.
You've provided your own ArrayList of objects, but you've not overridden the methods you will require to supply that data back to the table. The DefaultTableModel has no idea of your ArrayList
Try overridding some of the following;
public class OefeningenTableModel extends AbstractTableModel {
ArrayList<Oefening> oefeningen;
public OefeningenTableModel(){
oefeningen = DAOFactory.getFactory(0).getIDAOOefening().load();
}
#Override
public int getRowCount() {
return oefeningen.getSize();
}
#Override
public int getColumnCount() {
// You'll need to fill this out to meet your requirements
}
#Override
public String getColumnName(ing column) {
// You'll need to fill this out to meet your requirements
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
// You'll need to fill this out to meet your requirements
}
}
Take the time to have read through How to use Tables.