I'm using a JTabbedPane, and on each tab there's a panel, which changes when the user does things like click buttons. The problem I'm getting is that elements from the previous panel get left behind. Usually you can't see them until you run the mouse over them, but sometimes you can't see elements of the new panel until you run your mouse over where they should be. So quite often at first only a little is visible, then:
when you run your mouse over more stuff it becomes visible
Then for some reason when you hit 'multiple choice', which is supposed to create the four new buttons, everything becomes perfectly visible.
I've got repaint(); as pretty much every other line, and before I change anything on the GUI I do removeAll(); first, but it all keeps coming back! Any suggestions? The code for this panel is below, if it might help...
package com.GC01.gui;
import java.awt.Color;
public class PracticeQuizPanel extends JPanel implements MouseListener {
/**
* This panel will allow the user to practice quiz questions.
*/
private static final long serialVersionUID = 1L;
private User user = new User("misha");
boolean isMultipleChoice=false;
boolean usedClue=false;
boolean isStarted=false;
Calendar calendar = Calendar.getInstance();
AnswerAnalysis aA = new AnswerAnalysis();
private Quiz qz = new Quiz( Quiz.getQuestionsFromDisk(), 4, TabbedQuiz.getUser().getNumLogins() );
private JTextArea questionArea;
private JTextArea clueArea;
private JTextArea answerArea;
private JButton clueButton;
private JButton multiButton;
private JButton answerButton;
private JButton startButton;
private int index=0;
private Calendar startCalendar, endCalendar;
private JPanel backPanel;
public PracticeQuizPanel(){
add(new StartButtonPanel());
}
PracticeQuizPanel(int index) {
createVisualElements(index);
}
public void offerAnswer(){
endCalendar = Calendar.getInstance();
aA.setTimeSpent((int) (endCalendar.getTimeInMillis()-startCalendar.getTimeInMillis()));
aA.setRight(answerArea.getText());
JOptionPane.showMessageDialog(null, aA.toString());
answerArea.setEditable(false);
qz.setAnswersAnalysis(index, aA);
index++;
removeAll();
if( index<qz.getLength() ) createVisualElements(index);
else {
removeAll();
JOptionPane.showMessageDialog(null, qz.toFriendlyString());
addQuizResultsToUserProgress();
JOptionPane.showMessageDialog(null, qz.toFriendlyString());
UserProgress uP = new UserProgress(user);
System.out.println(uP.toString());
}
repaint();
startCalendar = Calendar.getInstance();
//JOptionPane.showMessageDialog(null, isStarted);
}
public void addQuizResultsToUserProgress(){
UserProgress userProgress = new UserProgress(user);
ArrayList<AnswerAnalysis> asA = userProgress.getAnswersAnalysis();
for (int i=0; i<qz.getLength(); i++){
asA.add( qz.getAnswersAnalysis()[i]);
}
userProgress.setAnswersAnalysis(asA);
userProgress.saveProgress();
}
/**
* This method creates/recreates all the text boxes, buttons etc. without resetting the quiz and
* the objects in memory.
*/
private void createVisualElements(int index){
if (TabbedQuiz.getUser().getNumLogins()<0)
JOptionPane.showMessageDialog(null, "There was an error. You may have done this quiz before.");
removeAll();
repaint();
startCalendar = Calendar.getInstance();
this.index=index;
setBackground(new Color(112, 128, 144));
setBounds(0,0,728,380);
setLayout(null);
questionArea = new JTextArea();
questionArea.setFont(TabbedQuiz.getDefaultFont().deriveFont(20));//new Font("Courier New", 0, 20));
questionArea.setEditable(false);
questionArea.setLineWrap(true);
questionArea.setWrapStyleWord(true);
questionArea.setBounds(295, 11, 423, 74);
add(questionArea);
//int index=0;
Question q = qz.getQuestions().get(index);
aA = new AnswerAnalysis(q.getQuestionID());
questionArea.setText(q.getQuestionText() );
clueArea = new JTextArea();
clueArea.setFont(TabbedQuiz.getDefaultFont().deriveFont(20));
clueArea.setEditable(false);
clueArea.setLineWrap(true);
clueArea.setWrapStyleWord(true);
clueArea.setBounds(295, 104, 423, 55);
add(clueArea);
JLabel lblQuestion = new JLabel("QUESTION:");
lblQuestion.setFont(TabbedQuiz.getDefaultFont().deriveFont(40));
lblQuestion.setBounds(43, 11, 216, 61);
add(lblQuestion);
answerArea = new JTextArea();//index+"");
answerArea.setFont(TabbedQuiz.getDefaultFont().deriveFont(20));
answerArea.setLineWrap(true);
answerArea.setWrapStyleWord(true);
answerArea.setBounds(295, 301, 423, 50);
answerArea.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) offerAnswer();
}
});
add(answerArea);
answerArea.setFocusable(true);
answerArea.requestFocusInWindow();
clueButton = new JButton("CLUE?");
clueButton.setFont(TabbedQuiz.getDefaultFont().deriveFont(40));
clueButton.addMouseListener(this);
clueButton.setBounds(15, 104, 244, 55);
add(clueButton);
multiButton = new JButton("MULTIPLE CHOICE?");
multiButton.setFont(TabbedQuiz.getDefaultFont().deriveFont(20));
multiButton.addMouseListener(this);
multiButton.setBounds(15, 195, 244, 55);
add(multiButton);
answerButton = new JButton("ANSWER!");
answerButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
answerButton.setFont(TabbedQuiz.getDefaultFont().deriveFont(40));
answerButton.setBounds(15, 301, 244, 55);
answerButton.addMouseListener(this);
add(answerButton);
backPanel = new JPanel();
backPanel.setBounds(0, 0, 728, 380);
//add(backPanel);
this.setVisible(true);
repaint();
}
#Override
public void mouseClicked(MouseEvent e) {
if ( e.getSource().equals(startButton) ) {
remove(startButton); repaint(); isStarted=true;
startCalendar = Calendar.getInstance();
}
else if ( e.getSource().equals(answerButton) ) offerAnswer();
else if ( e.getSource().equals(clueButton ) ) {
clueArea.setText(clueArea.getText() + qz.getQuestions().get(index).getClueText() + "\n");
aA.setUsedClue(true);
//JOptionPane.showMessageDialog(null, "hi");
}
else if ( e.getSource().equals(multiButton) ) {
String[] answerOptions = qz.getQuestions().get(index).getAnswerOptions(3);
JButton[] optionsButtons = new JButton[4];
for(int j=0;j<4;j++){
optionsButtons[j]=new JButton(answerOptions[j]);
if(optionsButtons[j].getText().length()>13)
optionsButtons[j].setFont(TabbedQuiz.getDefaultFont().deriveFont(10));
else optionsButtons[j].setFont(TabbedQuiz.getDefaultFont().deriveFont(15));
if(j<2) optionsButtons[j].setBounds(295+211*j , 170, 211, 55);
else optionsButtons[j].setBounds(295+211*(j-2) , 226, 211, 55);
optionsButtons[j].addMouseListener(this);
optionsButtons[j].setName("optionsButton"+"["+j+"]");
add(optionsButtons[j]);
repaint();
}
aA.setMultipleChoice(true);
}
else if ( ( (JButton) e.getSource() ).getName().startsWith("optionsButton") ) {
String answerOffered = ( (JButton) e.getSource() ).getText();
answerArea.setText(answerOffered);
offerAnswer();
}
}
#Override
public void mouseEntered(MouseEvent e) {
if(e.getSource()!=startButton && e.getSource().getClass().equals( answerButton.getClass()) ){
( (JButton) e.getSource() ).setBackground(Color.green);
}if(index>0 && e.getSource()==startButton) remove(startButton);
}
#Override
public void mouseExited(MouseEvent e) {
if(e.getSource()!=startButton && e.getSource().getClass().equals( answerButton.getClass()) ){
( (JButton) e.getSource() ).setBackground(UIManager.getColor("control"));
}
}
#Override
public void mousePressed(MouseEvent e) {
}
#Override
public void mouseReleased(MouseEvent e) {
}
}
and on each tab there's a panel, which changes when the user does things like click buttons.
and before I change anything on the GUI I do removeAll();
Whenever I see comments like that it sounds to me like you should be using a Card Layout so you don't have to worry about all these problems.
You need to call revalidate after removing or adding components to a container if using layout managers. Then after that you call repaint.
You've posted a lot of code, and most of it is completely unrelated to the problem at hand, but on the other hand, the code posted is incomplete, won't compile, and thus is not code that we can test. Please next time, try not posting a lot of code that's not relevant to the problem. Try to isolate the problem by continually cutting out code, and then if still stuck, post a minimal compiable program that we can study, test, run, and modify ourselves, an sscce.
I've gone through some of your GUI code (for gosh's sake, not all!), and there's some big time badness there where you're stepping on Swing painting such as this code from ProgressPanel.java:
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
createAndShowGUI(g2d);
// drawDifficultyChart(g2d, fontComboBox.getSelectedIndex());
}
private void createAndShowGUI(Graphics2D g2d) {
JButton showChartButton = new JButton("Show new chart!");
user = TabbedQuiz.getUser();
showChartButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
if (categoryComboBox.getSelectedIndex() == 0
&& difficultyComboBox.getSelectedIndex() != 0) {
drawDifficultyChart((Graphics2D) getGraphics(),
difficultyComboBox.getSelectedIndex());
} else if (difficultyComboBox.getSelectedIndex() == 0
&& categoryComboBox.getSelectedIndex() != 0) {
drawCategoryChart((Graphics2D) getGraphics(),
(String) categoryComboBox.getSelectedItem());
} else
drawGeneralChart((Graphics2D) getGraphics(),
(String) categoryComboBox.getSelectedItem(),
difficultyComboBox.getSelectedIndex());
} catch (NullPointerException e1) {
JOptionPane.showMessageDialog(null, "Sign in first.");
}
}
});
showChartButton.setBounds(10, 90, 96, 26);
showChartButton.setFont(font);
add(showChartButton);
}
The problem is that you're creating and adding components to a container from within a paint/paintComponent method, something you should never be doing and something that is bound to mess up your program's graphics.
To anyone else with this issue: I eventually solved the problem. setVisible(false); setVisible(true);. Put it EVERYWHERE.
Related
this is my first question on this website.
I have this problem, in this class I have two buttons with two different functions, one to exit and another to put the first and last name in a text field.
I can't get the second ActionEvent to work, please help me, thanks.
import javax.swing.*;
import java.awt.event.*;
public class Prueba1 extends JFrame implements ActionListener{
private JLabel nombre, apellidos,respondo;
private JTextField textfield, textfield1;
private JButton boton,botonoff;
public Prueba1() {
setLayout(null);
nombre = new JLabel("Nombre:");
nombre.setBounds(10, 10, 300, 30);
add(nombre);
apellidos = new JLabel("Apellidos");
apellidos.setBounds(10, 40, 300, 30);
add(apellidos);
textfield = new JTextField();
textfield.setBounds(100,10,150,20);
add(textfield);
textfield1 = new JTextField();
textfield1.setBounds(100,40,150,20);
add(textfield1);
boton = new JButton("¿Que saldrá?");
boton.setBounds(10,80,120,30);
boton.addActionListener(this);
add(boton);
botonoff = new JButton("Salir");
botonoff.setBounds(10,120,120,30);
botonoff.addActionListener(this);
add(botonoff);
respondo = new JLabel("UwU");
respondo.setBounds(160,80,300,30);
add(respondo);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == boton) {
String nombreyapellidos, nombre1, apellidos1;
nombre1 = textfield.getText();
apellidos1 = textfield1.getText();
nombreyapellidos = nombre1 + apellidos1;
respondo.setText(nombreyapellidos);
}
}
public void actionPerformed1(ActionEvent e) {
if(e.getSource() == botonoff) {
System.exit(0);
}
}
public static void main(String args[]) {
Prueba1 clase = new Prueba1();
clase.setVisible(true);
clase.setBounds(0, 0, 500, 500);
clase.setResizable(true);
clase.setLocationRelativeTo(null);
}
}
Remove public void actionPerformed1(ActionEvent e) method and add the body of that method in the else branch in the body of public void actionPerformed(ActionEvent e).
public void actionPerformed(ActionEvent e) {
if (e.getSource() == boton) {
String nombreyapellidos, nombre1, apellidos1;
nombre1 = textfield.getText();
apellidos1 = textfield1.getText();
nombreyapellidos = nombre1 + apellidos1;
respondo.setText(nombreyapellidos);
} else if (e.getSource() == botonoff) {
System.exit(0);
}
}
When you provide an ActionListener object to a buttons button.addActionListener(listener)
You have several ways to accomplish this.
button.addActionListener(this);
Is only one way. This way says the the class implements ActionListener.
In effect it implements the
public void actionPerformed(ActionEvent e)
method.
Your
public void actionPerformed1(ActionEvent e)
can't be used by the button at all.
Fortunately there are many other ways to describe the code that should be executed when an action event is produced.
An inner class, static or not. Other class/object.
A lambda expression.
You can find how to express a lambda here.
So I am making a space invaders clone. Originally I had no problem getting my game to work with a simple main class that created the frame, created the gameplay and started the thread.
But then I tried to implement a start menu and it all went to crap. The menu appears with success but the gameplay does not appear when I press start.
I am running out of ideas and I am completely stumped. I am somewhat new as well to SO, so if there is anything I left out, I appreciate any help.
Here is the original with no menu that worked fine:
public static void main(String[] args) {
JFrame frame = new JFrame("SpaceRaiders");
frame.setSize(600, 600);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Gameplay gameplay = new Gameplay();
frame.add(gameplay);
frame.setVisible(true);
Thread t1 = new Thread(gameplay);
t1.start();
}
However, the moment I tried to implement a menu to then play the game, I am running into all sorts of trouble. I created a UI class as well as an actual "game" class like so:
public class UI {
JFrame frame, f2;
JPanel titrePanel, startButtonPanel, loadButtonPanel, p2;
JLabel nomJeu;
JButton startButton, loadButton;
Font fontTitre, fontStart;
Gameplay gameplay;
public void createUI(ChoixJeu cj) {
frame = new JFrame("SpaceRaiders");
frame.setSize(600, 600);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setLayout(null);
frame.getContentPane().setBackground(Color.black);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//------------------ECRAN MENU---------------------
//Titre
titrePanel = new JPanel();
titrePanel.setBounds(100, 100, 400, 100);
titrePanel.setBackground(Color.BLUE);
Font fontTitre = new Font("Times New Roman", Font.BOLD, 50);
Font fontStart = new Font("Times New Roman", Font.PLAIN, 20);
nomJeu = new JLabel("SpaceRaiders");
nomJeu.setForeground(Color.white);
nomJeu.setFont(fontTitre);
titrePanel.add(nomJeu);
//Start button
startButtonPanel = new JPanel();
startButtonPanel.setBounds(200, 400, 200, 40);
startButtonPanel.setBackground(Color.BLACK);
startButton = new JButton("START");
startButton.setBackground(Color.BLACK);
startButton.setForeground(Color.WHITE);
startButton.setFont(fontStart);
startButton.setFocusPainted(false);
startButton.addActionListener(cj);
startButton.setActionCommand("start");
startButtonPanel.add(startButton);
//Load Button
loadButtonPanel = new JPanel();
loadButtonPanel.setBounds(200, 440, 200, 100);
loadButtonPanel.setBackground(Color.BLACK);
loadButton = new JButton("LOAD");
loadButton.setBackground(Color.BLACK);
loadButton.setForeground(Color.WHITE);
loadButton.setFont(fontStart);
loadButton.setFocusPainted(false);
titrePanel.add(nomJeu);
loadButtonPanel.add(loadButton);
frame.add(startButtonPanel);
frame.add(titrePanel);
//------------------ECRAN MENU FIN---------------------
frame.setVisible(true);
}
And the game class...
public class Jeu {
ChoixJeu cj = new ChoixJeu();
UI ui = new UI();
Ecrans e = new Ecrans(ui);
Gameplay gp;
public static void main(String[] args) {
new Jeu();
}
public Jeu() {
ui.createUI(cj);
Gameplay gameplay = new Gameplay();
this.gp = gameplay;
}
public class ChoixJeu implements ActionListener {
#Override
public void actionPerformed(ActionEvent ae) {
String yourChoice = ae.getActionCommand();
switch (yourChoice) {
case "start":
e.montrerEcranJeu();
new Thread(gp).start();
ui.frame.add(gp);
break;
default:
break;
}
}
}
}
I also tried to make a class/method that hides the menu panels
public void montrerEcranJeu() {
//Cache Menu
ui.titrePanel.setVisible(false);
ui.startButtonPanel.setVisible(false);
//Montre Jeu
// ui.frame.add(gameplay);
}
And just in case the Gameplay class. The run() method is at the bottom
public class Gameplay extends JPanel implements KeyListener, ActionListener, Runnable {
private Ship player = new Ship(new Point(200, 555));
Timer t = new Timer(5, this);
private ArrayList<Laser> lasers = new ArrayList<Laser>();
private int laserNb;
private boolean readytofire;
private boolean shot = false;
private ArrayList<Invader> invaders = new ArrayList<Invader>();
private boolean pause;
public Gameplay() {
super();
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
for (int j = 0; j < 80; j += 20) {
for (int i = 0; i < 20; i++) {
invaders.add(new Invader(5 + i * 30, j));
}
}
}
public boolean addLaser(Laser a) {
lasers.add(a);
return true;
}
public boolean addPlayer(Ship p) {
this.player = p;
return true;
}
#Override
public void keyTyped(KeyEvent ke) {
}
public void keyPressed(KeyEvent e) {
if (KeyEvent.VK_RIGHT == e.getKeyCode()) {
moveRight();
}
if (KeyEvent.VK_LEFT == e.getKeyCode()) {
moveLeft();
}
if (KeyEvent.VK_SPACE == e.getKeyCode()) {
shoot();
System.out.println("Space Action from Gameplay is working");
}
}
#Override
public void keyReleased(KeyEvent e) {
}
#Override
public void actionPerformed(ActionEvent ae) {
repaint();
}
public void moveRight() {
if (player.getCentre().getX() >= 580) {
player.setX(580);
} else {
double movement = player.getCentre().getX();
movement += 10;
player.setX(movement);
}
this.repaint();
}
public void moveLeft() {
if (player.getCentre().getX() <= 20) {
player.setX(20);
} else {
double movement = player.getCentre().getX();
movement -= 10;
player.setX(movement);
}
this.repaint();
}
public void shoot() {
shot = true;
if (readytofire) {
Point top = new Point(player.getTopX(), player.getTopY());
Laser laser = new Laser(top);
addLaser(laser);
}
}
public void moveShot() {
if (shot) {
for (Laser l : lasers) {
l.setY(l.getTopLeft().getY() - 1);
}
}
}
#Override
public void paint(Graphics g) {
setBackground(Color.black);
super.paint(g);
player.draw(g);
for (Laser l : lasers) {
l.draw(g);
}
for (Invader i : invaders) {
i.draw(g);
}
}
// public void paintComponent (Graphics g){
// Controle Thread
public void run() {
while (true) {
moveShot();
for (Invader i : invaders) {
i.moveAndUpdate();
}
// for (Invader i : invaders) {
// if (){
// System.out.println("YOU ARE DEAD!");
// }
// }
try {
Thread.sleep(10);
readytofire = true;
} catch (InterruptedException ex) {
Logger.getLogger(Gameplay.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
So, using null layouts is the beginning of your problems. I might recommend using CardLayout which is designed to help you dynamically switch between views. See How to Use CardLayout for more details. I'd also suggest taking the time to read through Laying Out Components Within a Container and finding one or more appropriate layouts to support your menu.
You're also making a lot of fundamental mistakes. Swing is not thread safe, so you should avoid updating the UI (or something the UI depends on) from outside the context of the EDT - see Concurrency in Swing for more information and How to Use Swing Timers for a possible solution.
As a general recommendation, you should avoid overriding paint and, in the case of classes which extend from JComponent, prefer paintComponent instead. You should also avoid call methods which might change the state of the component during a paint cycle, this can increase the number of repaint requests and degrade the performance of your program (ie, don't call setBackground inside paint).
Have a look at Performing Custom Painting and Painting in AWT and Swing for more details about how the paint system works and how best you can work with it.
You should also avoid KeyListener, this is likely to cause you issues when you introduce other, focusable, components into the picture. Instead, you should favour the Key bindings API instead
I've read through [insert link or tutorial], but it still doesn't help...
And forgive me if this doesn't happen all the time.
The point of providing you the tutorial links is to encourage you to learn something;
Learn where to find answers to your questions
Learn how the APIs work
Expand your knowledge and understanding of how the APIs work
Having said that, they're not always "obvious" as to the solution. What I do when I'm in this situation is start with one or more new projects, dedicated to just working on that aspect of the API I'm trying to understand. For here I can explore the concepts in isolation and when I "think" I understand them, try and implement them into the project I'm working on. This might take a number of iterations, but once it works, I have gained a much deeper understanding and appreciation of the API then I would have gained from a simple "copy-n-paste" solution
I have a simple login screen. I want UserName and Password to show up in the first and second text fields until they are clicked on. This functionality is working. However the first text field is always focused when the app launches and therefore shows up as "" until it loses focus. I tried to set a default button and request focus to no avail. It looks like the button is defaulting correctly, but it is not receiving focus for some reason. Anyone know how to fix this?
public class Basics implements ActionListener{
private JFrame frmBasics;
private JTextField userNameFeild;
private JTextField passwordFeild;
private JButton btnSignIn;
private JButton btnSignUp;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Basics window = new Basics();
window.frmBasics.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}//end main
/**
* Create the application.
*/
public Basics() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmBasics = new JFrame();
frmBasics.setTitle("Welcome to the POOPalace!!!");
frmBasics.setBounds(100, 100, 511, 344);
frmBasics.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmBasics.getContentPane().setLayout(null);
userNameFeild = new JTextField("UserName");
userNameFeild.setBounds(148, 79, 214, 20);
userNameFeild.addFocusListener(new FocusedClass());
frmBasics.getContentPane().add(userNameFeild);
userNameFeild.setColumns(10);
passwordFeild = new JTextField("Password");
passwordFeild.setBounds(148, 126, 214, 20);
passwordFeild.addFocusListener(new FocusedClass());
frmBasics.getContentPane().add(passwordFeild);
passwordFeild.setColumns(10);
btnSignIn = new JButton("Sign In");
btnSignIn.setBounds(148, 182, 89, 23);
btnSignIn.addActionListener(this);
frmBasics.getContentPane().add(btnSignIn);
btnSignUp = new JButton("Sign Up");
btnSignUp.setBounds(273, 182, 89, 23);
btnSignUp.addActionListener(this);
frmBasics.getContentPane().add(btnSignUp);
//from what I've been reading these 2 lines should be the solution
//but the request focus seems to not be working
frmBasics.getRootPane().setDefaultButton(btnSignIn);;
btnSignIn.requestFocus();
}
#Override
public void actionPerformed(ActionEvent e) {
//frmBasics.getContentPane().removeAll();
//frmBasics.repaint();
System.out.println(userNameFeild.getText());
System.out.println(passwordFeild.getText());
}//actionPerformed
private class FocusedClass implements FocusListener {
#Override
public void focusGained(FocusEvent arg0) {
if(arg0.getSource().equals(userNameFeild) && userNameFeild.getText().compareTo("UserName") == 0){
userNameFeild.setText("");
}
if(arg0.getSource().equals(passwordFeild) && passwordFeild.getText().compareTo("Password") == 0){
passwordFeild.setText("");
}
}
#Override
public void focusLost(FocusEvent arg0) {
if(userNameFeild.getText().compareTo("") == 0){
userNameFeild.setText("UserName");
}
if(passwordFeild.getText().compareTo("") == 0){
passwordFeild.setText("Password");
}
frmBasics.getContentPane().repaint();
}
}
}//class
Requesting focus works only after the layout of the window is complete.
We need to call the requestFocusInWindow() in one of the three specific situations:
In the windowOpened() method.
In the EventQueue's invokeLater(), which will run after all pending events are processed.
In the overriden JFrame's setVisible() method.
The first option:
//btnSignIn.requestFocusInWindow();
frmBasics.addWindowListener(new WindowAdapter() {
#Override
public void windowOpened(WindowEvent e) {
btnSignIn.requestFocusInWindow();
}
});
Also note that requestFocusInWindow() is more portable than
requestFocus().
Code with near-identical blocks like this makes me cringe. Plus it adds up to where you have a thousand lines of code where half that would suffice. Surely there is a way to make a loop to make it all happen and not have code that looks so unsophisticated and brainless.
Offhand it seems like to do so would be adding as much code as I seek to reduce: loop to make 5 buttons, array of labels for the buttons, array of backgrounds... maybe more. Even if that turned out to be acceptable, how would I make a loop to handle the listeners? I can't have an array of methods, can I? I guess such a loop it would have to include a switch. Yes? I'd probably do that if I didn't want to seek a better solution. So I'm asking...
What would code look like that would listen to the entire group of buttons and take action based on which one was pressed? To which component would I assign the single listener? And how?
(There's a chance that the answer to that question will make me cringe even more than the repetitive nature of the code, if I realize that I already know how to do so and needn't have even asked in the first place, but I'm asking anyway. I'm at one of those I've-had-it-for-today points where the brain just wants out.)
private void makeScoremasterBonuses(){
pnlBonuses = new JPanel(new GridLayout(1, 6));
pnlBonuses.setSize(6,1);
JButton t1 = (new JButton("3W"));
t1.setToolTipText("This is a triple-word cell.");
t1.setBackground(TRIPLE_WORD);
t1.setHorizontalAlignment(JButton.CENTER);
t1.addActionListener(new ActionListener() {
#Override public void actionPerformed(ActionEvent e) {
Highlighter.shadeSymmetric(currentCell,TRIPLE_WORD);
}});
JButton t2 = (new JButton("3L"));
t2.setToolTipText("This is a triple-letter cell");
t2.setBackground(TRIPLE_LETTER);
t2.setHorizontalAlignment(JButton.CENTER);
t2.addActionListener(new ActionListener() {
#Override public void actionPerformed(ActionEvent e) {
Highlighter.shadeSymmetric(currentCell,TRIPLE_LETTER);
}});
JButton t3 = (new JButton("2W"));
t3.setToolTipText("This is a double-word cell");
t3.setBackground(DOUBLE_WORD);
t3.setHorizontalAlignment(JButton.CENTER);
t3.addActionListener(new ActionListener() {
#Override public void actionPerformed(ActionEvent e) {
Highlighter.shadeSymmetric(currentCell,DOUBLE_WORD);
}});
JButton t4 = (new JButton("2L"));
t4.setToolTipText("This is a double-letter cell");
t4.setBackground(DOUBLE_LETTER);
t4.setHorizontalAlignment(JButton.CENTER);
t4.addActionListener(new ActionListener() {
#Override public void actionPerformed(ActionEvent e) {
Highlighter.shadeSymmetric(currentCell,DOUBLE_LETTER);
}});
JButton t5 = (new JButton(""));
t5.setToolTipText("No bonus");
t5.setBackground(WHITE);
t5.setHorizontalAlignment(JButton.CENTER);
t5.addActionListener(new ActionListener() {
#Override public void actionPerformed(ActionEvent e) {
Highlighter.shadeSymmetric(currentCell,B_NORMAL);
}});
pnlBonuses.add(new JLabel("Legend: "));
pnlBonuses.add(t1);
pnlBonuses.add(t2);
pnlBonuses.add(t3);
pnlBonuses.add(t4);
pnlBonuses.add(t5);
}
I'm not asking anyone to write the code; I wouldn't even want that (but I couldn't ignore it!).
Here's what the code above does:
Generally any time you have repeated functionality like that, you want to extract that code out into a helper method like this:
private JButton makeJButton(String label, String toolTip, Color bgColor, final Color highlight) {
JButton button = new JButton(label);
button.setToolTipText(toolTip);
button.setBackground(bgColor);
button.setHorizontalAlignment(JButton.CENTER);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Highlighter.shadeSymmetric(currentCell, highlight);
}
});
return button;
}
Then your makeScoremasterBonuses() method becomes much simpler:
private void makeScoremasterBonuses() {
pnlBonuses = new JPanel(new GridLayout(1, 6));
pnlBonuses.setSize(6, 1);
pnlBonuses.add(new JLabel("Legend: "));
pnlBonuses.add(makeJButton("3W", "This is a triple-word cell.", TRIPLE_WORD, TRIPLE_WORD));
pnlBonuses.add(makeJButton("3L", "This is a triple-letter cell.", TRIPLE_LETTER, TRIPLE_LETTER));
pnlBonuses.add(makeJButton("2W", "This is a double-word cell.", DOUBLE_WORD, DOUBLE_WORD));
pnlBonuses.add(makeJButton("3L", "This is a double-letter cell.", DOUBLE_LETTER, DOUBLE_LETTER));
pnlBonuses.add(makeJButton("", "No bonus.", WHITE, B_NORMAL));
}
Identify the aspects that vary, collect them, and iterate over the collection.
Something like this (untested):
pnlBonuses = new JPanel(new GridLayout(1, 6));
pnlBonuses.setSize(6,1);
pnlBonuses.add(new JLabel("Legend: "));
// Create class "CellInfo" with constructor and getters for desired properties.
CellInfo cellInfos[] = {
new CellInfo("3W", "This is a triple-word cell.", TRIPLE_WORD),
new CellInfo("3L", "This is a triple-letter cell.", TRIPLE_LETTER),
// ...
};
// Add a button for each item described by the cellInfos.
for (CellInfo cellInfo : cellInfos) {
Button b = new JButton(cellInfo.getLabel());
b.setToolTipText(cellInfo.getToolTipText());
b.setBackground(cellInfo.getBackground());
b.setHorizontalAlignment(JButton.CENTER);
b.addActionListener(new ActionListener() {
#Override public void actionPerformed(ActionEvent e) {
Highlighter.shadeSymmetric(currentCell, cellInfo.getBackground());
}});
pnlBonuses.add(b);
}
Note that you might need to create some "final" variables for placeholders for use in the inner anonymous class but the idea should work.
An enum could be your friend here. It's almost an array of methods:
static enum Btn {
TripleWord("3W", "This is a triple word cell.", TRIPLE_WORD),
TripleLetter("3L", "This is a triple letter cell.", TRIPLE_LETTER),
DoubleWord("2W", "This is a double word cell.", DOUBLE_WORD),
DoubleLetter("2L", "This is a double letter cell.", DOUBLE_LETTER),
NoBonus("", "No bonus.", WHITE, B_NORMAL);
final String label;
final String tooltip;
final Color color;
final Color shade;
Btn(String label, String tooltip, Color color, Color shade) {
this.label = label;
this.tooltip = tooltip;
this.color = color;
this.shade = shade;
}
Btn(String label, String tooltip, Color color) {
this(label, tooltip, color, color);
}
public JButton asJButton() {
JButton btn = (new JButton(label));
btn.setToolTipText(tooltip);
btn.setBackground(color);
btn.setHorizontalAlignment(JButton.CENTER);
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Highlighter.shadeSymmetric(currentCell, shade);
}
});
return btn;
}
}
private void makeScoremasterBonuses() {
int nBtns = Btn.values().length;
JPanel pnlBonuses = new JPanel(new GridLayout(1, nBtns + 1));
pnlBonuses.setSize(nBtns + 1, 1);
pnlBonuses.add(new JLabel("Legend: "));
for (Btn btn : Btn.values()) {
pnlBonuses.add(btn.asJButton());
}
}
(I know I could have edited my previous answer, but this one's so different...)
Thanks to #OldCurmudgeon, I have come up with what I think is pretty good.
Here's "proof" (I may just leave each label and tooltip as is):
public enum Colors {
TRIPLE_WORD (255, 220, 50),
TRIPLE_LETTER (255, 255, 150),
DOUBLE_WORD ( 0, 255, 0),
DOUBLE_LETTER (214, 245, 214),
NOT_A_BONUS (255, 255, 255);
private final int red, green, blue;
Colors(int r, int g, int b){
this.red = r;
this.green = g;
this.blue = b;
}
public java.awt.Color background(Colors c){
return new java.awt.Color(c.red, c.green, c.blue);
}
}
private void makeScoremasterBonuses(){
Colors c;
Colors all [] = Colors.values();
String labels[] = new String[all.length];
String abbrs [] = new String[all.length];
JButton but;
pnlBonuses = new JPanel();
pnlBonuses.add(new JLabel("Legend:"));
for (int i = 0; i < all.length; i++) {
labels[i] = all[i].name().replace("_", " ").toLowerCase();
abbrs [i] = abbreviate(all[i].name());
c = Colors.values()[i];
but = new JButton(abbrs[i]);
but.setToolTipText(labels[i]);
but.setBackground(c.background(c));
but.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
but.setActionCommand("" + i);
but.addActionListener(this);
pnlBonuses.add(but);
}
}
=== THIS IS A MAJOR EDIT OF WHAT I POSTED AN HOUR AGO ===
I wanted to see if I could implement my own naive method. Here it is:
public class Game implements ActionListener{
public Color [] backgrounds = {TRIPLE_WORD, TRIPLE_LETTER,
DOUBLE_WORD, DOUBLE_LETTER, B_NORMAL};
private void makeScoremasterBonuses(){
String[] labels = {"3W", "3L", "2W", "2L", " "};
JButton but;
pnlBonuses = new JPanel();
pnlBonuses.add(new JLabel("Legend:"));
for (int i = 0; i < labels.length; i++) {
char wt = labels[i].charAt(0);
char tp = labels[i].charAt(1);
but = new JButton(labels[i]);//("" + i);
but.setBackground(backgrounds[i]);
but.setHorizontalAlignment(SwingConstants.CENTER);
but.setActionCommand("" + i);
but.addActionListener(this);
but.setToolTipText("This is a "
+ (i == labels.length - 1 ? "non-bonus" :
(wt == '3' ? "triple" : "double")
+ " " + (tp == 'L' ? "letter" : "word"))
+ " cell.");
pnlBonuses.add(but);
}
}
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(evt.getActionCommand());
Highlighter.shadeSymmetric(currentCell,backgrounds[i]);
}
This has NOW (after edits) EVEN MORE SO been the best thread I've initiated, in terms of quality of responses and all that I've learned because of them. THANK YOU ALL.
BUT I STILL haven't managed to appropriately use setActionCommand. Whatever I did to TRY to use it wound up being so much longer code-wise that I gave up and went for the short and easy but inappropriate.
Any thoughts about how to use set... and getActionCommand the right way (i.e., as Actions) without adding a ton of code to do so?
I have a code below
jSlider1.setValue(0);
int i =0;
while (i <= jSlider1.getMaximum()) {
jSlider1.setValue(i);
// JOptionPane.showMessageDialog(rootPane,"deded");
Thread.sleep(2000);
i++;
}
What I want is I want to move JSlider automatically its min to max value. I have writeen above code. But I can not get the output. But when I remove the comment of "JOptionPane.showMessageDialog(rootPane,"deded"); " , It's work properly with the message. Please help me. I am in big trouble. I like to have a sample code if any one know the sollution.
Thank you
I am assuming you have create some JPanel or JFrame where you add this slider so,
final JSlider jSlider1 = new JSlider();
jSlider1.setValue(0);
jSlider1.setMaximum(100);
jSlider1.setBounds(0, 50, 300, 50);
final Timer increaseValue = new Timer(50, new ActionListener() {// 50 ms interval in each increase.
public void actionPerformed(ActionEvent e) {
if (jSlider1.getMaximum() != jSlider1.getValue()) {
jSlider1.setValue(jSlider1.getValue() + 1);
} else {
((Timer) e.getSource()).stop();
}
}
});
panel.add(jSlider1);
JButton moveSlider = new JButton("Start");
moveSlider.setBounds(10, 106, 55, 30);
moveSlider.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
increaseValue.start();
}
});
panel.add(moveSlider);