Skip to content

Instantly share code, notes, and snippets.

@larsbutler
Created June 8, 2016 21:05
Show Gist options
  • Save larsbutler/154dcb61c039df38f328995fc460c988 to your computer and use it in GitHub Desktop.
Save larsbutler/154dcb61c039df38f328995fc460c988 to your computer and use it in GitHub Desktop.
ChatGUIMockup - Java 8 - Swing
To compile:
$ javac ChatGUIMockup.java
To run:
$ java ChatGUIMockup
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ChatGUIMockup extends JFrame {
private JPanel panel;
private JLabel chatLogLabel = null;
private JTextArea chatLogTextArea = null;
private JScrollPane chatLogScrollPane = null;
private JLabel messageLabel = null;
private JTextField messageTextField = null;
private JButton sendButton = null;
public static final Dimension WINDOW_SIZE = new Dimension(500, 300);
public ChatGUIMockup() {
// Basic window configuration:
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(WINDOW_SIZE);
this.setMinimumSize(WINDOW_SIZE);
setUpGUI();
}
private static GridBagConstraints gbc() {
return new GridBagConstraints();
}
private void setUpGUI() {
panel = new JPanel();
this.add(panel);
GridBagLayout gridLayout = new GridBagLayout();
panel.setLayout(gridLayout);
GridBagConstraints con;
{
chatLogLabel = new JLabel("Chat log:");
con = gbc();
con.gridx = 0;
con.gridy = 0;
con.fill = GridBagConstraints.HORIZONTAL;
con.anchor = GridBagConstraints.WEST;
con.weighty = 0.05;
panel.add(chatLogLabel, con);
}
{
chatLogTextArea = new JTextArea();
chatLogTextArea.setEditable(false);
chatLogScrollPane = new JScrollPane(
chatLogTextArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
con = gbc();
con.gridx = 0;
con.gridy = 1;
con.gridwidth = 3;
con.weighty = 0.9;
con.weightx = 1.0;
con.fill = GridBagConstraints.BOTH;
con.anchor = GridBagConstraints.WEST;
panel.add(chatLogScrollPane, con);
}
{
messageLabel = new JLabel("Message:");
con = gbc();
con.gridx = 0;
con.gridy = 2;
con.weightx = 0.2;
con.weighty = 0.05;
con.anchor = GridBagConstraints.WEST;
panel.add(messageLabel, con);
}
{
messageTextField = new JTextField();
con = gbc();
con.gridx = 1;
con.gridy = 2;
con.weightx = 0.6;
con.weighty = 0.05;
con.ipadx = 200;
con.anchor = GridBagConstraints.WEST;
panel.add(messageTextField, con);
}
{
sendButton = new JButton("Send");
sendButton.addActionListener(e -> {
String msg = messageTextField.getText();
postMessage(msg);
});
con = gbc();
con.gridx = 1;
con.gridy = 2;
con.weightx = 0.2;
con.weighty = 0.05;
con.anchor = GridBagConstraints.EAST;
panel.add(sendButton, con);
}
}
private void postMessage(String msg) {
if (!msg.equals("")) {
chatLogTextArea.append(String.format("Me: %s%n", msg));
messageTextField.setText("");
messageTextField.grabFocus();
}
}
public void start() {
this.pack();
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(e -> {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
if (messageTextField.hasFocus()) {
String msg = messageTextField.getText();
postMessage(msg);
}
}
return false;
});
messageTextField.grabFocus();
this.setVisible(true);
}
public static void main(String [] args) {
ChatGUIMockup app = new ChatGUIMockup();
app.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment