This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I am trying to make a text based adventure that runs in a jframe but when i run the program i get a NullPointer error on line 97 (first time i append console in the game method) and I have no idea how to fix it. I am relatively new to java so its probably something simple that I just don't know.
my code is here
package window;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Gui extends JFrame {
public JPanel contentPane;
public static JTextField input;
public static JButton send;
public static JTextArea console;
public static JTextArea invintory;
public static JTextArea stats;
static String i = "";
/**
* Launch the application.
*/
/**
* Create the frame.
*/
public Gui() {
//variables
int Gold = 20;
int Health = 100;
int MaxHealth = 100;
//variables end
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JTextArea console = new JTextArea();
console.setBounds(10, 11, 281, 214);
contentPane.add(console);
input = new JTextField();
input.setBounds(10, 236, 281, 20);
contentPane.add(input);
input.setColumns(10);
JTextArea stats = new JTextArea();
stats.setBounds(301, 11, 123, 53);
contentPane.add(stats);
JTextArea invintory = new JTextArea();
invintory.setBounds(301, 75, 128, 137);
contentPane.add(invintory);
JButton send = new JButton("Send");
send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String i = input.getText();
input.setText("");
stats.setText("");
stats.append("Health: " + Health + "/" + MaxHealth + "\n");
stats.append("Gold: " + Gold);
}
});
send.setBounds(301, 224, 128, 32);
contentPane.add(send);
stats.append("Health: " + Health + "/" + MaxHealth + "\n");
stats.append("Gold: " + Gold);
}
public static void game (JButton send, JTextArea console, JTextArea stats, JTextArea invintory, JTextField input, String i) {
//start
//START
//START
//START
//START
while (true) {
console.append("You wake up open feild, with vast amounts of wheet in every direction");
console.append("There is a path going in either direction what do you want to do");
console.append("\t1. Go left.");
console.append("\t2. Go right.");
while (i == "") {
}
if (i == "1") {
console.append("1");
break;
}
else if (i == "2") {
console.append("2");
break;
}
}
//END
//END
//END
//END
}
public static void main(String[] args) {
try {
Gui frame = new Gui();
frame.setVisible(true);
Gui.game(send, console, stats, invintory, input, i);
} catch (Exception e) {
e.printStackTrace();
}
}
}
In your global variable console is null
public static JTextArea console;
You initialized it in Gui method
JTextArea console = new JTextArea();
So here in Gui method your global console variable is not initialized, This creates a local variable console under Gui method.
To initialize global variable in Gui method you need to initialize this way
console = new JTextArea();
You did this mistake to all variables so edit your code this way
public Gui() {
//variables
int Gold = 20;
int Health = 100;
int MaxHealth = 100;
//variables end
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
console = new JTextArea();
console.setBounds(10, 11, 281, 214);
contentPane.add(console);
input = new JTextField();
input.setBounds(10, 236, 281, 20);
contentPane.add(input);
input.setColumns(10);
stats = new JTextArea();
stats.setBounds(301, 11, 123, 53);
contentPane.add(stats);
invintory = new JTextArea();
invintory.setBounds(301, 75, 128, 137);
contentPane.add(invintory);
send = new JButton("Send");
send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String i = input.getText();
input.setText("");
stats.setText("");
stats.append("Health: " + Health + "/" + MaxHealth + "\n");
stats.append("Gold: " + Gold);
}
});
send.setBounds(301, 224, 128, 32);
contentPane.add(send);
stats.append("Health: " + Health + "/" + MaxHealth + "\n");
stats.append("Gold: " + Gold);
}
Related
I need some help about Java GUI. I want to add 10 numbers to memory from JTextField. And when it's done JButton must gonna be disable and program must show me a message dialog. How can I do this?
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Uygulama extends JFrame {
private JPanel contentPane;
private JTextField textField;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Uygulama frame = new Uygulama();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Uygulama() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 233, 140);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setBounds(48, 13, 116, 22);
contentPane.add(textField);
textField.setColumns(10);
JButton btnHesapla = new JButton("HESAPLA");
btnHesapla.addActionListener(new ActionListener() {int counter = 0;
public void actionPerformed(ActionEvent arg0) {
while(counter < 9) {
counter++;
if(counter == 10) {
buton.setEnabled(false);}
}
});
btnHesapla.setBounds(58, 48, 97, 25);
contentPane.add(btnHesapla);
}
}
Add an ActionListener to your JButton if using Swing, and inside that, you'll increment a global variable by one. Then you ask if the variable is == 10 and do yourButton.setEnabled(false) or yourButton.setEnabled(counter < 10) if you're not using an if. You already have your listener set up, so it's only a matter of adding a variable you'll increment inside it and a call to setEnabled of your button.
Here's a working example with an ArrayList used to store the numbers. No need to use a global count variable here because the size of the ArrayList already tells us how many numbers we have stored :-)
public class NumbersApplication extends JFrame {
private JPanel contentPane;
private JTextField textField;
private List<Integer> numbers;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
NumbersApplication frame = new NumbersApplication();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public NumbersApplication() {
numbers = new ArrayList<>();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 233, 140);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setBounds(48, 13, 116, 22);
contentPane.add(textField);
textField.setColumns(10);
JButton btnHesapla = new JButton("HESAPLA");
btnHesapla.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
int number = Integer.parseInt(textField.getText());
numbers.add(number);
btnHesapla.setEnabled(numbers.size() < 10);
System.out.println(number + " has been added to memory.");
if (numbers.size() == 10) {
System.out.println("Your numbers are: " + numbers);
}
}
});
btnHesapla.setBounds(58, 48, 97, 25);
contentPane.add(btnHesapla);
}
}
I'm trying to send a certain string when a button is pressed in my GUI. My Client class is currently running to keep taking string commands from the command line and send them to the server where they will be processed and a response will be returned.
How can I now send the data through my GUI and move the results back to my GUI?
E.g. I have a button called "pickup" which, when clicked will send the string "PICKUP" to the server, through the Client class.
Likewise, the response from the server would be either "SUCCESS" or "FAIL" which would be printed through the Thread "serverResponse" in my Client class and this needs to somehow be sent to an arbitrary method in the playerGUI class as a parameter.
Thanks for any help, sorry for not using the conventional class/method/field naming styles!
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class playerGUI {
private JFrame frame = new JFrame();
private JPanel displayPanel;
private JTextPane hostTextPane;
private JTextPane portTextPane;
private static Client newclient;
public static void main(String[] args) {
playerGUI GUI = new playerGUI();
GUI.frame.setVisible(true);
newclient = new Client(GUI);
}
/**
* Create the application.
*/
public playerGUI() {
frame.getContentPane().setBackground(new Color(255, 255, 255));
frame.getContentPane().setLayout(null);
frame.setBounds(100, 100, 500, 630);
frame.setUndecorated(false); // REMOVES MENU BAR
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel humanGameWindow = new JPanel();
humanGameWindow.setLayout(null);
humanGameWindow.setBackground(Color.LIGHT_GRAY);
humanGameWindow.setBounds(0, 0, 500, 630);
humanGameWindow.setVisible(false);
JButton pickup = new JButton("Pickup");
pickup.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//I WANT TO SEND THE STRING "PICKUP" TO WHERE THE STARS ARE IN Client.class
}
});
pickup.setBackground(new Color(100, 149, 237));
pickup.setBounds(40, 555, 100, 40);
displayPanel = new JPanel();
displayPanel.setBounds(48, 89, 400, 400);
displayPanel.setLayout(new GridLayout(5, 5));
displayPanel.setPreferredSize(new Dimension((int) (400), (int) (400)));
for (int i = 1; i < 26; i++) {
displayPanel.add(new JLabel("Label " + i));
}
JButton Look = new JButton("Look");
Look.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
Look.setBackground(new Color(100, 149, 237));
Look.setBounds(40, 514, 100, 40);
humanGameWindow.add(Look);
humanGameWindow.add(pickup);
humanGameWindow.add(displayPanel);
final JPanel mainMenu = new JPanel();
mainMenu.setLayout(null);
mainMenu.setBackground(Color.DARK_GRAY);
mainMenu.setBounds(0, 0, 500, 630);
mainMenu.setVisible(true);
JLabel mainMenuTitle = new JLabel("DUNGEON OF DOOM!!");
mainMenuTitle.setForeground(new Color(100, 149, 237));
mainMenuTitle.setFont(new Font("Moire", Font.BOLD, 28));
mainMenuTitle.setBounds(50, 13, 380, 50);
JButton mainMenuQuit = new JButton("Quit");
mainMenuQuit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
mainMenuQuit.setBackground(new Color(100, 149, 237));
mainMenuQuit.setBounds(220, 345, 70, 55);
JButton playGameHuman = new JButton("Play Game Human");
playGameHuman.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mainMenu.setVisible(false);
humanGameWindow.setVisible(true);
}
});
playGameHuman.setBackground(new Color(100, 149, 237));
playGameHuman.setBounds(50, 345, 150, 55);
mainMenu.add(mainMenuTitle);
mainMenu.add(mainMenuQuit);
mainMenu.add(playGameHuman);
frame.getContentPane().add(humanGameWindow);
frame.getContentPane().add(mainMenu);
frame.setVisible(true);
}
}
This is the Client class, the thread is where I want to send the response to the GUI class to process and display a specific output. The asterisks is where I want to send the text from button presses in the GUI class (there are other buttons I have deleted the code for easier reading!).
import java.net.*;
import java.io.*;
public class Client{
public Client(playerGUI GUI){
try{
final Socket sock = new Socket("localhost",4444);
final DataInputStream in = new DataInputStream(sock.getInputStream());
final PrintStream out = new PrintStream(sock.getOutputStream());
DataInputStream inputLine = new DataInputStream(new BufferedInputStream(System.in));
final Thread serverResponse = new Thread(){
public void run(){
System.out.println("DUNGEON OF DOOM HAS STARTED");
if(sock != null){
if(in != null){
try{
String response;
while((response = in.readLine()) != null){
//I WANT TO SEND "response" TO THE GUI CLASS
System.out.println(response);
}
}catch(UnknownHostException uhe){
System.err.println("Unknown host1: " + uhe);
}catch(IOException ioe){
System.err.println("IOException1: " + ioe);
}catch(NullPointerException npe){
System.err.println("Null Pointer1: " + npe);
}
}
}
}
};
serverResponse.start();
if(sock != null){
if(out != null){
try{
while(true){
String sending = *************************
//String sending = inputLine.readLine();
out.println(sending);
if(sending.equals("QUIT")) break;
}
}catch(UnknownHostException uhe2){
System.err.println("Unknown host2: " + uhe2);
}catch(IOException ioe2){
System.err.println("IOException2: " + ioe2);
}catch(NullPointerException npe2){
System.err.println("Null Pointer2: " + npe2);
}
}
}
out.close();
in.close();
sock.close();
}catch(UnknownHostException uhe3){
System.err.println("Unknown host3: " + uhe3);
}catch(IOException ioe3){
System.err.println("IOException3: " + ioe3);
}catch(NullPointerException npe3){
System.err.println("Null Pointer3: " + npe3);
}
}
}
I'm kind of new to java, so go easy. I'm trying to make a simple game where everytime you click a button it adds one to a variable. That all works fine, but i'm also trying to display the variable to my JFrame. This is where the trouble comes, I click the button, it does add one to my variable (I printed the variable to the console to be sure) but the JFrame isn't updating the variable. I should also note, when you first open the game, it opens a window allowing you to type a username, this is in a separate class, which contains my main method. Here is my code for my second window, the one with the problem:
import javax.swing.BorderFactory;
public class Game extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private String name;
public static int pennies = 0;
public static int dollars = 0;
public static int moneyAddRate = 1;
private JButton btnAddMoney = new JButton(new ImageIcon("C:\\Users\\Tanner\\git\\Money-Bags\\res\\coins\\oneCent.png"));
private Border emptyBorder = BorderFactory.createEmptyBorder();
public Game(String name) {
this.name = name;
createWindow();
}
private void createWindow() {
setTitle(name + "'s Economy");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
btnAddMoney.setBounds(329, 244, 96, 96);
btnAddMoney.setBorder(emptyBorder);
contentPane.add(btnAddMoney);
btnAddMoney.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
addMoney();
}
});
JLabel lblPennies = new JLabel("You have " + pennies + " Pennies");
lblPennies.setBounds(10, 11, 152, 24);
contentPane.add(lblPennies);
JLabel lblDollars = new JLabel(dollars + " Dollars");
lblDollars.setBounds(10, 70, 152, 24);
contentPane.add(lblDollars);
JLabel lblAnd = new JLabel("&");
lblAnd.setBounds(10, 45, 61, 14);
contentPane.add(lblAnd);
setVisible(true);
}
private void addMoney() {
pennies += moneyAddRate;
System.out.println(pennies + " " + dollars);
contentPane.validate();
contentPane.repaint();
}
}
It isn't updating because you aren't updating any Component with the new pennies amount. Your addMoney() method should look something like this:
private void addMoney() {
pennies += moneyAddRate;
lblPennies.setText(String.format("You have %d pennies", pennies));
lblPennies.repaint();
}
This is the source code for my calculator that i am building in eclipse windowbuilder, i am trying to make two textFields read input and output the area for a simple rectangle, i can type the numbers into the boxes however the numbers do not compute and i am at a loss as to what i could be doing wrong, please critique my code and let me know why it isn't working. Thank you
*Edit, i have removed and changed a few things and now the Answer box displays 0.0 and nothing else
import java.awt.EventQueue;
public class GUI {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUI window = new GUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 694, 499);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
tabbedPane.addTab("New tab", null, panel, null);
panel.setLayout(null);
JLabel lblInputBase = new JLabel("Input Base");
lblInputBase.setBounds(0, 0, 57, 39);
panel.add(lblInputBase);
textField = new JTextField();
textField.setBounds(0, 28, 86, 20);
panel.add(textField);
textField.setColumns(10);
double value = Double.parseDouble(textField.getText());
JLabel lblInputHeight = new JLabel("Input Height");
lblInputHeight.setBounds(0, 212, 68, 20);
panel.add(lblInputHeight);
textField_1 = new JTextField();
textField_1.setBounds(0, 230, 86, 20);
panel.add(textField_1);
textField_1.setColumns(10);
double value_1 = Double.parseDouble(textField_1.getText());
double Area = value*value_1;
String finalArea = Double.toString(Area);
JLabel lblArea = new JLabel("Area:");
lblArea.setBounds(244, 119, 75, 68);
panel.add(lblArea);
textField_2 = new JTextField();
textField_2.setBounds(273, 143, 86, 20);
panel.add(textField_2);
textField_2.setColumns(10);
textField_2.setEditable(false);
textField_2.setText(finalArea);
}
}
Your code is really confusing, but here are some problems that I found:
double value = Double.parseDouble(textField.getText());
This is being run before the user has any chance to even look at the gui. textField.getText() will return an empty string here, and the parseDouble method will always throw a NumberFormatException when the program is run, as it does not consider an empty string to be a valid number format.
double Area = Double.parseDouble(value)*Double.parseDouble(value_1);
Here, the variable value is already a double and does not need to be parsed, and the variable value_1 doesn't exist.
Where you meant to assign an actionListener to textField_1, you assigned a second one to textField.
Overall, it seems that all your calculation logic is being run in your initialize() method, and this means that it is only being run once. You'll want your math to be run whenever one of the text fields is updated.
Your field names are horribly confusing. Your textField variable names should give some indication of their function, like areaTextField, baseTextField, heightTextField.
Here is a modification of your code that seems to function as you intended yours to function. Note the addition of a calculate() method that is called by the action listeners that will find the area and display it, or display NaN (Not a Number) if either of the input fields are not valid numbers. All of your GUI code is retained as is, excepting the renaming of the text field variables.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
public class GUI {
private JFrame frame;
private JTextField baseTextField;
private JTextField heightTextField;
private JTextField areaTextField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUI window = new GUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 694, 499);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
tabbedPane.addTab("New tab", null, panel, null);
panel.setLayout(null);
JLabel lblInputBase = new JLabel("Input Base");
lblInputBase.setBounds(0, 0, 57, 39);
panel.add(lblInputBase);
baseTextField = new JTextField();
baseTextField.setBounds(0, 28, 86, 20);
panel.add(baseTextField);
baseTextField.setColumns(20);
baseTextField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
calculate();
}
});
JLabel lblInputHeight = new JLabel("Input Height");
lblInputHeight.setBounds(0, 212, 68, 20);
panel.add(lblInputHeight);
heightTextField = new JTextField();
heightTextField.setBounds(0, 230, 86, 20);
panel.add(heightTextField);
heightTextField.setColumns(20);
heightTextField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
calculate();
}
});
JLabel lblArea = new JLabel("Area:");
lblArea.setBounds(244, 119, 75, 68);
panel.add(lblArea);
areaTextField = new JTextField();
areaTextField.setBounds(273, 143, 86, 20);
panel.add(areaTextField);
areaTextField.setColumns(20);
areaTextField.setEditable(false);
}
private void calculate() {
try {
double base = Double.parseDouble(baseTextField.getText());
double height = Double.parseDouble(heightTextField.getText());
double area = base * height;
areaTextField.setText(String.format("%.4f", area));
} catch (NumberFormatException e) {
areaTextField.setText("NaN");
}
}
}
Note that you need to hit the enter key in one of the two fields to update the area field. That is what it takes to trigger the ActionListener for a JTextField.
I have written a program that updates the LookAndFeel on a test GUI first. Once the user clicks the "select" button, the LookAndFeel is passed to a main GUI method that creates a new window using that selection.
It works fine. Although, as the user cycles through the various LookAndFeels, the test GUI does not really update and show the changes. Each time the change button is clicked I have it update and display the current LookAndFeel(and repain();), so I know it is changing. Also, when you hit select, the new GUI is created using whatever option you were on at the time(even though the test GUI window was not showing the changes).
What am I doing wrong?
import javax.swing.*; //UI.Manager <-- available
import java.awt.*;
import java.awt.event.*;
import javax.swing.UIManager.*;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.net.URL;
public class gui1 {
static JFrame frame1, testFrame;
static Container pane, testPane;
static JButton btnConnect, btnDisconnect, changeButton, selectButton;
static JLabel lblServer, lblUsername, lblPassword, lblPort, testLabel;
static JTextField txtServer, txtUsername, txtPassword, txtPort;
static Insets insets;
static JTextArea area, testArea;
public JScrollPane scroller, testScroller;
static int currentIndex;
URL frameImage;
Image img;
static boolean LookAndFeelSelected;
public static void main(String[] args){
gui1 begin = new gui1();
}
public gui1() {
//build our test gui & components
testFrame = new JFrame("Select which LookAndFeel you prefer..");
testFrame.setSize(375, 250);
testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
testLabel = new JLabel("Text Example", JLabel.CENTER);
testArea = new JTextArea(300,200);
testPane = testFrame.getContentPane();
changeButton = new JButton("Change");
selectButton = new JButton("Select");
testScroller = new JScrollPane(testArea);
testScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
testScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
testPane.add(changeButton);
testPane.add(selectButton);
testFrame.getContentPane().add(BorderLayout.WEST, changeButton);
testFrame.getContentPane().add(BorderLayout.EAST, selectButton);
testFrame.getContentPane().add(BorderLayout.CENTER, testScroller);
testFrame.getContentPane().add(BorderLayout.NORTH, testLabel);
changeButton.addActionListener(new changeListener());
selectButton.addActionListener(new selectListener());
testFrame.setVisible(true);
while(!LookAndFeelSelected) {
try{
Thread.sleep(500);
}catch(InterruptedException ex) {ex.printStackTrace();}
}
System.out.println("Selected!");
//start the real GUI with selected LookAndFeel
testFrame.dispose();
buildGui(UIManager.getLookAndFeel());
}
public void buildGui(LookAndFeel chosen){
//The frame and panel
frame1 = new JFrame("Sample GUI Application");
frame1.setSize(800, 230);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane = frame1.getContentPane();
pane.setLayout(null);
insets = pane.getInsets();
//Set the image
try{
frameImage = new URL("http://i.imgur.com/jMaYO1f.jpg");
img = ImageIO.read(frameImage);
}catch(IOException ex){ex.printStackTrace();}
frame1.setIconImage(img);
//Construct our objects for gui
btnConnect = new JButton("Connect");
btnDisconnect = new JButton("Disconnect");
lblServer = new JLabel("Remote Host: ");
lblUsername = new JLabel("Username: ");
lblPassword = new JLabel("Password: ");
lblPort = new JLabel("Port #");
txtServer = new JTextField(10);
txtPassword = new JTextField(10);
txtUsername = new JTextField(10);
txtPort = new JTextField(5);
area = new JTextArea(700, 125);
area.setLineWrap(true);
area.setEditable(false);
scroller = new JScrollPane(area);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
//Add components to the pane
pane.add(lblServer);
pane.add(lblServer);
pane.add(lblPassword);
pane.add(lblUsername);
pane.add(lblPort);
pane.add(txtServer);
pane.add(txtPort);
pane.add(txtPassword);
pane.add(txtUsername);
pane.add(btnConnect);
pane.add(btnDisconnect);
pane.add(scroller); //includes area and scroller!
//Arrange our components in the pane
lblServer.setBounds(insets.left + 5, insets.top + 5, lblServer.getPreferredSize().width, lblServer.getPreferredSize().height);
txtServer.setBounds(lblServer.getX() + lblServer.getWidth() + 5, insets.top + 5, txtServer.getPreferredSize().width, txtServer.getPreferredSize().height);
lblUsername.setBounds(txtServer.getX() + txtServer.getWidth() + 5, insets.top + 5, lblUsername.getPreferredSize().width, lblUsername.getPreferredSize().height);
txtUsername.setBounds(lblUsername.getX() + lblUsername.getWidth() + 5, insets.top + 5, txtUsername.getPreferredSize().width, txtUsername.getPreferredSize().height);
lblPassword.setBounds(txtUsername.getX() + txtUsername.getWidth() + 5, insets.top + 5, lblPassword.getPreferredSize().width, lblPassword.getPreferredSize().height);
txtPassword.setBounds(lblPassword.getX() + lblPassword.getWidth() + 5, insets.top + 5, txtPassword.getPreferredSize().width, txtPassword.getPreferredSize().height);
lblPort.setBounds(txtPassword.getX() + txtPassword.getWidth() + 5, insets.top + 5, lblPort.getPreferredSize().width, lblPort.getPreferredSize().height);
txtPort.setBounds(lblPort.getX() + lblPort.getWidth() + 5, insets.top + 5, txtPort.getPreferredSize().width, txtPort.getPreferredSize().height);
btnConnect.setBounds(txtPort.getX() + txtPort.getWidth() + 5, insets.top + 5, btnConnect.getPreferredSize().width, btnConnect.getPreferredSize().height);
btnDisconnect.setBounds(insets.left + 5, lblServer.getY() + lblServer.getHeight() + 15, btnDisconnect.getPreferredSize().width, btnDisconnect.getPreferredSize().height);
scroller.setBounds(insets.left + 5, btnDisconnect.getX() + btnDisconnect.getHeight() + 33, 760, 125);
//Add button listeners
btnConnect.addActionListener(new connectListener());
btnDisconnect.addActionListener(new disconnectListener());
//Change the Look & Feel
try {
UIManager.setLookAndFeel(chosen);
} catch (Exception ex) {ex.printStackTrace();}
frame1.setVisible(true);
frame1.setResizable(false);
}
public static class connectListener implements ActionListener {
public void actionPerformed(ActionEvent event){
String toField = String.format("ServerIP: %s \t Username: %s \t Password: %s \t Port: %s\n", txtServer.getText(), txtUsername.getText(),
txtPassword.getText(), txtPort.getText());
area.append(toField);
}
}
public static class disconnectListener implements ActionListener {
public void actionPerformed(ActionEvent event){
area.append("Disconnected ..\n");
}
}
// Here only for initial LookAndFeel selection!
public static class changeListener implements ActionListener {
public void actionPerformed(ActionEvent event){
LookAndFeelInfo[] lafArray = UIManager.getInstalledLookAndFeels(); //not actual LookAndFeel(just name), thus use LookAndFeelInfo
try {
if (currentIndex == lafArray.length - 1) {
currentIndex = 0;
System.out.println("You have seen all Look and Feels, starting over .. \n\n");
}
UIManager.setLookAndFeel(lafArray[currentIndex++].getClassName());
testFrame.getContentPane().validate();
testFrame.getContentPane().repaint(); //repaint the frame to update changes
}catch(Exception ex) {ex.printStackTrace();}
String LookAndFeelName = String.format("Changed to: %s",UIManager.getLookAndFeel().getClass()); //gets current and displays name
System.out.println(LookAndFeelName);
}
}
public static class selectListener implements ActionListener {
public void actionPerformed(ActionEvent event){
LookAndFeelSelected = true;
}
}
}
This isnt a repainting issue, The look and feel goes deeper than just painting colors. Look at this tutorial http://www.roseindia.net/java/example/java/swing/GettingAndSettingLAF.shtml
below where you change your look and feel on button click change:
testFrame.getContentPane().validate();
testFrame.getContentPane().repaint();
to
SwingUtilities.updateComponentTreeUI(testFrame);