repaint a frame in java - java

I have written code for an online quiz. I would like to change questions by clicking "next" button, but repaint is not working; only new window is working.
i can't even hide jftMainFrame since it works for 8 windows only.quest is a list containing questions and options ,its accessed from access db.repaint() is not working while i click the button.
i have 4 radiobuttons which displays the label.i want to repaint the label of radiobutton and also question
Please help me.
JFrame jtfMainFrame, jtfMainFrame1;
nextButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Next question..");
j++;
quest = getCurrentQuestion();
createWindow();
validate();
}
});

I would like to change questions by clicking "next" button,
I think that your question is about using CardLayout, rather than create lots of Top-Level Comtainers on runtime

Your question is definitely not clear.
What are you trying to do. If you are simply trying to "repaint/refresh" a panel or a component use paintImmediately();
for example
jMyPanel.paintImmediately(jMyPanel.getVisibleRect());
Hope it helps

Related

Can I add and draw a component through code in Swing (Java)?

I'm trying to add and draw buttons using merely my code. To be more specific, my goal is visualizing several buttons after another button has been pressed. This must be done programmatically because the number of buttons that have to be drawn depends on the user's input.
I've seen this piece of code in another thread and tried to implement it, but it didn't quite work:
// Adds a single button to a panel and paints it programmatically
private void jButton1MousePressed(java.awt.event.MouseEvent evt) {
JButton button = new JButton();
jPanel1.add(button);
jPanel1.revalidate();
jPanel1.repaint();
}
Any suggestions?
Thanks in advance.
Note: I'm using NetBeans 8.0.2.

Java Application Restart or Reset Button

I have a very simple, choose your path, Java game that I am working on in the NetBeans IDE. Basically what happens is the user will click one of the three buttons, and pre-made JLabel's which are set to "" (nothing) will be reset to whatever text I decide that label should then use. I do this by adding the code below.
private void option1ActionPerformed(java.awt.event.ActionEvent evt) {
jLabel6.setText("You go down the dark tunnel...");
}
Now all this works fine but I'd like a way to restart/reset my application by clicking a button labelled "restart". I don't mind if it has to close the application and then open it again or if it will simply reset all the JLabel's back to "". I just don't want to have to type out the code below for every single JLabel.
jLabel1.setText("");
jLabel2.setText("");
jLabel3.setText("");
I have done some research on this site and none of the code that people provide seems to work for my situation, either because it simply doesn't work or because I am doing something wrong. Any help would be greatly appreciated and please try and be specific because I am fairly new to writing Java and don't understand everything.
It would also work for me if someone could provide a way for me to close 1 window in an application instead of the whole thing, like when
System.exit(0);
is used.
First, I recommend importing the JLabel class specifically so you can write JLabel instead of javax.swing.JLabel:
import javax.swing.JLabel;
Instead of declaring each JLabel individually, create an Array of JLabels:
JLabel[] jLabels = new JLabel[N]; // where N is the number of JLabels
Whenever you need to access a JLabel, use:
jLabels[6].setText("You go down the dark tunnel...");
When you want to reset all the JLabels, use:
for (JLabel jLabel : jLabels) {
jLabel.setText("");
}
For more details on Arrays, read http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
If you are having your program as a executable jar file then you can use
public void restert(){
Runtime.getRuntime().exec("java -jar yourapp.jar");
System.exit(0);
}
If you want to know more about restarting java application you can go through this
Found the answer to my own question:
Thanks to Holger for suggesting "dispose". This is what I found worked.
private void restartButtonActionPerformed(ActionEvent evt) {
if(evt.getSource() == restartButton)
{
dispose();
MyGUI game = new MyGUI();
game.setVisible(true);
}
}

jFrame multiple event listeners on one button

I'm quite new to the whole GUI scene of Java but I decided to give it a try. I have a project in NetBeans and I am using their little auto generator thing and was wondering if you can have multiple even listeners on one button? Right now I have it so when you click it changes the button to a certain color and I was wondering if you can make it so when you click it again, it changes the color back to the default color?
This is what my button looks like right now
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jButton1.setBackground(Color.black);
jButton1.setForeground(Color.yellow);
}
I have an idea of how it would look but I can't really get it to work. I know you have to set it back to the regular color like this.
jButton1.setBackground(null);
Any help is appreciated!
You can add a check in your event listener to see if the color is already changed:
if(jButton1.getBackground().equals(Color.black)) {
... // revert color
} else {
jButton1.setBackground(Color.black);
jButton1.setForeground(Color.yellow);
}

Using KeyListener for a Calculator in NetBeans

I have written a calculator in NetBeans and it functions perfectly. However, I have to actually click the buttons to insert numbers and am trying to remedy that with a KeyListener. I have all my numbers and function buttons set inside a JPanel named buttons. I have my display label in a JPanel named display.
I set my class to implement KeyListener and it inserted the KeyPressed, -Typed, and -Released methods; however I stuck from there. I'm not sure how to make my buttons actually listen for the KeyPressed event, and when it hears the event - activate the button. Also, my buttons are named by their number (e.g. the Zero Button is named zero, One button is one, etc.).
I've read that you actually have to implement a KeyListener somewhere by using: something.addKeyListener(something);
but I cannot seem to figure this out.
Can I get some help here? I'm new to Java and this is my first solo project. And let me know if I didn't provide enough information.
EDIT: Most of my code is NetBeans Generated and I cannot edit the initialization of the components which seems to be my problem I think?
My class declaration:
public class Calculator extends javax.swing.JFrame implements KeyListener {
//Creates new form Calculator
public Calculator() {
initComponents();
}
One of my buttonPressed actions (all identical with changes for actual number):
private void zeroActionPerformed(java.awt.event.ActionEvent evt) {
if (display.getText().length() >= 16)
{
JOptionPane.showMessageDialog(null, "Cannot Handle > 16 digits");
return;
}
else if (display.getText().equals("0"))
{
return;
}
display.setText(display.getText().concat("0"));
Main method supplied by NetBeans:
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Calculator().setVisible(true);
}
});
}
The initComponents() netbeans generated is absolutely massive (about 500 lines of code) and I cannot edit any of it. Let me know if I can supply any more helpful information.
Could there be an issue of Focus, and if so how can I resolve the issue?
Yes there is probably an issue with focus. That is why you should NOT be using a KeyListener.
Swing was designed to be used with Key Bindings. That is you create an Action that does what you want. Then this Action can be added to your JButton. It can also be bound to a KeyStroke. So you have nice reusable code.
Read the Swing tutorial on How to Use Key Bindings for more information. Key Bindings don't have the focus issue that you currently have.
I'm not sure I completely understand your question, and some code would help, but I'll take a crack, since it sounds like a problem I used to have a lot.
It sounds like the reason that your key presses aren't being recognized is that the focus is on one of the buttons. If you add keylisteners to the buttons, then you shouldn't have any problem.
In netbeans you can add keylisteners through the design screen really easily.
Here's a picture showing you how to add a keyPressed listener to a button in a jPanel.
private void jButton1KeyPressed(java.awt.event.KeyEvent evt) {
//Check which key is pressed
//do whatever you need to do with the keypressed information
}
It is nice to be able to write out the listeners yourself, but if you are just learning, then it is also nice to get as much help as possible.
This might not be the best solution, since you would have to add the listener for each of your buttons.

Java GUI Game Menu

I'm trying to work on a project - we are creating a little game with a GUI.
I decided to start by working on a 'main menu'. Essentially, there will be buttons such as "Single Player", "Help", etc...
I've made the GUI with the menu, etc. I have added listeners as well.
How do I approach the problem now? If someone clicks, say, 'Single Player', I'd like the screen to change to display a title showing "SINGLE PLAYER" and get rid of the main menu.
What do I need to do in my actionPerformed() method to get this effect?
I guess I will be able to work it out from there.
Any help would be greatly appreciated.
best of all options could be use JMenuItem to interact with Cards layed by CardLayout, options are described incl. images in the tutorial
best of questions asked about CardLayout ever
The best approach would be to put the corresponding windows onto a separate Jpanel and have one master panel in your gui. Whenever the user clicks on a button, you remove whatever was on the master panel add the corrseponding panel, then repaint the gui.
something like this:
if (e.getSource() == button){
masterPanel.removeAll();
masterPanel.add(newWindow);
this.setVisible(true);
this.repaint();
}

Categories

Resources