I am building an application which helps a user navigate a website by giving step by step instructions.
The instructions are given in the form of dialog boxes. I am using Java Swing to create the GUI dialog boxes.
Here is the structure of my code :
MainClass
{
//some selenium code to access the website 'Google.com'.....
InstructionDialog frame1 = new InstructionDialog("Enter 'Hello' in search field");
frame1.setVisible(true);
InstructionDialog frame2 = new InstructionDialog("Click 'Search' button");
frame2.setVisible(true);
InstructionDialog frame3 = new InstructionDialog("'Hello' is displayed in the results");
frame3.setVisible(true);
}
class InstructionDialog extends JFrame {
public String message;
public static javax.swing.JButton btnOk;
public InstructionDialog(String message)
{
//some code for the content pane //
msgArea = new JTextArea();
msgArea.setBounds(12, 13, 397, 68);
contentPane.add(msgArea);
msgArea.setEditable(false);
simpleStepMessage.msgArea.setText(message);
btnOk = new JButton("OK");
btnOk.setBounds(323, 139, 97, 25);
contentPane.add(btnOk);
btnOk.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
OkBtnActionPerformed(evt);
}
});
public void OkBtnActionPerformed(java.awt.event.ActionEvent evt)
{
this.dispose();
}
}
}
The problem when I run this is that all the 3 instances of the InstructionDialog run simultaneously. So I have all the 3 dialog boxes pop up at the same time.
But I want them to run one after another - the second dialog box should not appear until the OK button of the first is pressed, the third dialog box should not appear until the OK button of the second one is pressed.
How can I achieve this ?
Any help will be appreciated. Thanks.
The CardLayout is something I used for similar problems.
It is like a card deck and you can display one after another.
A time ago I had a similar issue. I developed the small library UiBooster. With UiBooster you can create blocking dialogs to ask the user for different inputs.
String opinion = new UiBooster().showTextInputDialog("What do you think?");
Related
I've read the similar questions regarding this problem, tried few methods but none is working.
I have 2 JFrame forms. I want to input information in the first form and submit it to the database. When I click a button, the second form will open and load the information
When I re-input new information in the first form and click the the button again, I want the second form to reload the new information inputted from the database.
This is my code so far.
time t = new time();
private void OrderButtonActionPerformed(java.awt.event.ActionEvent evt) {
if(t.isVisible()){
t.dispose();
t.revalidate();
t.repaint();
t.setVisible(true);
t.setLocationRelativeTo(null);
}
else{
t.setVisible(true);
t.setLocationRelativeTo(null);
}
You don't need to play with JFrames for that. See below example :
JLabel toe = new JLabel("I'm primary text");
JFrame cow = new JFrame("Primary Window");
JPanel cowpanel = new JPanel();
cowpanel.add(toe);
cow.setContentPane(cowpanel);
cow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
cow.pack();
cow.setVisible(true);
JButton tow = new JButton("Change");
tow.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
toe.setText("Hi!, I'm secondary text!");
}
});
JFrame dog = new JFrame("Secondary Window");
JPanel dogPanel = new JPanel();
dog.setContentPane(dogPanel);
dogPanel.add(tow);
dog.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
dog.pack();
dog.setVisible(true);
Clicking the 'Change' button from Frame 2 will change the JLabel's text in Frame 1.
I have two Jframes where frame1 has some text fields and when a button on frame1 is clicked, I open another JFrame which contains a search box and a JTable containing search results.
When I click on a result row on JTable, I want that particular values to be reflected in the frame1 text fields.
I tried passing the JFrame1's object as a parameter but I have no clear idea on how to achieve this.
Any help would be highly appreciated.
Thanks
First of all, your program design seems a bit off, as if you are using a JFrame for one of your windows where you should in fact be using a JDialog since it sounds as if one window should be dependent upon the other.
But regardless, you pass references of GUI objects the same as you would standard non-GUI Java code. If one window opens the other (the second often being the dialog), then the first window usually already holds a reference to the second window and can call methods off of it. The key often is when to have the first window call the second's methods to get its state. If the second is a modal dialog, then the when is easy -- immediately after the dialog returns which will be in the code immediately after you set the second dialog visible. If it is not a modal dialog, then you probably want to use a listener of some sort to know when to extract the information.
Having said this, the details will all depend on your program structure, and you'll need to tell us more about this if you want more specific help.
For a simple example that has one window open another, allows the user to enter text into the dialog windows JTextField, and then places the text in the first window's JTextField, please have a look at this:
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class WindowCommunication {
private static void createAndShowUI() {
JFrame frame = new JFrame("WindowCommunication");
frame.getContentPane().add(new MyFramePanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
// let's be sure to start Swing on the Swing event thread
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
class MyFramePanel extends JPanel {
private JTextField field = new JTextField(10);
private JButton openDialogeBtn = new JButton("Open Dialog");
// here my main gui has a reference to the JDialog and to the
// MyDialogPanel which is displayed in the JDialog
private MyDialogPanel dialogPanel = new MyDialogPanel();
private JDialog dialog;
public MyFramePanel() {
openDialogeBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openTableAction();
}
});
field.setEditable(false);
field.setFocusable(false);
add(field);
add(openDialogeBtn);
}
private void openTableAction() {
// lazy creation of the JDialog
if (dialog == null) {
Window win = SwingUtilities.getWindowAncestor(this);
if (win != null) {
dialog = new JDialog(win, "My Dialog",
ModalityType.APPLICATION_MODAL);
dialog.getContentPane().add(dialogPanel);
dialog.pack();
dialog.setLocationRelativeTo(null);
}
}
dialog.setVisible(true); // here the modal dialog takes over
// this line starts *after* the modal dialog has been disposed
// **** here's the key where I get the String from JTextField in the GUI held
// by the JDialog and put it into this GUI's JTextField.
field.setText(dialogPanel.getFieldText());
}
}
class MyDialogPanel extends JPanel {
private JTextField field = new JTextField(10);
private JButton okButton = new JButton("OK");
public MyDialogPanel() {
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
okButtonAction();
}
});
add(field);
add(okButton);
}
// to allow outside classes to get the text held by the JTextField
public String getFieldText() {
return field.getText();
}
// This button's action is simply to dispose of the JDialog.
private void okButtonAction() {
// win is here the JDialog that holds this JPanel, but it could be a JFrame or
// any other top-level container that is holding this JPanel
Window win = SwingUtilities.getWindowAncestor(this);
if (win != null) {
win.dispose();
}
}
}
You'd do a very similar technique to get information out of a JTable.
And again, if this information doesn't help you, then please tell us more about your program including showing us some of your code. The best code to show is a small compilable example, an SSCCE similar to what I've posted above.
I wrote the following code to have a JPopupMenu that allows multiple selection of different items.
The problem is that, as soon as the mouse enters one of the displayed JCheckboxMenuItems, the JPopupMenu gets closed. This issue doesn't occur if I replace JCheckboxMenuItem with, for example, JLabel but, for sure, JLabel doesn't work for my purpose.
Any idea of what could trigger this issue? Any idea of how this problem can be resolved in a better way? I apologize for the newbie question but I'm not a java developer. Thanks in advance for any help.
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedborder(),"Select Layers");
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
for (MyAction layer : layers) {
JCheckBoxMenuItem box = new JCheckBoxMenuItem(layer);
box.setIcon(new SquareIcon(myColor));
panel.add(box);
}
JPopup popup = new JidePopup();
popup.add(panel)
JButton button = new JButton("Layers");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
popup.show(button,0,button.getHeight())
}
});
Thats in the nature of JPopupMenus. They disappear when the invoker component loses the focus. But I found a little trick here.
Create your own class and extend it from JPopupMenu. Then override the setVisible method that it will only forward true to the super class and create an own method that will setVisible of the super class to false.
public class StayOpenPopup extends JPopupMenu{
public void setVisible(boolean visible){
if(visible == true)
super.setVisible(visible);
}
public void disappear() {
super.setVisible(false);
}
}
Then use it like this in your code
[...]
StayOpenPopup popup = new StayOpenPopup();
popup.add(panel);
[...]
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if(popup.isVisible())
popup.disappear();
else popup.show(button,0,button.getHeight());
}
});
Now one click on button will show it. And it will stay visible until next click on Button.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm new to java and programming in general, and I am writing a program that has a menu in it. (It is a JFrame in java) What it does is when you hit a JButton, it shows an applet on the screen. When the applet is done, it goes to a screen where you can choose to run the applet again or go back to the main menu when you hit a button. The problem is that when you hit the button to go back to the menu, it doesn't. All it does is make neither button clickable.
This is the method I use to draw the menu:
public static void drawMenu()
{
f.add(BOption1);
f.add(BOption2);
}
The two jbuttons are already declared and such in the constructor, and they work fine the first time I run the menu. Then, when you hit one of the buttons, it removes both buttons from the screen with f.remove(...). Does anyone know why it won't work when I call this method a second time?
Edit:
Sorry, I meant to say canvas, not applet.
Edit Edit:
I found the solution to my problem, but thanks anyway.
Here is the code for your main class, Frame:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Frame extends JFrame {
static OnePlayer onePlayer;
static TwoPlayer twoPlayer;
static Frame f;
static JButton BOnePlayer = new JButton("Single Player");
static JButton BTwoPlayer = new JButton("Multiplayer");
static JButton BInstructions = new JButton("Instructions");
static JButton toMenu;
static JButton replay;
public Frame(String name) {
super(name);
this.setTitle(name);
this.setVisible(true);
this.setSize(640, 673);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setLayout(null);
this.setBackground(Color.GRAY);
BOnePlayer.setBounds(120, 150, 400, 100);
BTwoPlayer.setBounds(120, 250, 400, 100);
BInstructions.setBounds(120, 350, 400, 100);
BOnePlayer.setFont(new Font("Comic Sans MS", Font.ITALIC, 20));
BTwoPlayer.setFont(new Font("Comic Sans MS", Font.ITALIC, 20));
BInstructions.setFont(new Font("Comic Sans MS", Font.ITALIC, 20));
BOnePlayer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Container onePane = f.getContentPane();
onePane.setLayout(new GridLayout(1, 1));
onePlayer = new OnePlayer();
onePane.add(onePlayer);
onePlayer.init();
f.remove(BOnePlayer);
f.remove(BTwoPlayer);
f.remove(BInstructions);
}
});
BTwoPlayer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Container twoPane = f.getContentPane();
twoPane.setLayout(new GridLayout(1, 1));
twoPlayer = new TwoPlayer();
twoPane.add(twoPlayer);
twoPlayer.init();
f.remove(BOnePlayer);
f.remove(BTwoPlayer);
f.remove(BInstructions);
}
});
BInstructions.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
}
public static void main(String[] args) {
f = new Frame("Snake");
drawMenu();
}
public static void OnePlayerDone(int score) {
}
public static void TwoPlayerDone(int winner, int p1score, int p2score) {
f.remove(twoPlayer);
replay = new JButton("Play Again");
toMenu = new JButton("Return to Menu");
replay.setBounds(120, 100, 400, 100);
toMenu.setBounds(120, 500, 400, 100);
replay.setFont(new Font("Comic Sans MS", Font.ITALIC, 20));
toMenu.setFont(new Font("Comic Snas MS", Font.ITALIC, 20));
f.add(replay);
f.add(toMenu);
replay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Container twoPane = f.getContentPane();
twoPane.setLayout(new GridLayout(1, 1));
twoPlayer = new TwoPlayer();
twoPane.add(twoPlayer);
twoPlayer.init();
}
});
toMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
drawMenu();
}
});
}
public static void drawMenu() {
f.add(BOnePlayer);
f.add(BTwoPlayer);
f.add(BInstructions);
}
}
Suggestions:
First off, rename your class to something other than Frame. This is the name of a closely related core Java class, the AWT equivalent of JFrame, and your giving it the same name can confuse many. Perhaps call it SnakeFrame.
All of your static variables should instead be instance variables.
You shouldn't even have a SnakeFrame variable (your variable f). Instead you should use the current SnakeFrame instance, the this if you will.
Don't mix AWT with Swing components unnecessarily and without need. For instance you should use no Canvas-derived objects but rather JPanel-derived objects.
Your code should follow Java naming conventions so that others (us for instance) can understand it. Variable and method names should all begin with a lower case.
You will want to read up on and use the layout managers to help you place your GUI components on your GUI rather than use null layout and setBounds(...).
Most important, to swap views as you're trying to do, read up on and use a CardLayout as this was built for just this purpose.
This is not an answer and will be deleted, but I needed to post a more complex comment:
I am writing a program that has a menu in it. (It is a JFrame in java) What it does is when you hit a JButton, it shows an applet on the screen.
Can you explain why you're doing this? It is most unusual and difficult for a JFrame to display an applet as an applet. Are you doing this by launching the applet using some stand-alone applet viewer? Perhaps you're doing something else, such as displaying a dialog or another JFrame, but using the wrong terms to describe it?
When the applet is done, it goes to a screen where you can choose to run the applet again or go back to the main menu when you hit a button.
How do you achieve this? What code do you use?
The problem is that when you hit the button to go back to the menu, it doesn't. All it does is make neither button clickable. This is the method I use to draw the menu:
public static void drawMenu()
{
f.add(BOption1);
f.add(BOption2);
}
Does anyone know why it won't work when I call this method a second time?
Somehow your code is changing the state of your program and thereby makes your JButtons unresponsive. How it's doing this -- you haven't told us enough for us to say.
I'm not sure that we have enough information to answer your question yet. Please show more code and give us more detailed and precise description of what is going on. Best would be if you could post an sscce.
I'm building simple GUI for my app. I have couple of JPanels. I want to display them depending on action that was performed by clicking on a JButton. How can I disable one JPanel and enable another one ?
Couple of details. I have a class with JFrame where I'm building starting gui. Where I have buttons and some text. Clicking on one of the buttons should change the view in this JFrame
my button definition
JButton btnStart = new JButton("Start");
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnStart.setBounds(10, 11, 110, 23);
contentPane.add(btnStart);
// edit
I've found the problem. buttons were in static method
Simple as:
jframe.setContentPane(your_new_panel);
jframe.invalidate();
jframe.validate();
You may want to use CardLayout.
Or you can simple remove the oldpanel and add new panel:
contentPane.remove(oldPanel);
contentPane.add(newPanel);
First remove the jPanel and add the new jPanel. Then use validate to perform relayout.
jFrame.remove(jPanelOld);
jFrame.add(jPanelNew);
jFrame.validate();