I'm trying to make 3 tabs and in each tab, I have a text field and multiple button and check boxes and a big text area.
The problem is when I'm trying to use the GridBagLayout
or GridLayout
with JTabbedPane
at the same time, either it doesn't show anything or it brings all three tabs in one little box in the center. I don't know if we can use it both together?
But I would like to use some sort of layout to set the text filed on the top then in the middle of each tab use buttons and on the bottom of the tab have JTextArea
for stream output.
Here is my code:
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.io.PrintStream;
import javax.swing.JTabbedPane;
public class FrameFinder extends JFrame {
private static final int FRAME_HEIGHT = 600;
private static final int FRAME_WIDTH = 500;
private final Font LARGE_FONT = new Font(Font.SERIF, Font.BOLD, 34);
public static JTextField text;
private JPanel panel;
private JButton button;
private JLabel label;
private JMenuBar menubar;
private KeyListener newkey;
private JMenuItem exit;
private JMenuItem about;
private JTabbedPane jtap;
private JTextArea textArea;
static private final String newline = "\n";
// constructor
public FrameFinder(){
}
//creating constructor
public FrameFinder(String str){
createFrame(str);
CreateTabbedPane();
}
// click listener
private void addMenuListeners(){
exit.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
exitActionPerformed();
}
});
about.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
// aboutActionPerformed();
}
});
}
private void exitActionPerformed(){
dispose();
}
//creating frame
private void createFrame(String title) {
setTitle(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setVisible(true);
setResizable(false);
}
// here we create the tab bar
public void CreateTabbedPane() {
// super(String.valueOf(new BorderLayout()));
setTitle("Tabbed Pane");
//setLayout(new GridLayout());
jtap = new JTabbedPane();
getContentPane().add(jtap);
textArea = new JTextArea(50, 10);
textArea.setEditable(false);
// PrintStream printStream = new PrintStream(new CustomOutputStream(textArea));
//JPanel controls = new JPanel();
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.insets = new Insets(10, 10, 10, 10);
constraints.anchor = GridBagConstraints.WEST;
jp1.add(new JTextField("Find:", 20));
jp1.add(new JButton("Search"));
jp2.add(new JTextField("Directory path :", 20));
jp2.add(new JButton("Browser "));
jp1.add(new JCheckBox("match whole word."));
jp1.add(new JCheckBox("Match case."));
jp3.add(new JTextArea(50, 10));
jp2.add(new JTextField("Find in folders:", 20));
jp2.add(new JButton("Search"));
jp2.add(new JTextField("Directory path :", 20));
jp2.add(new JButton("Browser "));
jp2.add(new JCheckBox("match whole word."));
jp2.add(new JCheckBox("Match case."));
jp3.add(new JTextArea(50, 10));
jp3.add(new JTextField("Find in folders:", 20));
jp3.add(new JButton("Search"));
jp2.add(new JTextField("Directory path :", 20));
jp2.add(new JButton("Browser "));
jp3.add(new JCheckBox("match whole word."));
jp3.add(new JCheckBox("Match case."));
jp3.add(new JTextArea(50, 10));
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JLabel labe13 = new JLabel();
// label2.setText("You are in area of Tab2");
createFrame("Finder");
jp1.add(label1);
jp2.add(label2);
jp3.add(labe13);
jtap.addTab("Find ", jp1);
jtap.addTab("Replace All in one file", jp2);
jtap.addTab("Replace All in Multi files",jp3);
}
}
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
setLayout(new GridBagLayout());
The above code is setting the layout manager for the frame, which is not what you want to do.
And the default layout manager for a panel is a FlowLayout (since you don't specify the layout manager).
You want to set the layout manager for each panel:
JPanel jp1 = new JPanel( pick a layout manager);
JPanel jp2 = new JPanel( pick a layout manager);
JPanel jp3 = new JPanel( pick a layout manager);
//setLayout(new GridBagLayout());
So each panel you add to a tab can have a different layout manager to achieve your desired layout. And you can also nest panels with different layout managers to give you even more flexibility.
The layout of each tab is completely independent of the other tabs. The only relation is that all tabs share the same space so the preferred size of the tabbed pane will be the preferred size of the largest tab.
But I would like to use some sort of layout to set the text filed on the top then in the middle of each tab use buttons and on the bottom of the tab have Jtextarea for stream output. Any help will be appreciated. Here is my code:
So maybe you start with a BorderLayout for the main panel to add to the tab. Then you can create other panels as required to display the component in the PAGE_START, CENTER, PAGE_END of the BorderLayout. Read the Swing tutorial on Layout Manager for some examples to get you started.
Edit:
Following up on my suggestion from above you can do something like:
JPanel top = new JPanel( new GridLayout(0, 3) );
top.add( new JLabel("Label1") );
top.add( new JLabel("Label2") );
top.add( new JLabel("Label3") );
JTextArea textArea = new JTextArea(5, 30);
JPanel bottom = new JPanel( new GridLayout(0, 2) );
bottom.add( new JButton("Button1") );
bottom.add( new JButton("Button2") );
JPanel tab1 = new JPanel( new BorderLayout() );
tab1.add(top, BorderLayout.PAGE_START);
tab1.add(new JScrollPane(textArea), BorderLayout.CENTER);
tab1.add(bottom, BorderLayout.PAGE_END);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Tab1", tab1);
frame.add(tabbedPane);