Recently I have written a pair of applications with JAVA/Swing with a simple GUI:
They look OK, they work OK. Until I executed then on a Windows 7 tablet (HP Slate). They both look empty, with a tiny symbol in the middle. This picture shows how the first application (the JTextArea one) looks over the tablet:
What's the problem here and how can I solve it?
I'm showing the code, but since it looks fine when using a computer, I guess this is not the problem:
import java.io.*;
import java.net.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.text.DefaultCaret;
class EstacionBase extends JFrame{
private static final long serialVersionUID = 1L;
private static final String logFile = "log.txt";
private DateFormat df;
private FileWriter logWriter;
private Socket sisnetSocket;
private ServerSocket serverSocket;
private Socket clientSocket;
private BufferedWriter outToPlane;
private BufferedWriter outToDataServer;
private BufferedReader inputFromDataServer;
private JTextArea console;
private boolean terminated;
public EstacionBase() {
setTitle("Base station");
createGUI();
pack();
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
try{
df = new SimpleDateFormat ("[hh:mm:ss]");
logWriter = new FileWriter(System.getProperty("user.dir") + "\\" + logFile, true);
serverSocket = new ServerSocket(PORT); // A socket I use for listening for connections
} catch(IOException e){
JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
//mainLoop(); // doing some stuff
try{
logWriter.close();
serverSocket.close();
} catch(IOException e){
JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void createGUI() {
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.BOTH;
console = new JTextArea(40, 80);
JScrollPane scroll = new JScrollPane(console);
DefaultCaret caret = (DefaultCaret) console.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); // Autoscroll
console.setText("Welcome to base station");
console.setEditable(false);
console.setVisible(true);
add(scroll, c);
}
private void writeToConsole(String msg) throws IOException{
Date now = new Date();
String timeStr = df.format(now);
console.append("\n");
console.append(timeStr + " " + msg);
writeToLogFile(msg, timeStr);
}
private void writeToLogFile(String msg, String timeStr) throws IOException{
logWriter.write(timeStr + " " + msg + CRLF);
logWriter.flush();
}
public static void main(String[] arg) {
EstacionBase frame = new EstacionBase();
}
}
This is a GridBagLayout
problem; I'm not sure of the optimal fix, but it works fine with the default JFrame
layout, BorderLayout.CENTER
. Functionally, these three layouts have similar effects:
BorderLayout.CENTER
: the default for JFrame#add()
.
GridLayout()
: one column, one row, no gaps.
GridBagLayout()
: the default GridBagConstraints()
has all fields set to default values.
As an aside, Swing GUI objects should be constructed and manipulated only on the event dispatch thread.