Upating a frame
- From: "Johannes Beekhuizen" <jbeekhui@xxxxxxxxxxxxxxxxxx>
- Date: Sun, 28 Aug 2005 11:42:14
Hello,
I have a frame with three panels. One of the panels lets me change the
language. When that happens I like to change the texts in all panels.
How to do that? I looked for repaint, redraw, update... but could not
find anything that seemed to do what I wanted.
Source follows below.
Regards,
Hans.
jdh dot beekhuizen at duinheks dot xs4all dot nl
=== import ===
/*
* @(#)Login.java 1.00 - 25/07/2005
*
* Copyright 2005 Sales Supporting System B.V.
* All rights reserved.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import nl.Relotec.*;
/**
* Login and language setting utility.
*
* @author Johannes D.H. Beekhuizen
* @version 1.00 - 25/07/2005
*
* Version 1.00 - initial development
*/
public class Login extends JPanel
implements ActionListener {
private static String language = "Language";
private static String login = "Login";
private static String info = "info";
JPanel languagePanel,
loginPanel;
JComponent buttonPanel;
JComboBox languageCombo;
JFrame controllingFrame;
JPasswordField passwordField;
static ResourceBundle messageBundle;
static Locale myLocale;
static Language myLanguage;
int selectedIndex;
public Login(JFrame f) {
// Use the default FlowLayout.
controllingFrame = f;
// Create the sub panels
languagePanel = createLanguagePanel();
loginPanel = createLoginPanel();
buttonPanel = createButtonPanel();
// Add the sub panels to the controlliung frame
add(languagePanel);
add(loginPanel);
add(buttonPanel);
}
/**
* Create the language choice panel
*/
protected JPanel createLanguagePanel() {
selectedIndex = myLanguage.getIndex(myLocale);
JPanel p = new JPanel();
languageCombo = new JComboBox(Language.languageList);
languageCombo.setSelectedIndex(selectedIndex);
languageCombo.setEditable(false);
languageCombo.setAlignmentX(Component.LEFT_ALIGNMENT);
languageCombo.setActionCommand(language);
languageCombo.addActionListener(this);
JLabel languageLabel =
new JLabel(messageBundle.getString("language"));
// Lay out everything.
p.add(languageLabel, BorderLayout.LINE_START);
p.add(languageCombo, BorderLayout.LINE_END);
p.setAlignmentX(Component.LEFT_ALIGNMENT);
return p;
}
/**
* Create the login panel
*/
protected JPanel createLoginPanel() {
JPanel p = new JPanel();
// The login panel
passwordField = new JPasswordField(10);
passwordField.setEchoChar('*');
passwordField.setActionCommand(login);
passwordField.addActionListener(this);
JLabel passwordLabel =
new JLabel(messageBundle.getString("password"));
passwordLabel.setLabelFor(passwordField);
p.add(passwordLabel, BorderLayout.LINE_START);
p.add(passwordField, BorderLayout.LINE_END);
return p;
}
/**
* Create the button panel
*/
protected JComponent createButtonPanel() {
JPanel p = new JPanel(new GridLayout(1,0));
JButton loginButton = new JButton(login);
JButton infoButton = new JButton(info);
loginButton.setActionCommand(login);
infoButton.setActionCommand(info);
loginButton.addActionListener(this);
infoButton.addActionListener(this);
p.add(loginButton);
p.add(infoButton);
return p;
}
/**
* Check the required action and act accordingly
*/
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if ( language.equals(cmd) ) { // Language selected
myLocale =
myLanguage.newLocale(languageCombo.getSelectedIndex());
System.out.println(myLocale);
messageBundle = ResourceBundle.getBundle("Login", myLocale);
repaint();
}
if ( login.equals(cmd) ) { // Process the password.
char[] input = passwordField.getPassword();
if (isPasswordCorrect(input)) {
JOptionPane.showMessageDialog(controllingFrame,
"Success! You typed the right password.");
try {
String[] cmdArray = new String[2];
cmdArray[0] = "java";
cmdArray[1] = "Greetings";
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(cmdArray);
runtime.exit(0);
} catch (IOException ioex) { System.out.println(ioex); }
}
else {
JOptionPane.showMessageDialog(controllingFrame,
"Invalid password. Try again.",
"Error Message",
JOptionPane.ERROR_MESSAGE);
}
// Zero out the possible password, for security.
for (int i = 0; i < input.length; i++) {
input[i] = 0;
}
passwordField.selectAll();
resetFocus();
}
if ( info.equals(cmd) ) { // The user asked for help.
JOptionPane.showMessageDialog(controllingFrame,
messageBundle.getString("info1") + "\n" +
messageBundle.getString("info2") + "\n" +
messageBundle.getString("info3") );
}
}
/**
* Checks the passed-in array against the correct password.
* After this method returns, you should invoke eraseArray
* on the passed-in array.
*/
private static boolean isPasswordCorrect(char[] input) {
boolean isCorrect = true;
char[] correctPassword = { 'b', 'u', 'g', 'a', 'b', 'o', 'o' };
if (input.length != correctPassword.length) {
isCorrect = false;
} else {
for (int i = 0; i < input.length; i++) {
if (input[i] != correctPassword[i]) {
isCorrect = false;
}
}
}
// Zero out the password.
for (int i = 0; i < correctPassword.length; i++) {
correctPassword[i] = 0;
}
return isCorrect;
}
// Must be called from the event-dispatching thread.
protected void resetFocus() {
passwordField.requestFocusInWindow();
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
// Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
// Create and set up the window.
JFrame frame = new JFrame("Relotec login");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create and set up the content pane.
final Login newContentPane = new Login(frame);
newContentPane.setOpaque(true); //content panes must be opaque
newContentPane.setLayout(new BoxLayout(newContentPane,
BoxLayout.Y_AXIS));
frame.setContentPane(newContentPane);
// Make sure the focus goes to the right component
// whenever the frame is initially given the focus.
frame.addWindowListener(new WindowAdapter() {
public void windowActivated(WindowEvent e) {
newContentPane.resetFocus();
}
});
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
myLanguage = new Language();
myLocale = myLanguage.MyLocale(args);
messageBundle = ResourceBundle.getBundle("Login", myLocale);
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
=== tropmi ===
.
- Prev by Date: Re: setting size of elements
- Next by Date: Re: Layout question
- Previous by thread: setFont()
- Next by thread: Different height at JComboBox editor and renderer
- Index(es):