Frame with ComboBox not appearing - java

I would like that when I click on a button it open a Frame containing a combo box, but the frame does not appears. I'm using AWT.
public class ActionF extends Frame implements ActionListener {
public void actionPerformed(ActionEvent evt) {
setLayout(null);
setBackground(Color.blue);
setBounds(100, 200, 900, 450);
Choice choice = new Choice();
choice.addItem("Choice 1");
choice.addItem("Choice 2");
choice.addItem("Choice 3");
add(choice);
setVisible(true);
}
}
Can you tell me what's wrong?
Thanks in advance.

The code you provided is missing some essential information, e.g. the button that is supposed to open your frame.
A shot in the dark: Could it be possible, that you forgot to add the ActionListener to the actual button instance? This should do it:
public static void main(String[] args) {
Frame f = new Frame();
Button button = new Button();
ActionF actionF = new ActionF();
button.addActionListener(actionF);
f.add(button);
f.setVisible(true);
}

Related

The JButton is not responding, why?

I'm trying to make button show a message when pressed, it's not working. Can anyone tell me what I missed?
In the end I have the KeyListener and the if for JOptionPane, but the website is not letting me post it (I'm new to this).
Anyway, it would be really nice if someone could tell me what I'm doing wrong, thanks.
public javalearning(){
FlowLayout f = new FlowLayout();
setLayout(f);
this.setSize(200,200);
JFrame j = new JFrame();
this.setTitle("this is a tittle");
JButton button = new JButton();
button.setText("Button");
this.add(button);
JButton button2 = new JButton();
button2.setText("Button2");
this.add(button2);
this.setVisible(true);
}
please follow example in this code and you will be fine. If at the end of the day you are unable to resolve it. you can write back. i believe this will help you.
import javax.swing.*;
import java.awt.event.*;
public class ChangeButtonLabel{
JButton button;
public static void main(String[] args){
ChangeButtonLabel cl = new ChangeButtonLabel();
}
public ChangeButtonLabel(){
JFrame frame = new JFrame("This is a Frame");
button = new JButton("Button");
button.addActionListener(new MyAction());
frame.add(button);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class MyAction implements ActionListener{
public void actionPerformed(ActionEvent e){
String text = (String)e.getActionCommand();
if (text.equals("Button2")){
button.setText("I am Sectona");
}
else{
button.setText("Click Me");
}
}
}
}
You state:
In the end I have the KeyListener and the if for JOptionPane,
As the tutorial that I've linked to in my comment will explain, you don't use KeyListeners with JButtons but rather ActionListeners.
e.g.,
myButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
System.out.println("Button pressed");
}
});
You state:
but the website is not letting me post it (I'm new to this).
This site will let you post any reasonable amount of code. If you're having problems posting it, tell us the specifics of what's wrong, and maybe we can help you. Again, if you're trying to post code as an image, don't. It should be text that is formatted as code, not an image. But most important, don't keep us in the dark, or we can't help you.
like Hovercraft said, you will need to set ImagIcon(String image_name)
The code below will help you in embedding image on JButton. Give me a shout if you still find it difficult to integrate
import javax.swing.*;
import java.awt.*;
public class IconButton{
public static void main(String[] args){
JFrame frame = new JFrame("Icon on button");
JButton button = new JButton("Image button fro Sectona");
Icon imgicon = new ImageIcon("sectona.gif");
JPanel panel = new JPanel();
button.setIcon(imgicon);
panel.add(button);
frame.add(panel, BorderLayout.NORTH);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

JWindow does not receive events in Java 7 for Mac

See the sample code below. It simply creates a button and adds it to a window. But when *menu_item3* is selected, the ActionListener doesn't receive the event. This error only occurs on Java 7 for Mac. If I run this same code in Windows, it works fine. When I run this same code on Java 6 for Mac, it works fine. If I use a JFrame instead of JWindow, it works fine. I do not want to use a JFrame because I do not want to display the window title bar and border.
Any ideas?
public class SandBox {
public static JFrame frame = new JFrame();
public static JPopupMenu menu = new JPopupMenu();
public static JLabel button = new JLabel();
public static void main(String[] args) {
JFrame window = new JFrame();
JPanel panel = new JPanel();
JMenuItem menu_item1 = new JMenuItem("Item1");
JMenuItem menu_item2 = new JMenuItem("Item2");
JMenuItem menu_item3 = new JMenuItem("Item3");
menu.add(menu_item1);
menu.add(menu_item2);
menu.add(menu_item3);
menu.setEnabled(true);
button.setText("Button");
button.setBorder(BorderFactory.createMatteBorder(4, 4, 4, 4, new Color(255,0,0)));
button.setSize(100, 24);
button.add(menu);
button.setVisible(true);
button.setEnabled(true);
panel.add(button);
panel.setVisible(true);
window.add(panel);
window.setVisible(true);
window.setLocation(100, 100);
window.setAlwaysOnTop(true);
window.setFocusable(true);
window.setFocusableWindowState(true);
window.pack();
frame.setVisible(false);
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
menu.show(button, 0, 0);
}
});
menu_item3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(frame, "Eggs are not supposed to be green.");
}
});
}
}
I have submitted a bug with Oracle. Still waiting for their response whether the bug will be officially filed. I will update this answer when I do hear something.
In the mean time, I did find a viable workaround. I use a JFrame instead of a JWindow. I was unaware you can remove the window title and borders of a JFrame using the method setUndecorated(). Also be aware that this method can only be called while the frame isn't displayable.

How can I minimize/iconify a JWindow in Java?

My app has a JWindow that needs to be minimized when the custom minimizer button clicked.
Please reply if anyone knows how to minimize a JWindow. I have searched a lot but couldn't find any suitable method to minimize.
I know how to minimize a JFrame. So please don't bother answering regarding JFrame.
Thanks.
I know you don't want to hear this, but the terrible truth is that there is no big difference between undecorated jframes (with setstate methods) and jwindows... :)
JFrame f = new JFrame("Frame");
f.setUndecorated(true);
Due to the fact that a JWindow is not decorated with any control icons, no setState method is provided. One workaround is to allow your custom minimizer button to set the window visible as required:
public class JWindowTest extends JFrame {
JWindow window = new JWindow();
JButton maxMinButton = new JButton("Minimize Window");
public JWindowTest() {
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
maxMinButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (window.isVisible()) {
maxMinButton.setText("Restore Window");
} else {
maxMinButton.setText("Minimize Window");
}
window.setVisible(!window.isVisible());
}
});
add(maxMinButton);
window.setBounds(30, 30, 300, 220);
window.setLocationRelativeTo(this);
window.add(new JLabel("Test JWindow", JLabel.CENTER));
window.setVisible(true);
}
public static void main(String[] args) {
new JWindowTest().setVisible(true);
}
}

Unable to set button's location in java swing application

I am new to Java and was trying to develop a basic swing application. I wanted to set the location of the button on the JFrame. I tried to do this but was unable to do this this is my code. I am using eclipse for development
public class MyUI extends JFrame {
JButton button1 = new JButton("Click");
JTextField tb1 = new JTextField(5);
JPanel panel1 = new JPanel();
public MyUI() {
super("Test");
setVisible(true);
this.setLayout(null);
panel1.setLayout(null);
panel1.setVisible(true);
button1.setVisible(true);
panel1.add(button1);
add(panel1);
panel1.setLocation(10, 10);
button1.setLocation(10, 10);
setDefaultCloseOperation(EXIT_ON_CLOSE);
button1.addActionListener(this);
}
public static void main(String[] args) {
MyUI gui = new MyUI();
gui.setSize(400, 300);
}
}
1.why you put two JComponents to the same Bounds
panel1.setLocation(10, 10);
button1.setLocation(10, 10);
2.have look at Initials Thread
3.public class MyUI extends JFrame {
should be
public class MyUI extends JFrame implements ActionListener{
4.don't extend JFrame, create a local variable
5.setVisible(true); should be (in this form) only last code line into MyUI() constructor
6.setVisible(true); is important issue, you visibled JFrame and then to add JComponent(s)
7.don't use NullLayout, use proper LayoutManager, in the case that you remove this.setLayout(null); and panel1.setLayout(null); added JComponents could be visible
8.use pack() before setVisible(true) as last two code lines in constructor
EDIT (by using built_in LayoutManagers, BorderLayout for JFrame and FlowLayout for JPanel)
import java.awt.event.*;
import javax.swing.*;
public class MyUI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton button1 = new JButton("Click");
private JTextField tb1 = new JTextField(5);
private JPanel panel1 = new JPanel();
public MyUI() {
super("Test");
panel1.add(tb1);
panel1.add(button1);
add(panel1);
setDefaultCloseOperation(EXIT_ON_CLOSE);
button1.addActionListener(this);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
MyUI testing = new MyUI();
}
});
}
}
Your panel and button are not seen because they have zero size. Add something like:
panel1.setSize(100, 100);
button1.setSize(80, 30);
or use the setBounds method which is more convenient to set location and size simultaneously:
panel1.setBounds(10, 10, 100, 100);
button1.setBounds(10, 10, 80, 30);
Would like to suggest something, though its not the direct immediate answer to your question, but still its important from my point of view....
You can use Group Layout which was developed by NetBeans team back in 2005, its awesome to work with.... Try using the Windows Builder Pro which is provided by Google for free now... You can get your application up and running in no time......

Opening double amount of new frames on every click

Have a little problem with some code i have writing to try out something. I have made a frame with a single button in it. When i click on this button, a new frame opens, which it should. I close down the new frame, and then click on the button again, to try see if it still works. The problem starts here, corse insted of opening a single new frame, it opens two new frames. Third time i click it opens 4 frames and so on. I have tried quite a few things, but sadly cant seem to find the reason why it is opening more frames. Please help.
package budget;
import java.awt.event.*;
import javax.swing.*;
public class GUI extends JFrame {
String labelPrefix;
JButton button;
JButton button2;
JLabel label;
public static void main(String[] args) {
JFrame f = new GUI();
f.setExtendedState(f.MAXIMIZED_BOTH);
f.setVisible(true);
}
public GUI() {
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
p.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
button = new JButton("Click Me");
label = new JLabel(labelPrefix);
p.add(button);
this.setTitle("Try");
getContentPane().add(p);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
button.addActionListener(new MyActionListener());
}
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
button.addActionListener(this);
labelPrefix = "Try";
JFrame f2 = new GUI(label, labelPrefix);
f2.setExtendedState(f2.MAXIMIZED_BOTH);
f2.setVisible(true);
}
}
public GUI(JLabel label, String labelPrefix) {
JPanel p2 = new JPanel();
button2 = new JButton("Close");
p2.add(label);
p2.add(button2);
this.setTitle("Try");
getContentPane().add(p2);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pack();
button2.addActionListener(new MyActionListener2());
}
class MyActionListener2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
button2.addActionListener(this);
dispose();
}
}
}
Clearly, the problem is here:
button.addActionListener(this);
Every time you click the button, it adds the listener yet another time to the button.
Simply remove that line and the error will go away. Once a listener is added to a button, it stays there. It isn't "consumed" after being triggered.
Check the first line in the actionPerformed of MyActionListener which states:
button.addActionListener(this);
This line should be removed.

Categories