JButton does not appear on JDialog - java

I have a problem in this code that is, I have added a JButton on JDialog but the button is not visible when dialog appears. Please help.
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class A implements ActionListener {
JFrame f = new JFrame();
public A() {
JButton b = new JButton("JDialog");
f.add(b);
b.addActionListener(this);
f.setVisible(true);
f.setSize(500,500);
}
public static void main(String arg[]) {
new A();
}
public void actionPerformed(ActionEvent e) {
JDialog d = new JDialog(f,"Dialog",true);
d.setSize(100,100);
d.setVisible(true);
d.setLayout(new FlowLayout());
JButton b = new JButton("OK");
d.add(b);
}
}

add button before calling to setvisible(true).
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class A implements ActionListener {
JFrame f = new JFrame();
public A() {
JButton b = new JButton("JDialog");
f.add(b);
b.addActionListener(this);
f.setVisible(true);
f.setSize(500,500);
}
public static void main(String arg[]) {
new A();
}
public void actionPerformed(ActionEvent e) {
JDialog d = new JDialog(f,"Dialog",true);
d.setSize(100,100);
d.setLayout(new FlowLayout());
JButton b = new JButton("OK");
d.add(b);
d.setVisible(true);
}
}

I believe your Component is rendered before you add your button to it. Try adding the button before you render your Component. Try to add it before you call setVisible or repaint your Component.

Related

How to have functional button in Java?

I am new in programing and I need help.
How to have functional buttons in Java?
Check test code here:
import java.awt.BorderLayout;
import javax.swing.DefaultButtonModel;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class test {
test(){
JFrame frame = new JFrame("Flow Layout");
JButton button;
button = new JButton("button 1");
frame.add(button);
frame.setLayout(new FlowLayout());
frame.setSize(300,300);
frame.setVisible(true);
}
public static void main(String[] args) {
new test();
}
}
you need to add an ActionListaner to your button, in which you define what to do when it is clicked
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// do whatever
}
});

How do I change the background colour in the Play class?

I want to change the color of the background and a clear window without creating a new JFrame. Any suggestions?
import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Dodge EM");
frame.setSize(1000, 700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
placeComponents(frame);
frame.setVisible(true);
frame.getContentPane().setBackground(Color.black);
}
private static void placeComponents(JFrame frame) {
frame.setLayout(null);
JLabel dodgeEM = new JLabel("Dodge EM");
dodgeEM.setForeground (Color.RED);
dodgeEM.setFont(new Font("Serif", Font.BOLD, 30));
dodgeEM.setBounds(440,10,300,150);
frame.add(dodgeEM);
JButton playButton = new JButton("Play");
playButton.setBounds(460,150,95,30);
frame.add(playButton);
ActionListener play = new Play();
playButton.addActionListener(play);
JButton scoresButton = new JButton("Scores");
scoresButton.setBounds(460,250,95,30);
frame.add(scoresButton);
JButton helpButton = new JButton("Help");
helpButton.setBounds(460,350,95,30);
frame.add(helpButton);
JButton quitButton = new JButton("Quit");
quitButton.setBounds(460,450,95,30);
frame.add(quitButton);
}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Play extends JFrame implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
//JOptionPane.showMessageDialog(null, "Play button has been pressed");
this.getContentPane().setBackground(Color.red);
}
}
Any suggestions are much appreciated.
Rather then creating a new class, you can add the action listener to your button like shown below
playButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
//do stuff onclick
frame.getContentPane().setBackground(Color.yellow);
}
});

Java Swing method addActionListener error

I am new to Java and trying to create a simple Swing program with two buttons, but I'm getting an error with addActionListener.
public class swingEx1
{
JFrame f;
JPanel p;
JLabel l;
JButton b1,b2;
public swingEx1()
{
f = new JFrame("Swing Example");
p = new JPanel();
l = new JLabel("Initial Label");
b1 = new JButton("Clear");
b2 = new JButton("Copy");
}
public void launchFrame()
{
p.add(b1,BorderLayout.SOUTH);
p.add(b2,BorderLayout.EAST);
p.add(l,BorderLayout.NORTH);
p.setSize(200,300);
f.getContentPane().add(p);
b1.addActionListener(new ClearButton());
b2.addActionListener(new CopyButton());
f.pack();
f.setVisible(true);
}
public static void main(String args[])
{
swingEx1 swObj1 = new swingEx1();
swObj1.launchFrame();
}
}
class ClearButton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Button b1 = (Button) e.getSource();
b1.setLabel("It it Clear Button");
}
}
class CopyButton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Button b2 = (Button) e.getSource();
b2.setLabel("It it Copy Button");
}
}
Line b1.addActionListener(new ClearButton()) produces error:
The method addActionListener(ActionListener) in the type
AbstractButton is not applicable for the arguments (ClearButton)
Line b2.addActionListener(new CopyButton()) produces error:
The method addActionListener(ActionListener) in the type
AbstractButton is not applicable for the arguments (CopyButton)
In your two inner classes you should be using JButton and you are using Button. This is probably why you are getting your exception. I ran your code with the recommended changes and I got no error.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class swingEx1 {
JFrame f;
JPanel p;
JLabel l;
JButton b1, b2;
public swingEx1() {
f = new JFrame("Swing Example");
p = new JPanel();
l = new JLabel("Initial Label");
b1 = new JButton("Clear");
b2 = new JButton("Copy");
}
public void launchFrame() {
p.add(b1, BorderLayout.SOUTH);
p.add(b2, BorderLayout.EAST);
p.add(l, BorderLayout.NORTH);
p.setSize(200, 300);
f.getContentPane().add(p);
b1.addActionListener(new ClearButton());
b2.addActionListener(new CopyButton());
f.pack();
f.setVisible(true);
}
public static void main(String args[]) {
swingEx1 swObj1 = new swingEx1();
swObj1.launchFrame();
}
}
class ClearButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton b1 = (JButton) e.getSource();
b1.setLabel("It it Clear Button");
}
}
class CopyButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton b2 = (JButton) e.getSource();
b2.setLabel("It it Copy Button");
}
}
I adjusted your ActionListener class (which is a separate defaulted-access class in your code) to an inner class and changed Button to JButton. It works.
class ClearButton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JButton b1 = (JButton) e.getSource();
b1.setLabel("It it Clear Button");
}
}
The GUI appears as follows:
1. Before clicking:
Panel before clicking
2.After clicking:
panel after clicking
I changed my imports from:
import javax.swing.*;
import java.awt.*;
to:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
This fixed the problem. Not sure why the original imports didn't work.

How can I add text to combobox from textfield?

I want to create a combobox and a textbox. And user will enter a text into the textbox, and the text will be added as an item of combobox.
How can I do it? I wrote a code, but I couldn't find what will I write in actionlistener.
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Q2 extends JFrame {
JTextField t;
JComboBox combobox = new JComboBox();
public Q2() {
t = new JTextField("Enter text here", 20);
t.setEditable(true);
t.addActionListener(new act());
add(t);
add(combobox);
combobox.addItem(t.getText().toString());
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
setLocationRelativeTo(null);
setVisible(true);
}
public class act implements ActionListener {
public void actionPerformed(ActionEvent e) {
}
}
public static void main(String[] args) {
Q2 test = new Q2();
}
}
I added a button, and put the functionality on adding to the JComboBox on the button. Here's an example:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Q2 extends JFrame {
JTextField t;
JComboBox combobox;
JButton b;
public Q2() {
combobox = new JComboBox();
t = new JTextField("Enter text here", 20);
t.setEditable(true);
b = new JButton("Add");
b.addActionListener(new act()); //Add ActionListener to button instead.
add(t);
add(combobox);
add(b);
//combobox.addItem(t.getText().toString()); //Moved to ActionListener.
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
setLocationRelativeTo(null);
setVisible(true);
}
public class act implements ActionListener {
public void actionPerformed(ActionEvent e) {
combobox.addItem(t.getText()); //Removed .toString() because it returns a string.
}
}
public static void main(String[] args) {
Q2 test = new Q2();
}
}
private JTextComponent comboboxEditor;
Vector ComboData = new Vector();
public void addActionListners() {
//adding action listner to the NameComboBox
this.comboboxEditor = (JTextComponent) yourCombo.getEditor().getEditorComponent();
comboboxEditor.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent evt) {
int i = evt.getKeyCode();
if (i == 10) {
//combobox action on enter
ComboData.add(comboboxEditor.getText());
yourCombo.setListData(ComboData);
}
}
});
}
you have to set your editable property in comboBox to true otherwise you wont able to write on comboBox. make sure you call addActionListners() method
at the startup(constructor). i am giving the combobox the functionality of a jtext field by changing the editor of the combobox to jtextComponent. try this example

Why is control TAB keystroke not working?

Following is my code
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.util.Arrays;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
public class Test {
private int selected = 0;
public Test() {
JLabel[] lables = new JLabel[] { new JLabel("Lable1"),
new JLabel("Label2") };
JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = (JPanel) frame.getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(lables[0]);
contentPane.add(lables[1]);
frame.pack();
contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke("control TAB"), "next");
Action action = new AbstractAction("next") {
private static final long serialVersionUID = 1L;
#Override
public void actionPerformed(ActionEvent e) {
selected++;
Arrays.stream(lables).forEach(
l -> l.setForeground(Color.LIGHT_GRAY));
lables[selected % 2].setForeground(Color.BLACK);
System.out.println(selected);
contentPane.revalidate();
contentPane.repaint();
}
};
contentPane.getActionMap().put("next", action);
frame.setVisible(true);
}
public static void main(String[] args) {
new Test();
}
}
But when I press control TAB, nothing happens. But when if I change the code to something like KeyStroke.getKeyStroke("control K"), "next"); the control K keystorke changes the foreground color.
What am I doing wrong? Is the "control Tab" reserved for some purpose? If so, how can I change that behaviour so that I can bind that keystroke to my action?

Categories