Nie widać JButtonów na JPanelu.

0

Dodałem na swoim JPanlu JTable oraz 3 JButtony. Jednak po uruchomieniu programu tylko JTable jest widoczny, 3 buttony znajdujące się pod nim nie są widoczne od razu, tylko dopiero kiedy najadę na miejsca, w których powinny się znajdować to się pojawiają. Dlaczego tak się dzieje? Co zrobić żeby były od razu widoczne? JTable od razu wyświetla poprawnie.

Kod:

public class UsersJPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private List<User> usersList;

	/**
	 * Create the panel.
	 */
	public UsersJPanel(ClientJFrame cf) {

		setLayout(null);
		setBounds(200, 150, 694, 472);
		usersList = cf.getRec().usersList();

		String[] kolumny = { "Id", "Login", "Haslo", "Rola" };
		JTable jTable = new JTable();
		DefaultTableModel model = (DefaultTableModel) jTable.getModel();
		model.setColumnIdentifiers(kolumny);

		int ile = model.getRowCount();
		for (int i = ile - 1; i >= 0; i--) {
			model.removeRow(i);
		}
		for (User u : usersList) {
			Object[] objects = new Object[4];
			objects[0] = u.getId();
			objects[1] = u.getUsername();
			objects[2] = u.getPassword();
			objects[3] = u.getRole();
			model.addRow(objects);
			model.fireTableDataChanged();
		}
		JScrollPane scrollPane = new JScrollPane(jTable);
		scrollPane.setSize(300, 297);
		scrollPane.setLocation(202, 35);
		add(scrollPane);
		
		JButton btnNewButton = new JButton("Dodaj");
		btnNewButton.setBounds(202, 360, 89, 23);
		add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("Edytuj");
		btnNewButton_1.setBounds(308, 360, 89, 23);
		add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("Usun");
		btnNewButton_2.setBounds(414, 360, 89, 23);
		add(btnNewButton_2);
	}
}
 
0

Błąd jest raczej w tym kodzie, którego nie zamieściłeś - umieszczenie panelu w obiekcie JFrame.
Btw, co Cię natchnęło do wyłączenia LayoutManagera i ręcznego rozmieszczania komponentów?

0

Mam główny JFrame o nazwie ClientJFrame, po lewej JPanel z przyciskami, jeśli wcisnę przycisk "Użytkownicy" to po prawej zmienia się drugi JPanel i dodaje się tam właśnie ten powyższy UsersJPanl.
Tutaj umieszczam:

public class AdminJPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	/**
	 * Create the panel.
	 */
	public AdminJPanel(final ClientJFrame cf) {

		JButton btnUser = new JButton("Użytkownicy");
		btnUser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JPanel usersJPanel = new UsersJPanel(cf);
				cf.add(usersJPanel);
				usersJPanel.setVisible(true);
			}
		});
		btnUser.setBounds(40, 50, 120, 40);
		add(btnUser);

		JButton btnClient = new JButton("Klienci");
		btnClient.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JPanel clientsJPanel = new ClientsJPanel();
				cf.add(clientsJPanel);
				clientsJPanel.setVisible(true);
				clientsJPanel.setBounds(200, 150, 694, 472);
			}
		});
		btnClient.setBounds(40, 120, 120, 40);
		add(btnClient);
1

Jaki Layout ma główny JFrame?
Po dodaniu panelu do wyświetlonego już okna

cf.add(usersJPanel);

trzeba wywołać validate() i repaint().

1 użytkowników online, w tym zalogowanych: 0, gości: 1