My code is duplicating the creation of the main screen when I hit the button of the search..the search event should only open one new frame with a textbox to type query. The creation of another frame causes this duplication or is this some sort of bug? I tryed to use the mainFrame to upload the date of the searchPane (p1 in the code) but when I do this, this solves my problem of the window, but the defaultCloseOperation() on the Search Windows causes the close of the entire program - how may I solve this situtation?
thanks in advance
SearchScreen
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SearchScreen extends MainScreen{
JButton btsearch;
JLabel lbsearch;
protected JTextField txtsearch;
JPanel p1;
protected JFrame searchFrame = new JFrame();
public SearchScreen(){
//Button Creation
btsearch= new JButton("Search");
//Label Creation
lbsearch= new JLabel("Type Keywords in english to be searched below:");
//TextBox
txtsearch= new JTextField();
//Pane Creation
p1=new JPanel();
p1.setBackground(Color.gray);
//Pane Components
p1.add(lbsearch);
p1.add(txtsearch);
p1.add(btsearch);
//JFrame Layout Setup
p1.setLayout(new GridLayout(3,3));
btsearch.setEnabled(true);
//Adding JPaneel
searchFrame.add(p1);
//JFrame Setup
searchFrame.setTitle("SHST");
searchFrame.setSize(400, 400);
searchFrame.setVisible(true);
searchFrame.setDefaultCloseOperation(1);
}
}
MainScreen
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class MainScreen implements ActionListener {
JMenuBar bar;
JMenu file, register;
JMenuItem close, search;
JPanel entrance = new JPanel();
JFrame mainFrame = new JFrame();
public MainScreen()
{
bar= new JMenuBar();
file= new JMenu("File");
register= new JMenu("Search");
close= new JMenuItem("Close");
close.addActionListener(this);
search= new JMenuItem("Request Query");
search.addActionListener(this);
//mainFrame Setup
bar.add(file);
bar.add(register);
file.add(close);
register.add(search);
mainFrame.setExtendedState(mainFrame.getExtendedState() | mainFrame.MAXIMIZED_BOTH);
mainFrame.setTitle("SHST");
mainFrame.setJMenuBar(bar);
mainFrame.setDefaultCloseOperation(0);
mainFrame.setVisible(true);
WindowListener J=new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
};
mainFrame.addWindowListener(J);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==close){
System.exit(0);
}
if(e.getSource()==search){
SearchScreen s= new SearchScreen();
}
public static void main (String[] args){
MainScreen m= new MainScreen();
}
}
Your issue is here
if(e.getSource()==search){
SearchScreen s= new SearchScreen();
}
Using inheritance implementation is dangereous, a SearchScreen is a MainScreen then the constructor of this class is always called.
Related
When I click the buttons I added for ok and cancel, actionPerformed does not get called
code:
void testServerFlags()
{
dlgEmpty mdlgCreateBot = new dlgEmpty(null);
mdlgCreateBot.show();
ted=0;
}
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Box;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import Indicator.cGenIndicator;
import Indicator.cIndicatorUtil;
import MyLib.cLib;
public class dlgEmpty extends JDialog implements ActionListener
{
Box parPan, topPan, butPan,centerPan;;
public dlgEmpty(JFrame parent)
{
super(parent,"Indicators", true);
Container content = getContentPane();
content.setLayout(new BorderLayout());
butPan = Box.createHorizontalBox();
JButton okBut = new JButton("OK");
okBut.setActionCommand("ok");
butPan.add(okBut, BorderLayout.SOUTH);
JButton CancelBut = new JButton("Cancel");
CancelBut.setActionCommand("Cancel");
butPan.add(CancelBut, BorderLayout.SOUTH);
content.add(butPan, BorderLayout.SOUTH);
centerPan = Box.createVerticalBox();
content.add(centerPan, BorderLayout.CENTER);
pack();
show();
}
boolean isOk=false;
public void actionPerformed(ActionEvent e)
{
if ("OK".equals(e.getActionCommand()))
isOk=true;
setVisible(false);
}
}
You are setting the action command to:
okBut.setActionCommand("ok");
But are checking for:
if ("OK".equals(e.getActionCommand()))
This won't match, because the case is different.
setActionCommand only sets the message in the ActionEvent. What you need is addActionListener. I have updated your code.
But the other thing you should know is that there is no need for this. Instead use JOptionPane.showConfirmDialog with a JComponent as the message parameter: It does all the button handling and modal in-line waiting automatically
public dlgEmpty(JFrame parent)
{
super(parent,"Indicators", true);
Container content = getContentPane();
content.setLayout(new BorderLayout());
butPan = Box.createHorizontalBox();
JButton okBut = new JButton("OK");
okBut.setActionCommand("ok");
okBut.addActionListener(this); // added this line
butPan.add(okBut, BorderLayout.SOUTH);
JButton CancelBut = new JButton("Cancel");
CancelBut.setActionCommand("Cancel");
CancelBut.addActionListener(this);
butPan.add(CancelBut, BorderLayout.SOUTH); //added this line
content.add(butPan, BorderLayout.SOUTH);
centerPan = Box.createVerticalBox();
content.add(centerPan, BorderLayout.CENTER);
pack();
show();
}
boolean isOk=false;
public void actionPerformed(ActionEvent e)
{
if ("OK".equals(e.getActionCommand()))
isOk=true;
setVisible(false);
}
}
I am building a simple program. I have a class that extends from JDialog and class that extends from JFrame and is GUI of the application. I implemented ActionListener which should open the dialog after clicking on the JButton. Nothing happens though and I can't figure out why.
GUI
package nemocnice_sam;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class App extends JFrame {
JTable tbl = new JTable();
JButton pridejPacienta = new JButton("Přidej pacienta");
JButton smazPacienta = new JButton("Smaž pacienta");
JButton export = new JButton("Export");
JButton konec = new JButton("Konec");
JPanel panel = new JPanel();
PacientDialog novyPacient;
ActionListener al = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == pridejPacienta){
novyPacient = new PacientDialog();
novyPacient.setModal(true);
novyPacient.setVisible(true);
}
}
};
SeznamPacientu pacienti = new SeznamPacientu();
ModelPacientu model = new ModelPacientu(pacienti);
public static void main(String[] args) {
new App();
}
public App() {
setLayout(new BorderLayout());
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
panel.add(pridejPacienta);
panel.add(smazPacienta);
panel.add(export);
panel.add(konec);
add(panel,BorderLayout.NORTH);
tbl.setModel(model);
add(new JScrollPane(tbl), BorderLayout.CENTER);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
}
}
DIALOG CLASS
package nemocnice_sam;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class PacientDialog extends JDialog {
JTextField jmeno = new JTextField();
JTextField prijmeni = new JTextField();
JTextField rc = new JTextField();
JTextField cp = new JTextField();
JButton ok = new JButton("OK");
public PacientDialog(){
setLayout(new GridLayout(5,2));
add(new JLabel("Jméno:"));
add(jmeno);
add(new JLabel("Příjmení:"));
add(prijmeni);
add(new JLabel("RČ:"));
add(rc);
add(new JLabel("ČP:"));
add(cp);
pack();
}
}
You need to define the actionListener in button.
konec.addActionListener(al);
Defining an ActionListener alone is not sufficient.
In order to do its job, that listener must be registered with some component that actually sends Events to that Listener.
So you have it to add to the corresponding button for example, like:
pridejPacienta.addActionListener(al);
Besides: when you do that, you do not need that if (source == check within your action listener. You see, when each button has a distinct listener, then there will not be different sources.
You only need such kinds of checks when you want to attach the same ActionListener to multiple buttons!
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import javafx.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class View extends JFrame{
JTextField tField = new JTextField(10);
JButton sortButton = new JButton("Sort");
JButton shuffleButton = new JButton("Shuffle");
JButton reverseButton = new JButton("Reverse");
JTextArea tArea = new JTextArea();
JLabel label = new JLabel("Enter a number: ");
JPanel upperPanel = new JPanel();
JPanel buttonPanel = new JPanel();
public View(){
setTitle("Exercise 22.2");
setSize(500,150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
sortButton.addActionListener(new Listener());
upperPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
upperPanel.add(label);
upperPanel.add(tField);
buttonPanel.add(sortButton);
buttonPanel.add(shuffleButton);
buttonPanel.add(reverseButton);
add(upperPanel, BorderLayout.NORTH);
add(tArea, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
}
/*Getting error here!*/
public class Listener implements ActionListener{
#Override
public void actionPerformed(ActionEvent e)
{
System.out.println("Hello");
}
}
}
I'm trying to create a class that implements ActionListener to that I can register the class to my buttons.
I'm getting:
View.Listener is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
I've tried implementing as an Anonymous Class and get the same error. I cannot figure it out.
I relied on auto imports and it imported the wrong library for ActionEvent.
import javafx.event.ActionEvent;
I needed import:
java.awt.event.ActionEvent;
My bad.
I have this code below. How do I make this JComboBOx run in an Applet? I'm trying to create an applet that allows the user to select a font and type in that font in a JTextArea. I have been searching for answers and made little progress. The error java.lang.reflect.InvocationTargetException keeps popping up. If you need more specifics please just comment.
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Scanner;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Font_Chooser extends JApplet {
JLabel jlbPicture;
public Font_Chooser(){
// Create the combo box, and set first item as Default
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] names = ge.getAvailableFontFamilyNames();
final JComboBox comboTypesList = new JComboBox(names);
comboTypesList.setSelectedIndex(0);
comboTypesList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox jcmbType = (JComboBox) e.getSource();
String cmbType = (String) jcmbType.getSelectedItem();
jlbPicture.setIcon(new ImageIcon(""
+ cmbType.trim().toLowerCase() + ".jpg"));
System.out.println(comboTypesList.getSelectedItem());
//Font myFont = new Font((String)comboTypesList.getSelectedItem(), Font.BOLD, 12);
}
});
// Set up the picture
jlbPicture = new JLabel(new ImageIcon(""
+ names[comboTypesList.getSelectedIndex()] + ".jpg"));
jlbPicture.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
jlbPicture.setPreferredSize(new Dimension(177, 122 + 10));
// Layout the demo
setLayout(new BorderLayout());
add(comboTypesList, BorderLayout.NORTH);
add(jlbPicture, BorderLayout.SOUTH);
}
public static void main(String s[]) {
JFrame frame = new JFrame("JComboBox Usage Demo");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setContentPane(new Font_Chooser());
frame.pack();
frame.setVisible(true);
}
}
Given my code below, I'm a little confused as to what I am missing in order to allow it to function so that when the user inputs their hexadecimal value with a maximum of five digits, they will get their binary equivalent and the decimal equivalent. I thought the code at the bottom automatically converts given a certain number in hex. Also would it be difficult to store the binary values in an array?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.event.InputMethodListener;
import java.util.Scanner;
import java.text.NumberFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ConvertPanel {
public static void main(String[] args) {
JFrame frame = new JFrame ("Hexadecimal to Binary and Decimal.");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new NumberConverter());
frame.pack();
frame.setVisible(true);
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputMethodListener;
import java.util.Scanner;
import java.text.NumberFormat;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class NumberConverter extends JPanel {
private JLabel binaryLabel = new JLabel();
private JLabel totalone = new JLabel();
private JLabel totaltwo = new JLabel();
private JLabel decimalLabel = new JLabel();
private JTextField hexdecString = new JTextField();
private JButton convert;
public NumberConverter() {
setLayout(new BorderLayout());
setPreferredSize(new Dimension(400, 300));
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JLabel converterName = new JLabel("Hexadecimal Input");
convert = new JButton ("Convert");
convert.addActionListener (new ButtonListener());
JPanel panelName = new JPanel(new GridLayout(2,2));
panelName.add(converterName);
panelName.add(hexdecString);
add(panelName, BorderLayout.NORTH);
add (convert);
JPanel totalPanel = new JPanel(new GridLayout(1,3));
totalPanel.add(new JLabel("Binary:"));
totalone = new JLabel("---- ---- ---- ---- ----");
totalPanel.add(totalone);
totalPanel.add(binaryLabel);
JPanel totalPanel2 = new JPanel(new GridLayout(2,3));
totalPanel2.add(new JLabel("Decimal:"));
totaltwo = new JLabel("------");
totalPanel2.add(totaltwo);
totalPanel2.add(decimalLabel);
JPanel south = new JPanel(new GridLayout(2,1));
south.add(totalPanel);
south.add(totalPanel2);
add(south, BorderLayout.SOUTH);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event){
Integer n = Integer.valueOf(hexdecString.getText(), 16);
decimalLabel.setText(String.valueOf(n));
binaryLabel.setText(Integer.toBinaryString(n));
} {
}
}
}