Swing Jframe using 2 jframes - java

I have 2 classes which extends Jframe. I would like to call the 2nd Jframe when button clicked and return back when close clicked. So on 1st JFrame I wrote this code
public void actionPerformed(ActionEvent e) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Lessons frameLessons = new Lessons();
frameLessons.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
But when I try to close the 2nd Jframe, the 1st also closes

Try this:
Lessons frameLessons = new Lessons();
frameLessons.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frameLessons.setVisible(true);

Related

How can I insert a login JFrame into a test in Selenium?

When I call the JFrame from another class to test, the JFrame works, but when I insert it into my Selenium test, the JFrame and the browser open so quickly that there's no time to input anything and the test appears like passed.
Sample of the elements that I mention:
#Test
public void Run() throws InterruptedException{
Keys.loginFrame(); //This method is static in another class named 'Keys'
...
}
Is there any way to say to Selenium the following?:
Execute first ONLY Keys.loginFrame();, then wait till the JFrame is closed. Finally execute the rest of the test code.
Thank you for your answers!
remake your loginFrame() method to return JFrame object
add this method:
public static void startFrameThread(JFrame frame) {
Thread thread = new Thread() {
public void run() {
while (frame.isVisible()) {
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
}
}
}
};
thread.start();
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent arg0) {
frame.setVisible(false);
}
});
try {
thread.join();
} catch (InterruptedException e) {
}
}
apply new usage:
JFrame loginFrame = Keys.loginFrame();
loginFrame.setVisible(true); // if not setting visible in loginFrame() method
startFrameThread(loginFrame);

How to close a JFrame while opening another JFrame? [duplicate]

This question already has answers here:
The Use of Multiple JFrames: Good or Bad Practice? [closed]
(9 answers)
Closed 4 years ago.
I have a frame (main). There are two buttons: Items and Sale.
When I click button Items it opens a frame (Items)
and I want to, when I click on button Sale, it should close the Items and open Sale.
This is Items frame:
public class Items extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Items frame = new Items();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
UPDATE :- here is my sale class
public class Sale extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Sale frame = new Sale();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Just close the previous Item JFrame with dispose() method.
salesframe.setVisible(true);
itemframe.dispose();
In your case, I think you should also add an ActionListener to the button.
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
frameToClose.dispose();
}
});
You should .dispose(); the frame in the ActionListener of the button and since you are extending JFrame in you class it means you .dispose(); the instance itself. Something like this:
public class Main extends JFrame {
public Main() {
Sale sale = new Sale();
Items item = new Items();
JButton btnSale = new JButton("Sale");
getContentPane().add(btnSale, BorderLayout.WEST);
JButton btnItems = new JButton("Items");
getContentPane().add(btnItems, BorderLayout.CENTER);
btnSale.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
sale.setVisible(true);
if(item.isVisible()) {
item.dispose();
}
}
});
btnItems.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
item.setVisible(true);
if(sale.isVisible()) {
sale.dispose();
}
}
});
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}

Page transition In Applets With Button

i had designed two applets in eclipse and when I click a button I want to show second applet's design how can I do it with mouseClicked method?
this is my first code.
JButton btnReserveASeat = new JButton("RESERVE A SEAT NOW!");
btnReserveASeat.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Policies ();
}
});
this is my second code that i want to give link in button.
public class Policies extends JApplet {
/**
* Create the applet.
*/
public Policies() {
getContentPane().setBackground(Color.PINK);
getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
ColumnSpec.decode("450px"),},
new RowSpec[] {
RowSpec.decode("29px"),
FormFactory.RELATED_GAP_ROWSPEC,
Goes like this.
public void actionPerformed(ActionEvent e) {
new Policies ();
}
Should be something like:
URL url = new URL(getDocumentBase(), "policies.html");
// ..
public void actionPerformed(ActionEvent e) {
ThisApplet.getAppletContext().showDocument(url);
}

Jlayer in a Jpanel with circular loading bar

here is my exemple of making a circular loading bar with Jlayer but now the layer start and stop after the execution of the btnLoad.addActionListener() and stop after a while of determinated timer (4000) so my problem that I need it to start when I click the button load
and stop after complete the loading of the file !!!
final WaitLayerUI layerUI = new WaitLayerUI();
jlayer = new JLayer<JPanel>(this, layerUI);
final Timer stopper = new Timer(4000,new ActionListener() {
public void actionPerformed(ActionEvent ae) {
layerUI.stop();
}
});
stopper.setRepeats(false);
if (!stopper.isRunning()) {
stopper.start();
}
btnLoad.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent ae) {
layerUI.start();
DataManager dataManager = new DataManager();
try {
dataManager.loadFromFile("C:/Users/*****PC/Downloads/****.csv");
} catch (Exception e) {
e.printStackTrace();
}
}
}
);
You should load the file on another Thread and not the Event Dispatch Thread. Assuming your loadFromFile method blocks until it loads the file, you can then hide the layer, but you must hide on the Event Dispatch Thread and not the new Thread you started for loading the file.
Remove your timer and replace your try block with this:
try {
new Thread(new Runnable(){
public void run() {
dataManager.loadFromFile("C:/Users/*****PC/Downloads/****.csv");
EventQueue.invokeLater(new Runnable(){
public void run() {
layerUI.stop();
}
});
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}

JTextField Doesn't Update With Thread.sleep()

I'm trying to figure out why the text field isn't updating. I'm aware that using SwingWorker will probably fix this problem, but I can't understand why it doesn't work in the first place.
public class waitExample {
private JFrame frame;
private JTextField txtLeadingText;
private String one = "update string 1";
private String two = "update string 2";
private String three = "update string 3";
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
waitExample window = new waitExample();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public waitExample() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
txtLeadingText = new JTextField();
txtLeadingText.setHorizontalAlignment(SwingConstants.CENTER);
txtLeadingText.setText("leading text");
frame.getContentPane().add(txtLeadingText, BorderLayout.SOUTH);
txtLeadingText.setColumns(10);
JButton btnClickMeTo = new JButton("CLICK ME TO UPDATE TEXT");
btnClickMeTo.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
try {
updateOne();
Thread.sleep(1000);
updateTwo();
Thread.sleep(1000);
updateThree();
Thread.sleep(1000);
updateLast();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
frame.getContentPane().add(btnClickMeTo, BorderLayout.CENTER);
}
private void updateOne() {
txtLeadingText.setText(one);
}
private void updateTwo() {
txtLeadingText.setText(two);
}
private void updateThree() {
txtLeadingText.setText(three);
}
private void updateLast() {
txtLeadingText.setText("default text");
}
}
From what I understand, the default Thread will prevent any GUI updates. That shouldn't matter because I am setting the textField BEFORE the Thread.sleep.
Why doesn't the text field update? Shouldn't the text be set, then the Thread wait?
EDIT: As per the answers, the above code has been updated.
You are invoking Thread.sleep(1000); on EDT. This means that when your method will end - only then the repaint() will fire (at some point in time later).
Until then your GUI is freezed.
Consider that this is going on one thread (so processing is straightforward):
txtLeadingText.setText(one);
Thread.sleep(1000);
txtLeadingText.setText(two);
Thread.sleep(1000);
txtLeadingText.setText(three);
Thread.sleep(1000);
...
<returning from updateText()>
<processing other events on button click>
...
// some time later
<Swing finds out that GUI needs repaint: calls rapaint()>
This is what you should do (I didn't compile or test it):
public class MyRunnable implements Runnable {
private List<String> strsToSet;
public MyRunnable(List<String> strsToSet) {
this.strsToSet = strsToSet;
}
#Override
public void run() {
try {
if(strsToSet.size() > 0) {
final String str = strsToSet.get(0);
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
txtLeadingText.setText(str);
}
});
Thread.sleep(1000);
List<String> newList = new LinkedList<String>(strsToSet);
newList.remove(0);
new Thread(new MyRunnable(newList)).start();
}
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
}
new Thread(new MyRunnable(Arrays.asList(one, two, three))).start();
It is hard to do in Swing but in contrast in dynamically languages (like Groovy) it would go as simple as that (you'll get a better grasp of what is going on):
edt {
textField.setText(one)
doOutside {
Thread.sleep(1000);
edt {
textField.setText(two)
doOutside {
Thread.sleep(1000);
edt {
textField.setText(three)
}
}
}
}
}
The GUI event loop updates the screen, but it can't update the screen until you return.
I suggest you avoid doing any blocking operations in the GUI event thread.

Categories