I want to set a custom background image on a JTextArea
. I had looked in google but without a result, the background could be a logo, also I want to know how to set the background's resolution.
I Have only one class in a package. I have a MySQL connector driver as Referenced Library, my workbench is Eclipse, exporting jar with the Fat-jar plugin
Code:
public class panel extends JPanel implements ActionListener {
protected JTextField textField, textField2;
protected static JTextArea textArea;
private final static String newline = "\n";
public panel() {
super(new GridBagLayout());
textField = new JTextField(30);
textField.addActionListener(this);
textField.setBackground(Color.LIGHT_GRAY);
textField2 = new JTextField(30);
textField2.addActionListener(this);
textField2.setEnabled(false);
textArea = new JTextArea(30, 100);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
//Add Components to this panel.
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
add(textField, c);
add(textField2, c);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add(scrollPane, c);
}
public void actionPerformed(ActionEvent evt) {
String select = textField.getText();
if(textField.getText().equalsIgnoreCase("connect")){
textArea.setForeground(Color.BLACK);
connect();
textField.setText("");
}else if(textField.getText().equalsIgnoreCase("select test")){
textArea.setForeground(Color.BLACK);
viewTest();
textField.setText("");;
}else if(textField.getText().equalsIgnoreCase("clear")){
textArea.setForeground(Color.BLACK);
textField.setText("");
clear();
}else if(textField.getText().equalsIgnoreCase("commands")){
textArea.setForeground(Color.BLACK);
commandsmenu();
textField.setText("");
}else if(textField.getText().equalsIgnoreCase("insertinto test")){
textField2.setEnabled(true);
if(textField2.getText().equals("")){
textArea.append("Please add the VALUES of the table on the second textfield! Syntax: 'Agevaulue', namevalue, adressvalue !" + newline);
}else{
textArea.setForeground(Color.BLACK);
InsertIntoTest();
textField2.setText("");
textField2.setEnabled(false);
}
}
else {
clear();
textArea.setForeground(Color.RED);
textArea.append("Uknown Command -- use: commands -- to see all commands!");
textField.selectAll();
}
//Make sure the new text is visible, even if there
//was a selection in the text area.
textArea.setCaretPosition(textArea.getDocument().getLength());
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Java + MySQL Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add contents to the window.
frame.add(new panel());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public void connect(){
Connection conn = null;
try
{
String url = "jdbc:mysql://localhost/users";
Class.forName("com.mysql.jdbc.Driver");
textArea.append("Database connection established");
conn = DriverManager.getConnection(url, "root", "kristian76");
}
catch (Exception e){
e.printStackTrace();
}
}
public void viewTest()
{
Connection conn = null;
try{
String url = "jdbc:mysql://localhost/users";
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, "root", "kristian76");
String query = "SELECT * FROM test";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()){
int age = rs.getInt("age");
String name = rs.getString("name");
String adress = rs.getString("adress");
textArea.append("AGE: " + age + " |Name: " + name + " |Adress: " + adress + newline);
}
}catch(Exception e){
textArea.setForeground(Color.RED);
textArea.append("Got an Exception!" + newline);
textArea.append(e.getMessage());
}
}public void InsertIntoTest() {
Connection conn = null;
try{
String url = "jdbc:mysql://localhost/users";
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, "root", "kristian76");
String query = "INSERT INTO test(age, name, adress)VALUES(" + textField2.getText() + ")";
Statement stmt = conn.createStatement();
stmt.executeUpdate("INSERT INTO test(age, name, adress)VALUES(" + textField2.getText() + ")");
textArea.append("Data Imported!" + newline);
}catch(Exception e){
textArea.setForeground(Color.RED);
textArea.append("Got an Exception" + newline);
textArea.append(e.getMessage());
}
}
public void clear(){
textArea.setText("");
}
public void commandsmenu() {
textArea.append("select <table> - read a <table>" + newline);
textArea.append("clear - clear output" + newline);
textArea.append("commands - see Commands" + newline);
textArea.append("insertinto <table> - insert data into <table>" + newline);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}