JButton does not open JDialog - java

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!

Related

JTextField not visible in invoked method

I'm trying to populate a Panel with a textfield and a label, the label is being reflecting as expected, however the textfield is not showing up.
Below is the code that is being used:
package qmutility;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class panetest1
{
public static void main(String[] args)
{
createSubframe();
}
public static void createSubframe()
{
final JFrame subframe = new JFrame("Object Choice");
subframe.setSize(1000, 500);
subframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
subframe.getContentPane().setLayout(new GridLayout(1, 1));
JTextArea out = new JTextArea();
out.setEditable (false);
JScrollPane jp = new JScrollPane(out);
jp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
JPanel queue = new JPanel();
JLabel lblqname = new JLabel("Please enter the queue name");
JTextField txtqname = new JTextField(20);
queue.add(lblqname, txtqname);
JPanel chl = new JPanel();
tabbedPane.addTab("Queues", queue);
tabbedPane.addTab("Channels", chl);
subframe.getContentPane().add(tabbedPane);
subframe.getContentPane().add(jp);
tabbedPane.setVisible(true);
subframe.setVisible(true);
}
}
Edit: Attached the screengrabresult
Try changing the Layout of your panel.
Like, queue.setLayout(new FlowLayout());
or Add components to panel one by one,
queue.add(lblqname);
queue.add(txtqname);
The method Container.add(Component comp,Object constraints) adds a given Component with given constraints, it is not meant to add two Component at once.
Replace :
queue.add(lblqname, txtqname);
With
queue.add(lblqname);
queue.add(txtqname);

java not displaying jpanel in jframe after button press

I am simply making a user interface and all i want it to do after the button is pressed is display thanks... I am pretty new to this but from what i see there are no errors? I have tried playing around with the set visible and to no avail...Any help is great thanks
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JList;
public class GuiApp1 {
public static void main(String args[]) {
String title = (args.length == 0 ? "CheckBox Sample" : args[0]);
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel(new GridLayout(0, 1));
Border border = BorderFactory.createTitledBorder("Pizza Toppings");
panel.setBorder(border);
JLabel label1 = new JLabel("Enter name below:");
panel.add(label1);
JTextField field = new JTextField(20);
panel.add(field);
JCheckBox check = new JCheckBox("Car0");
panel.add(check);
check = new JCheckBox("Car1");
panel.add(check);
check = new JCheckBox("Car2");
panel.add(check);
check = new JCheckBox("Car3");
panel.add(check);
check = new JCheckBox("Car4");
panel.add(check);
JButton button = new JButton("Submit");
final JPanel listPanel = new JPanel();
listPanel.setVisible(false);
JLabel listLbl = new JLabel("Vegetables:");
listPanel.add(listLbl);
button.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
listPanel.setVisible(!listPanel.isVisible());
panel.setVisible(!panel.isVisible());
}
});
Container contentPane = frame.getContentPane();
contentPane.add(panel, BorderLayout.CENTER);
contentPane.add(button, BorderLayout.SOUTH);
frame.setSize(300, 300);
frame.setResizable(true);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
}
The reason for the vegetables panel not appearing is simple: Xou never add ist to the contentPane.
For the code to function properly you need to add/remove the panels in the ActionListener of the button:
button.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
listPanel.setVisible(!listPanel.isVisible());
panel.setVisible(!panel.isVisible());
if (listPanel.isVisible()) {
contentPane.remove(panel); // Vegetables are visible, so remove the Cars
contentPane.add(listPanel, BorderLayout.CENTER); // And add the Vegetables
} else {
contentPane.remove(listPanel); // Vice versa
contentPane.add(panel, BorderLayout.CENTER);
}
}
});
Then, you need to move the ActionListener below the contentPane declaration and make it final.
Also you should consider putting the different checkboxes is different variables, so you can read the state of them. If you don't want to have so many variables hanging you could put them into an array.
JCheckBox[] checks = new JCheckbox[5];
checks[0] = new JCheckBox("Car0");
panel.add(checks[0]);
...

View.Listener is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener

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.

How can we put value on text field on output screen?

I want to put value in txtf1 at output screen and get it. How can we put value on text field on output screen?
import java.awt.Color;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class demog extends JPanel implements ActionListener{
private TextField textf, txtf1;
public void jhand(){
textf = new TextField();
textf.setSize(40, 40);
textf.setText("20");
textf.setEditable(false);
textf.setBackground(Color.WHITE);
textf.setForeground(Color.BLACK);
//textf.setHorizontalAlignment(SwingConstants.CENTER);
textf.setLocation(15, 15);
//textf.addActionListener(this);
txtf1 = new TextField();
txtf1.setSize(40, 40);
txtf1.getText();
txtf1.setEditable(false);
txtf1.setBackground(Color.WHITE);
txtf1.setForeground(Color.BLACK);
//txtf1.setHorizontalAlignment(SwingConstants.CENTER);
txtf1.setLocation(50, 50);
JFrame frame = new JFrame("demo");
JPanel p = new JPanel();
p.setOpaque(true);
p.setBackground(Color.WHITE);
p.setLayout(null);
frame.setContentPane(p);
frame.setSize(500,500);
frame.setVisible(true);
p.add(textf);
p.add(txtf1);
}
public void actionPerformed(ActionEvent evt) {
String text = textf.getText();
System.out.println(text);
}
public static void main(String... args){
demog g = new demog();
g.jhand();
}
}
You have to change some of your code in order to work. You had some problem in your code which I resolved them for you in the following code. See the comments to learn some in swing ;-)
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
// Use upper Case in the start of you class names:
public class Demog extends JPanel implements ActionListener {
private JTextField textf, txtf1;
public Demog() {
jhand();
}
public void jhand() {
setLayout(new FlowLayout()); // Always set the layout before you add components
// you can use null layout, but you have to use setBounds() method
// for placing the components. For an advanced layout see the
// tutorials for GridBagLayout and mixing layouts with each other.
textf = new JTextField(); // Do not mix AWT component with
// Swing (J components. See the packages)
//textf.setSize(40, 40); // Use setPreferredSize instead
textf.setPreferredSize(new Dimension(40, 40));
textf.setText("20");
textf.setEditable(false); // Text fields are for getting data from user
// If you need to show something to user
// use JLabel instead.
textf.setBackground(Color.WHITE);
textf.setForeground(Color.BLACK);
add(textf);
txtf1 = new JTextField();
//txtf1.setSize(40, 40); Use setPreferredSize instead
txtf1.setPreferredSize(new Dimension(40, 40));
txtf1.getText();
txtf1.setEditable(false);
txtf1.setBackground(Color.WHITE);
txtf1.setForeground(Color.BLACK);
add(txtf1);
JButton b = new JButton("Click ME!");
b.addActionListener(this);
add(b);
}
public void actionPerformed(ActionEvent evt) {
String text = textf.getText();
JOptionPane.showMessageDialog(Demog.this, "\"textf\" text is: "+text);
}
public static void main(String[] args) {
JFrame frame = new JFrame("demo");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Demog p = new Demog();
p.setBackground(Color.WHITE);
frame.setContentPane(p);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
Good Luck.

How to make radio buttons change text dynamically in Java

I'm fairly new to GUI. I'm trying to make it so that depending on which radio button is selected, a JLabel changes its value. For example, if "id" is selected, it'll display "http://steamcommunity.com/id/" and if "profile" is selected, it'll display "http://steamcommunity.com/profiles/". I have some code up and running and it's nearly complete:
package sgt;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class RadioButtonPrompt extends JPanel
implements ActionListener {
private static final long serialVersionUID = 1L;
static String idString = "ID";
static String profileString ="Profile";
static String type = idString;
public RadioButtonPrompt() {
super(new BorderLayout());
// Create radio buttons.
JRadioButton idButton = new JRadioButton(idString, true);
idButton.setMnemonic(KeyEvent.VK_I);
idButton.setActionCommand(idString);
JRadioButton profileButton = new JRadioButton(profileString);
profileButton.setMnemonic(KeyEvent.VK_P);
profileButton.setActionCommand(profileString);
// Group radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(idButton);
group.add(profileButton);
idButton.addActionListener(this);
profileButton.addActionListener(this);
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
radioPanel.add(idButton);
radioPanel.add(profileButton);
JPanel textPanel = new JPanel ();
JLabel URL = new JLabel(setJLabelValue());
JTextField text = new JTextField("sampletextfield");
text.setPreferredSize(new Dimension(100, 20));
textPanel.add(URL);
textPanel.add(text);
JPanel buttonPanel = new JPanel(new GridLayout(1, 0));
JButton submit = new JButton("Submit");
submit.setMnemonic(KeyEvent.VK_S);
buttonPanel.add(submit);
add(radioPanel, BorderLayout.LINE_START);
add(textPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.PAGE_END);
setBorder(BorderFactory.createCompoundBorder());
}
private String setJLabelValue() {
if (type.equals("ID")) {
return "http://steamcommunity.com/id/";
}
return "http://steamcommunity.com/profiles/";
}
public void actionPerformed(ActionEvent e) {
// Returns either "Profile" or "ID"
type = ((JRadioButton)e.getSource()).getText();
System.out.println(type);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Steam Game Tracker");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new RadioButtonPrompt();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Take a look at this SO thread.
in actionPerformed() you need to textpanel.setText() to whatever you want based on which button was clicked. I'm guessing at the method name, haven't done any UI stuff with Java for a while.

Categories