i have a Jtable and now i want to have a JLabel and a JTextField and a JButton in one row
above this Jtable.
How do i do that?
until now i had this:
final JTableHeader header = table.getTableHeader();
setLayout(new BorderLayout());
add(header, BorderLayout.PAGE_START);
add(table, BorderLayout.CENTER);
now i added this above:
ImageIcon leftButtonIcon = createImageIcon("images/add.gif");
JButton addButton = new JButton(leftButtonIcon);
JLabel labelF = new JLabel("Filter:");
labelF.setAlignmentX(Component.RIGHT_ALIGNMENT);
JTextField eingabeF = new JTextField();
eingabeF.setSize(50, 10);
but how to do, that there ist this: "Filter: TEXTFIELD BUTTON"
and under this there is the Table ?
Add the labelF label, eingabeF text field and addButton button to an instance of JPanel with FLowLayout. And add it to the content pane(which has BorderLayout) with constraint BorderLayout.PAGE_START. JPanel has FlowLayout as it's default layout
Enclose your table to an instance of JScrollPane and Then add the scroll pane to the content pane with constraint BorderLayout.CENTER. The scroll pane automatically places the table header at the top of the viewport.
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
Related
I have a frame f, panel Fpanel. and textarea j.
This is a part of my code.
The scroll does not seem to be working on my text area.
JTextArea j=new JTextArea();
j.setBounds(60,150, 400,400);
j.setMargin(new Insets(3,3,3,3));
j.setEditable ( false ); // set textArea non-editable
JScrollPane scroll = new JScrollPane(j);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
I have added
Fpanel.add(scroll);
and
f.add(Fpanel);
to my code as well but it does not seem to be scrolling.
Am I missing a piece of the code or have I written something incorrectly?
if you want to have a vertical scroll bar:
JPanel panel = new JPanel();
JScrollPane jsp = new JScrollPane(panel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
if you want to have a horizontal scroll bar:
JPanel panel = new JPanel();
JScrollPane jsp = new JScrollPane(panel, ScrollPaneConstants.HORIZONTOL_SCROLLBAR_ALWAYS, ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
I'm trying to build a Java GUI where among other things there will be several tabs.
In one of the tabs I want to have both a scrollable JTextArea, and also some buttons at the top/bottom that interacts with the JTextArea. I can't figure out how to get both into the same tab, I'm either getting the buttons and a non-scrollable jtextarea, or just the scrollable jtextarea. I also don't want to display the button in the other tabs. Here is my code:
private final JTextArea music = new JTextArea();
private final JTextArea button = new JTextArea();
private final JTextArea test = new JTextArea();
private final JTabbedPane tab = new JTabbedPane();
private JTable table;
music.append(newPlaylist.toString());
JFrame frame = new JFrame("GUI");
frame.setTitle("Music File Organiser");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton button1 = new JButton("Hello");
JButton button2 = new JButton("Sort by Album Title");
JButton button3 = new JButton("Sort by Track Title");
button1.addActionListener(new ActionListener() {code};
button2.addActionListener(new ActionListener() {code};
button3.addActionListener(new ActionListener() {code};
panel.add(music);
panel.add(button1);
panel.add(button2);
panel.add(button3);
JScrollPane scroll = new JScrollPane(panel);
JScrollPane scroll2 = new JScrollPane(table);
tab.add("Music Files", scroll);
tab.add("Table", scroll2);
this.add(tab);
frame.setLocationRelativeTo(null);
this.setSize(1200, 1000);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
The tab in mind is the first one. How do I add buttons to "scroll"? The way I'm trying it here is to add the JTextArea "music" into panel, then adding the buttons to the same panel, then adding the panel to a JScrollPane, and then adding the JScrollPane to tab. Any help would really be appreciated.
Encapsulate your visual elements into child Panels.
JPanel buttonPanel = new JPanel(); //panel for buttons.
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
JScrollPane scroll = new JScrollPane(music); //scrollable pane for JTextArea
panel.add(buttonPanel);
panel.add(scroll); //add sub-components to panel for tab
/*here you would add some layout code to fit the panel and scroll into the associated spaces */
tab.add("Music Files", panel); //add panel to tab
You can do something like this:
JPanel tab = new JPanel()
JPanel tabWithButtons = new JPanel()
JPanel tabWithScrollPanel = new JPanel()
tab.add(tabWithScrollPanel)
tab.add(tabWithButtons)
IMO this is better way, because I assume that buttons shouldn't be included in JScrollPane and they should be visible all the time
Of course, you have to add all elements to proper JPanels.
Also, you can try with width/height settings of panel which contains all your elements, maybe ScrollArea panel is to big?
I am trying to add a scroll bar to a JTextArea. Would someone please tell me what I did wrong with the code below?
JFrame frame = new JFrame ("Test");
JTextArea textArea = new JTextArea ("Test");
JScrollPane scrollV = new JScrollPane (textArea);
JScrollPane scrollH = new JScrollPane (textArea);
scrollV.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollH.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.setVisible (true);
Thank you in advance.
EDIT: I fixed the code with the Adel Boutros' advice below.
//FRAME
JFrame frame = new JFrame ("Test");
frame.setSize(500,500);
frame.setResizable(false);
//
//TEXT AREA
JTextArea textArea = new JTextArea("TEST");
textArea.setSize(400,400);
textArea.setLineWrap(true);
textArea.setEditable(false);
textArea.setVisible(true);
JScrollPane scroll = new JScrollPane (textArea);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add(scroll);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
It doesn't work because you didn't attach the ScrollPane to the JFrame.
Also, you don't need 2 JScrollPanes:
JFrame frame = new JFrame ("Test");
JTextArea textArea = new JTextArea ("Test");
JScrollPane scroll = new JScrollPane (textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add(scroll);
frame.setVisible (true);
Open design view
Right click to textArea
open surround with option
select "...JScrollPane".
A scroll pane is a container which contains another component. You can't add your text area to two different scroll panes. The scroll pane takes care of the horizontal and vertical scroll bars.
And if you never add the scroll pane to the frame, it will never be visible.
Read the swing tutorial about scroll panes.
You don't need two JScrollPanes.
Example:
JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta);
// Add the scroll pane into the content pane
JFrame f = new JFrame();
f.getContentPane().add(sp);
I have a JTable that is placed inside a JPanel which is then placed in a JFrame that has another JPanel that has a ScrollPane. The general idea can be seen below if the explanation is confusing. I have set my JTable to auto resize the last column, but it never auto sizes. What is the problem?
JFrame -> JPanel -> JTable
-> JPanel -> Scroll Pane
My code:
this refers to my class which extends JFrame
this.setLayout(new BorderLayout());
JTextArea log = new JTextArea(20, 50);
log.setEditable(false);
JScrollPane logScrollPane = new JScrollPane(log);
String[] configRow = {"Config File", "Not Loaded"};
String[] logRow = {"Log File" , "Not Loaded"};
DefaultTableModel dtm = new DefaultTableModel();
dtm.addColumn("");
dtm.addColumn("");
dtm.addRow(configRow);
dtm.addRow(logRow);
JTable status = new JTable(dtm);
status.setTableHeader(null);
status.setShowGrid(false);
status.setEnabled(false);
status.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
JPanel tablePanel = new JPanel();
tablePanel.setLayout(new BorderLayout());
tablePanel.add(status, BorderLayout.LINE_START);
JPanel logPanel = new JPanel();
logPanel.add(logScrollPane);
this.add(tablePanel, BorderLayout.NORTH);
this.add(logPanel, BorderLayout.SOUTH);
Your JTable must be placed inside a JScrollPane in order for the resizing to work properly. You are placing the JTable directly on a JPanel.
tablePanel.add(new JScrollPane(status), BorderLayout.LINE_START):
I'd like to use a JScrollPane for a panel which has an arbitrary list of labels in it using box layout. I'm trying to get it so that the scrollbar would appear if there were too many items (labels) to display.
I tried adding a JScrollPane to the panel and then add the labels but then I don't see any scroll bar.
Any ideas?
TIA
For this kind of thing, you'd normally use a JList or JTable (if you need custom rendering).
Make sure that you call validate() or revalidate() on the JScrollPane after adding an item, to force the preferred size of the panel to be recalculated.
Here's how I did it.
JPanel midPanel = new JPanel();
midPanel.setLayout(new BoxLayout(midPanel, BoxLayout.Y_AXIS));
midPanel.add(new JLabel("<html><u>Label</u>"));
Box box = Box.createVerticalBox();
for (Item item : data.getInventory()) {
inventory.add(box.add(new JLabel(item.getName())));
}
JScrollPane jscrlpBox = new JScrollPane(box);
midPanel.add(jscrlpBox);
add(midPanel, BorderLayout.CENTER);
From:
http://www.java2s.com/Code/Java/Swing-JFC/JScrollPanetoholdscrollablecomponent.htm
Did you remember to set the preferred size of the content panel?
final JFrame frame = new JFrame("Scroll Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
final Box textArea = Box.createVerticalBox();
final JScrollPane textAreaScroll = new JScrollPane(textArea);
textAreaScroll.setPreferredSize(new Dimension(80,150)); /* essential! */
JButton addButton = new JButton("ADD");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.add(new JLabel("abc"));
textArea.revalidate();
}
});
frame.getContentPane().add(textAreaScroll, BorderLayout.SOUTH);
frame.getContentPane().add(Box.createRigidArea(new Dimension(10,10)), BorderLayout.CENTER);
frame.getContentPane().add(addButton, BorderLayout.NORTH);
frame.pack();
frame.setVisible(true);
In this example, the scroll bar works correctly, but if you remove the line marked as "essential", it will not work anymore.