-3

i want to show my JTextArea after button CONNECT_BUTTON is clicked. What should I do ? Furthermore i'm trying to add some grid in my JTextArea because I want to use it for show some records from databse. Any ideas how to do that ?

package DataBase_Hospital;      

import java.awt.BorderLayout;
import java.awt.FlowLayout;          
import java.awt.GridLayout;      
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
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 java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class test extends JFrame implements ActionListener {



private JButton FIND_BUTTON;
private JButton MESSAGE_BUTTON;
private JButton CONNECT_BUTTON;
private JButton CLEAR_BUTTON;
private JButton ADD_BUTTON;
private JButton RAPORT_BUTTON;
private JButton EDIT_BUTTON;
private JButton DOWNLOAD_BUTTON;

private JTextArea DATABASE_FIELD;

private DatabaseManagement DATABASE;



public test(){

setTitle("Hospital Management");
setSize(900,600);

setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
JLabel background=new JLabel(new ImageIcon("/Users/Dominik/Desktop/j.jpg"));
add(background);

DATABASE_FIELD = new JTextArea();
JScrollPane scrollPane = new JScrollPane(DATABASE_FIELD);
scrollPane.setBounds(50, 50, 800, 400);
background.add(scrollPane);
DATABASE_FIELD.setEditable(true);
DATABASE_FIELD.setLayout(new GridLayout(3, 3));
//DATABASE_FIELD.setVisible(false);

CONNECT_BUTTON = new JButton();
CONNECT_BUTTON.setIcon(new ImageIcon("/Users/Dominik/Desktop/connect_no.png"));
CONNECT_BUTTON.setBounds(165, 500, 60, 60);
background.add(CONNECT_BUTTON);
CONNECT_BUTTON.addActionListener(this);
CONNECT_BUTTON.setToolTipText("Connect to Data Base");

FIND_BUTTON = new JButton();
FIND_BUTTON.setIcon(new ImageIcon("/Users/Dominik/Desktop/search.png"));
FIND_BUTTON.setBounds(240, 500, 60, 60);
background.add(FIND_BUTTON);
FIND_BUTTON.addActionListener(this);
FIND_BUTTON.setToolTipText("Find record in Data Base");


ADD_BUTTON = new JButton();
ADD_BUTTON.setIcon(new ImageIcon("/Users/Dominik/Desktop/user_add.png"));
ADD_BUTTON.setBounds(315, 500, 60, 60);
background.add(ADD_BUTTON);
ADD_BUTTON.addActionListener(this);
ADD_BUTTON.setToolTipText("Add record to Data Base");


RAPORT_BUTTON = new JButton();
RAPORT_BUTTON.setIcon(new ImageIcon("/Users/Dominik/Desktop/raport.png"));
RAPORT_BUTTON.setBounds(392, 500, 60, 60);
background.add(RAPORT_BUTTON);
RAPORT_BUTTON.addActionListener(this);
RAPORT_BUTTON.setToolTipText("Generates raport");

EDIT_BUTTON = new JButton();
EDIT_BUTTON.setIcon(new ImageIcon("/Users/Dominik/Desktop/user_edit.png"));
EDIT_BUTTON.setBounds(467, 500, 60, 60);
background.add(EDIT_BUTTON);
EDIT_BUTTON.addActionListener(this);
EDIT_BUTTON.setToolTipText("Edit record from Data Base");

CLEAR_BUTTON = new JButton();
CLEAR_BUTTON.setIcon(new ImageIcon("/Users/Dominik/Desktop/delete.png"));
CLEAR_BUTTON.setBounds(544, 500, 60, 60);
background.add(CLEAR_BUTTON);
CLEAR_BUTTON.addActionListener(this);
CLEAR_BUTTON.setToolTipText("Clear all Data Base");

MESSAGE_BUTTON = new JButton();
MESSAGE_BUTTON.setIcon(new ImageIcon("/Users/Dominik/Desktop/message.png"));
MESSAGE_BUTTON.setBounds(619, 500, 60, 60);
background.add(MESSAGE_BUTTON);
MESSAGE_BUTTON.addActionListener(this);
MESSAGE_BUTTON.setToolTipText("Send message to another user");

DOWNLOAD_BUTTON = new JButton();
DOWNLOAD_BUTTON.setIcon(new ImageIcon("/Users/Dominik/Desktop/download.png"));
DOWNLOAD_BUTTON.setBounds(694, 500, 60, 60);
background.add(DOWNLOAD_BUTTON);
DOWNLOAD_BUTTON.addActionListener(this);
DOWNLOAD_BUTTON.setToolTipText("Download Data Base to a text file");


validate();
}


public static void main(String[] args) {
// TODO Auto-generated method stub
test window = new test();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);


}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

Object EVENT_SOURCE = e.getSource();
DATABASE = new DatabaseManagement();


if (EVENT_SOURCE == CLEAR_BUTTON) 
{
    System.out.println("siema");

}   
else if (EVENT_SOURCE == DOWNLOAD_BUTTON)
{
    dispose();
}
else if (EVENT_SOURCE == CONNECT_BUTTON) 
{
    DATABASE_FIELD.setText("");
      //**           TRZEBA ZROBIC SIATKE !!!                    **//
    DATABASE_FIELD.append("IMIE ");
    DATABASE_FIELD.append("NAZWISKO ");
    DATABASE_FIELD.append("PESEL  \n");
    DATABASE_FIELD.append(DATABASE.showDataBase());
}

    }

}
C1pher
  • 1,933
  • 6
  • 33
  • 52
user3196130
  • 33
  • 2
  • 5
  • *"add some grid in my JTextArea"* Use a `JTable` for that. As general advice, ask just one question per question and post an [MCVE](http://stackoverflow.com/help/mcve) that focuses on that single question. – Andrew Thompson Jan 15 '14 at 12:06

1 Answers1

2
  • you have to set proper LayoutManager in the case that you want to use JLabel as container (Grid or FlowLayout)

  • don't to use NullLayout

  • JTextArea isn't designated to be a container remove DATABASE_FIELD.setLayout(new GridLayout(3, 3));

  • use JTextArea (10, 15) as intial size instead of any sizing (then valid for JScrollPane too)

  • add JScrollPane with JTextArea to JFrames CENTER area or to change JLabels LayoutManager to BorderLayout, then to put JButtons to another separate JLabel (Grid or FlowLayout)


setTitle("Hospital Management");
setSize(900,600);

setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
  • to move those code lines to the end of constructor, then remove validate, as aside should be revalidate() and repaint(), becuase you didn't stop for Image repainting

  • call pack() instead of setSize

  • See Initial Thread

  • only 1quater of possible issues, just about most important things

mKorbel
  • 109,525
  • 20
  • 134
  • 319