harry-potter
harry-potter

Reputation: 2049

Put a JTextField search bar in JScrollPane

I have to create a search textField object above a JTable. This JTable is inside a JScrollPane: enter image description here

To reach my target I implement a Jpanel where I add the JTextField and the Jtable:

JTable table = new JTable();
JTextField search = new JTextField();
JPanel panel = new JPanel();
panel.add(search, BorderLayout.NORTH);
panel.add(table, BorderLayout.CENTER);
this.setViewportView(panel);

In this way I get the following result:

enter image description here

Upvotes: 1

Views: 257

Answers (1)

rdonuk
rdonuk

Reputation: 3956

Default layout for JPanel is FlowLayout. So you must set your panel's layout to BorderLayaout.

Change this

JPanel panel = new JPanel();

with

JPanel panel = new JPanel(new BorderLayout());

Upvotes: 2

Related Questions