I am busy doing a school project and hoped some awesome people out there could help me.
I currently have a Main, a Log in GUI and a Search frame (designed with Netbeans Gui dev).
I have an actionPerformed(ActionEvent e) method, where I am struggling is to access the Log in button that is on my Log in GUI. Currently I am doing this.
In my constructor: Declare the Frame. LogInFrame x = new LogInFrame();
In
public actionPerformed(ActionEvent eve)
{
if(eve.getSource()==x.LogInBtn())
{
x.setVisible(false);
}
}
At the moment the button isn't responding, so I was wondering if I was doing anything wrong.
The current Frame is just a standard frame. Really simple
public static void main (String[] args)
{
JFrame LogInFrame = new JFrame ("Log in");
LogInFrame.setSize (300, 300);
JButton close = new JButton ("Hid frame");
LogInFrame.getContentPane ().add (close);
}
}
Thats about everything that is needed. The problem does not lie with the GUI itself as I can run the GUI in netbeans fine. I can use the auto generated actionPerformed method of the button by double clicking on it. But i want to be able to access my GUI's compenents in my main.(tell the button what to do from my main). I have made sure that the button is public and can be accessed. I dont get any physical "coding" errors, the code just doesnt seem to be working (the button isnt responding if I add events from my main)
Related
I´ve created a simple warehouse management program for my company and everything is working fine.
But now I am trying to implement a darkmode to my program, that will be 'active' if the user presses the corresponding button in the mainframe of the program.
There are multiple buttons in the Mainframe that open other JFrames. And that´s the point im stuck at.
If I´m trying to change the background-color of one of the JFrames it only works if the JFrame already is open.
Here´s an example:
Pressing the "Darkmode"-Button in the Mainframe
turnOnDarkmodePanel(EditArticle.edCP);
Method:
public static void turnOnDarkmodePanel(JPanel panel) {
panel.setBackground(new Color(18,18,18));
}
This will be executed. And while the mainframe is perfectly in darkmode the other JFrames are not until I open them and press the Darkmode button in the mainframe again.
Everything else in the JFrame is working fine - Just the background of the Panel doesnt.
Additional Code for Buttons in Darkmode (Working on every single button without pressing the darkmode button again after opening the JFrame):
public static void turnOnDarkmode(JButton button) {
button.setForeground(button_fg);
button.setBackground(button_dm);
button.setBorder(null);
button.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent evt) {
button.setBackground(button_dm_hover);
}
public void mouseExited(MouseEvent evt) {
button.setBackground(button_dm);
}
});
}
Anyone knows how I can fix this that the JFrames are effected without opening them first?
PS.: I am new to JAVA and if more code is needed - I will add it.
Thanks in advance. :)
i created a dialog class that opens when a JLabel is clicked but i want the main window to close when the label is clicked and a more bigger issue is that the label is in a class that extends a JPanel now if the label is clicked the panel goes as in setVisible(false), do you get what i mean, but when i tried to use polymorphism in the panel class to obtain both the main window class and the dialog it proved successful but when the label is clicked an new similar main
wnidow pops up and immediately disappears. ie it duplicates the main window, i know that this problem might look like a chalenge because there are no codes, the file is too complicated but i kmow there is a pro out there who can get a picture of what this code is and help me, thank you
"an new similar main wnidow pops up and immediately disappears. ie it duplicates the main window, " -
Seeing how your JPanel is a separate class, it seems to me like you have a referencing issue. I bet what you did was create a new MainWindow so that you could reference it. Like
mousePressed(MouseEvent e) {
MainWindow window = new MainWindow();
window.dispose();
}
That would definitely explain the the issue. There are a few ways to deal with this. I'm going to give you the rookie way, since you still seem like a rook :D You'll probably learn more proper ways as your learning gets deeper. So you can do something like below, where you pass the reference of the MainWindow to the JPanel class, instead of creating a new MainWindow
public class MyPanel extends JPanel {
private MainWindow window;
public MyPanel(final MainWindow window) {
this.window = window;
JLabel label = new Label();
label.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
window.setVisible(false); // or dispose
}
});
}
}
When you instantiate the MyPanel, pass the reference of the MainWindow to the MyPanel, like MyPanel panel = new MyPanel(MainWindow.this);
I'm trying to add components to a JDialog after it has been created and displayed. Nothing I try makes the changes actually update to the screen, and I've read and applied every question I could find related to this.
This example code creates a modal JDialog showing the word "test". I cannot get it to display "test2". Almost exactly the same code but with a JFrame instead of a JDialog behaves as I expect, so I don't understand. I'm new to Java and especially to swing.
import javax.swing.*;
public class DialogTester {
public static void main(String[] args) {
new DialogTester();
}
public DialogTester() {
JFrame jframe = new JFrame();
jframe.setVisible(true);
JDialog jdialog = new JDialog(jframe,true);
JPanel jpanel = new JPanel();
jpanel.add(new JLabel("test"));
jdialog.add(jpanel);
jdialog.setVisible(true);
jpanel.add(new JLabel("test2"));
jpanel.revalidate();
jdialog.getContentPane().validate();
jdialog.pack();
}
}
I also tried calling
jdialog.repaint();
which did nothing.
You created a modal dialog. So, as soon as you call setVisible(true), the following instructions wait for the dialog to be closed to be executed.
Put the code adding a label before the dialog is made visible, or put it in an event handler called after the dialog is shown, for example, when you click on a button in this dialog.
I'm using a KeyListener on a JFrame object which I set as FullScreenWindow, something like this code:
class Game{
private GraphicsDevice device;
...
public void run(){
JFrame frame = new JFrame();
frame.addKeyListener(new MarioKeyListener());
device.setFullScreenWindow(frame);
}
...
}
And it works fine if I just create a Game object in my main method and call run().
However I want to do this inside the mousePressed() function of a MouseAdapter which I added to another JFrame-s MenuItem. The result is that the program runs as normal but doesn't accept any keyboard input.
JMenu gamemenu = new JMenu("Game");
JMenuItem newGame = new JMenuItem("New Game");
newGame.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e){
Game g = new Game();
g.run();
}
});
gamemenu.add(newGame);
I think My frame object is not in focus, but calling setFocusable(true) and requestfocusinwindow() did not help.
If anyone knows whats wrong or how to fix this, help would be greatly appreciated...
Tomi
requestFocusInWindow()..
Requests that this Component get the input focus, if this Component's top-level ancestor is already the focused Window.
Are you checking the return value? I suspect it is failing because the new window is not the focused component at the moment the method is called.
If that is the case, the answer might be found in similar fashion to the dialog focus strategy of adding a RequestFocusListener to the mix.
I'm creating a small crypto app for the desktop using java.
I'm using JFrames (import javax.swing.JFrame) with Oracle
JDeveloper 11g under Linux.
I want to have a "welcome" form/frame where users can choose
their encryption method, and then on choosing the method,
I want to dynamically create the appropriate form for the
chosen encryption method and also destroy/free/dispose() of
the welcome form. When the user has finished their encrypting,
they should close the frame/form (either by clicking on the
x at the top right - or using the Exit button or by any
method) and the welcome frame should be dynamically recreated
and appear.
I've tried various things - btnEncode_actionPerformed(ActionEvent e)
then this.dispose() - and I've fiddled with this_windowClosed(WindowEvent e)
and dispose(), but nothing seems to work.
Even a workaround using setVisibl(true/false) would be acceptable at
this stage - this has been wrecking my head all day. It's very
easy to do in Delphi!
TIA and rgs,
Paul...
something like this usually does the trick: (Note I haven't tested this)
public class WelcomeMsg extends JFrame
.
.
.
public void btnContinue_actionPerformed(ActionEvent e)
{
this.dispose();
SwingUtilities.invokeLater(new Runnable(){ new JFrameAppropriateWindow(args) });
}
where btnContinue is the Continue button on your welcome form and JFrameAppropriateWindow is the next frame you would like to show depending on the user's choice. Args are any arguments you need to pass.
When you are ready, you can simply dispose the current frame and relaunch an instance of WelcomeMsg
I put together this simple example for creating and displaying a panel depending on a user choice.
public class Window extends JFrame {
public Window() {
this.setLayout(new BorderLayout());
JComboBox encryptionCombobox = new JComboBox();
encryptionCombobox.addItem("foo");
encryptionCombobox.addItem("bar");
//...
encryptionCombobox.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// find choices and the correct panel
JPanel formPanel = new JPanel();
formPanel.setOpaque(true);
formPanel.setBackground(Color.RED);
//...
Window.this.add(formPanel, BorderLayout.CENTER);
Window.this.validate();
Window.this.repaint();
}
});
add(encryptionCombobox, BorderLayout.NORTH);
}
public static void main(String[] args) {
new Window().setVisible(true);
}
}
When I come to think about it, you should probably use a CardLayout instead, which allows you to switch between different panels (cards).