I have a code in java.
package interfaces;
import javax.swing.JPanel;
public class TabbedPaneDemo extends JPanel {
public TabbedPaneDemo() {
JTabbedPane pane = new JTabbedPane();
JPanel dashboardPanel = new JPanel();
dashboardPanel.add(new JLabel("Dashboard"));
// Add Dashboard Tab
pane.addTab("Dashboard", dashboardPanel);
JPanel transactionPanel = new JPanel();
transactionPanel.add(new JLabel("Transactions"));
// Add Transactions Tab
pane.addTab("Transactions", transactionPanel);
JPanel accountPanel = new JPanel();
accountPanel.add(new JLabel("Account"));
// Add Account Tab
pane.addTab("Account", accountPanel);
this.setLayout(new BorderLayout());
this.setPreferredSize(new Dimension(400, 200));
this.add(pane, BorderLayout.CENTER);
}
public static void main(String[] args) {
JPanel panel = new TabbedPaneDemo();
panel.setOpaque(true);
// panel.setBounds(12, 12, 45, 98);
JFrame frame = new JFrame("JTabbedPane Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}}
and the output it's !
I want to change in this code to be like this.
I want to use an absolute layout (null).
I want to add menu, buttons and labels with these tabs ...
How can I do??
You should NOT use absolute layout. There is not reason to use absolute layout.
Instead you should use layout managers.
Maybe create a panel and add buttons to the panel. Then add the panel to the NORTH of the content pane. The tabbed pane would be added to the CENTER.
Read the Swing tutorial on Using Layout Managers.
Related
I try to program a GUI like this. When a button clicked every time a new button is created and placed at specific position but after adding some buttons in jscrollpane, scrollbar not activated, so I unable to see all created buttons.
My code is here:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test{
private JFrame frame;
private JPanel panel1,panel2;
private JScrollPane pane;
private JButton button;
int i = 1, y = 10;
public Test()
{
panel2 = new JPanel(null);
panel2.setBounds(0,0,280,300);
button = new JButton("Add Button");
button.setBounds(90,10,120,30);
pane = new JScrollPane();
pane.setBounds(10,50,280,300);
panel1 = new JPanel(null);
panel1.setPreferredSize(new Dimension(300,400));
panel1.setBackground(Color.WHITE);
frame = new JFrame("Test");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel1);
frame.pack();
panel1.add(pane);
panel1.add(button);
pane.add(panel2);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
panel2.add(new JButton("Button "+i)).setBounds(80,y,120,30);
i += 1;
y += 35;
}
});
}
public static void main(String[] args) {
new Test();
}
}
Don't use a null layout. Don't use setBounds().
The scrollbars will only appear automatically when the preferred size of the panel is greater that the size of the scroll pane.
It is the job of the layout manager to:
set the location of a component
set the size of a component
calculate the preferred size of the panel.
So the solution is to use the appropriate layout manager on your panel.
So for example you can use a BoxLayout:
//panel2 = new JPanel(null);
panel2 = new JPanel();
panel2.setLayout( new BoxLayout(panel2, BoxLayout.Y_AXIS) );
And then when you add components to a visible frame you need to revalidate() the panel to invoke the layout manager:
//panel2.add(new JButton("Button "+i)).setBounds(80,y,120,30);
panel2.add(new JButton("Button "+i));
panel2.revalidate();
There is no need for panel1. Just add the components to the frame:
//panel1.add(pane);
//panel1.add(button);
frame.add(button, BorderLayout.PAGE_START);
frame.add(pane, BorderLayout.CENTER);
But there are other issues:
pane = new JScrollPane();
You actually need to add the panel to the scroll pane. So the code should be:
pane = new JScrollPane(panel2);
Since a component can only have a single parent, you need to remove:
pane.add(panel2);
Since the panel2 has been added to the scroll pane.
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel1);
frame.pack();
The above logic is wrong.
You should only invoked pack() and setVisible( true ) AFTER all the component have been added to the frame.
So most of the code posted is wrong.
Start by reading the section from the Swing turtorial on Layout Managers. Download the working demo code and learn how to better structure your code. The modify the code for your specific example.
I have been working on this for hours. I honestly cannot figure it out. I have JTextArea's inside a JSplitPane which is inside a JPanel with a JButton and all that is put in my JFrame. I am using Layout managers. I have tried using pack(). I have tried using preferred sizes. Without the JPanel my button does not display in the proper location or switch buttons in other Tabs. With the JPanel it cuts off all my text, stops the scroll function(yes I have tried setting the TextAreas to always have horizontal and vertical scroll bars...does not solve the problem where text just stops wrapping for no apparent reason).
public static void main(String[] args) throws IOException
{
JFrame frame = new JFrame();
Deck blackjack = new Deck(Deck.TYPE[0]);
JTextArea textBlackjackUnshuffled = new JTextArea();
JTextArea textBlackjackShuffle = new JTextArea();
JButton shuffleButtonBlackjack = new JButton(new ImageIcon(ImageIO.read(new File("res/shuffle.png"))));
JToolBar toolBarBlackjack = new JToolBar("Blackjack");
JSplitPane splitPaneBlackjack = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
JPanel panel = new JPanel();
JTabbedPane tabbedPaneBlackJack = new JTabbedPane();
JTabbedPane tabbedPaneCanasta = new JTabbedPane();
JTabbedPane tabbedPanePinochle = new JTabbedPane();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
textBlackjackUnshuffled.setColumns(10);
textBlackjackUnshuffled.setLineWrap(true);
textBlackjackUnshuffled.setWrapStyleWord(true);
textBlackjackUnshuffled.setEditable(false);
textBlackjackUnshuffled.setFont(new Font("DejaVu Sans", Font.PLAIN, 100));
textBlackjackUnshuffled.append(blackjack.toString());
textBlackjackShuffle.setColumns(10);
textBlackjackShuffle.setLineWrap(true);
textBlackjackShuffle.setWrapStyleWord(true);
textBlackjackShuffle.setEditable(false);
textBlackjackShuffle.setFont(new Font("DejaVu Sans", Font.PLAIN, 100));
textBlackjackShuffle.append(blackjack.toString());
shuffleButtonBlackjack.setBorderPainted(false);
shuffleButtonBlackjack.setFocusPainted(false);
shuffleButtonBlackjack.setContentAreaFilled(false);
splitPaneBlackjack.add(new JScrollPane(textBlackjackUnshuffled));
splitPaneBlackjack.add(new JScrollPane(textBlackjackShuffle));
panel.add(splitPaneBlackjack, BorderLayout.CENTER);
panel.add(shuffleButtonBlackjack, BorderLayout.PAGE_END);
tabbedPaneBlackJack.addTab("Blackjack", panel);
frame.add(tabbedPaneBlackJack);
frame.setSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize()));
frame.setVisible(true);
}
You're adding the JScrollPanes to the panel in BorderLayout positions, but have not set the layout manager of panel to BorderLayout. In this situation, panel will be using JPanel's default layout manager, FlowLayout, a manager which is not smart enough to respect the scroll pane's preferred sizes.
Your code needs:
panel.setLayout(new BorderLayout());
As you see the code, I would like to implement the 2nd tab with a text area, scrolling, and a button down there. This (JScrollPane scr = new JScrollPane(secondTab(panel2));) code was in the function, private static JTextArea secondTab(JPanel panel) before but I took it out of the function and put that back to MainFrame. Because the scroll and text area didn't show up. Now that I moved the code to mainframe the tex tarea and scroll are visible, but I'm struggling with making the button showing up in the 2nd tab. Do you guys have any idea?
public MainFrame(String username)
{
JTabbedPane tab = new JTabbedPane();
mainFrame.add(tab, BorderLayout.CENTER);
JPanel panel1 = new JPanel();
firstTab(panel1);
tab.add("babababa", panel1);
JPanel panel2 = new JPanel();
JScrollPane scr = new JScrollPane(secondTab(panel2));
JButton saveButton = new JButton("Save");
panel2.add(saveButton);
saveButton.setBounds(190, 280, 80, 40);
tab.add("hahaha", panel2.add(scr));
mainFrame.setBounds(200,200,500,400);
mainFrame.setVisible(true);
}
private static JTextArea secondTab(JPanel panel) {
panel.setLayout(null);
final JTextArea nameTextArea=new JTextArea();
nameTextArea.setBounds(10,10,440,270);
nameTextArea.setLineWrap(true);
return nameTextArea;
}
}
You should avoid use of null layout as this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. Not only that, JScrollPanes do not work well when the viewport view (the component that you display inside of the JScrollPane) uses null layout. Not only that, if you set the bounds or even the preferred size of a JTextArea, it will not expand inside the JScrollPane and you won't see scrollbars.
Solution:
Avoid using null layouts like the plague.
Learn and use the layout managers: The Swing Layout Manager Tutorials
Never set a JTextArea's size or preferredSize. Instead consider setting its columns and rows.
For example:
import java.awt.BorderLayout;
import javax.swing.*;
public class MainFrame2 extends JPanel {
private JTabbedPane tabbedPane = new JTabbedPane();
private JTextArea textArea = new JTextArea(20, 40);
public MainFrame2() {
tabbedPane.add("Bahahahaha", new JPanel());
tabbedPane.add("TextArea Info", createTextAreaPane());
setLayout(new BorderLayout());
add(tabbedPane, BorderLayout.CENTER);
}
private JComponent createTextAreaPane() {
JPanel btnPanel = new JPanel();
btnPanel.add(new JButton("Save"));
JPanel textAreaPane = new JPanel(new BorderLayout());
textAreaPane.add(new JScrollPane(textArea), BorderLayout.CENTER);
textAreaPane.add(btnPanel, BorderLayout.SOUTH);
return textAreaPane;
}
private static void createAndShowGui() {
MainFrame2 mainPanel = new MainFrame2();
JFrame frame = new JFrame("Main Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
It's better to build each tab panel in each method.
private JPanel buildFirstTab() {
// create new panel
// add components to the panel
// return the panel
}
private JPanel buildSecondTab() {
// create new panel
// create JScrollPane
// create JTextArea
// add text area to scrollpane by calling scrollpane.setViewPort method
// add scrollpane to the panel
// create button
// add button to the panel
// return the panel
}
public MainFrame(String username) {
JTabbedPane tab = new JTabbedPane();
mainFrame.add(tab, BorderLayout.CENTER);
tab.add("1st tab", buildFirstTab());
tab.add("2nd tab", buildSecondTab());
mainFrame.setBounds(200,200,500,400);
mainFrame.setVisible(true);
}
Adding to the previous answer, try to use the simple layout like BoxLayout (adds components on top of each other), or FlowLayout (adds components next to previous component), then learn more complex layout like GridBagLayout (so far, this is the best layout for me, most of the time).
Write a program that displays two buttons labeled “Green” and “Orange”.
If the user clicks on the green button, the background of the window changes to green. If the user clicks on the orange button, the background of the window changes to Orange.
Create a JFrame for this GUI. The GUI employs the default layout manager. A JPanel is needed.
Place the two buttons inside the panel and add the panel to the south region of the border layout.
Notice the text in the title bar. The green button should have white text and a green background. The orange button should have black text with an orange background.
Below is what I have so far, it doesn't seem to work.
public class LabAssign91 extends JFrame implements ActionListener{
private JPanel loc1Panel;
private JButton greenButton, orangeButton;
public LabAssign91()
{
super("Colored Buttons");
setLayout(new GridLayout(2, 2));
setSize(300,250);
setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(loc1Panel);
loc1Panel = new JPanel();
add(loc1Panel, BorderLayout.SOUTH);
greenButton = new JButton("Green");
greenButton.addActionListener(this);
loc1Panel.add(greenButton, BorderLayout.WEST);
greenButton.setBackground(Color.green);;
orangeButton = new JButton("Orange");
orangeButton.addActionListener(this);
loc1Panel.add(orangeButton, BorderLayout.EAST);
orangeButton.setBackground(Color.orange);
}
public static void main(String[] args) {
LabAssign91 app = new LabAssign91();
}
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
I have used BorderLayout for the JFrame and FlowLayout for the ButtonPanel. ButtonPanel is the bottom panel of the frame.
frame = new JFrame();
frame.setLayout(new BorderLayout());
topPanel = new JPanel();
topPanel.add(new JLabel("Top Panel"));
middlepanel = new JPanel();
middlepanel.add(new JLabel("Middle Panel"));
bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
bottomPanel.add(new JButton("Orange"));
bottomPanel.add(new JButton("Green"));
frame.add(topPanel, BorderLayout.NORTH);
frame.add(middlepanel, BorderLayout.CENTER);
frame.add(bottomPanel, BorderLayout.SOUTH);
The default layout for a JFrame is BorderLayout which has a SOUTH constraint. So there is no need for this statement.
//setLayout(new GridLayout(2, 2));
The default layout for a JPanel is a FlowLayout. So the following statements do nothing:
loc1Panel.add(greenButton, BorderLayout.WEST);
loc1Panel.add(orangeButton, BorderLayout.EAST);
Read the section from the Swing tutorial on Using Layout Managers. There is a section on using a BorderLayout and on using a FlowLayout. I don't know if you are supposed to use just panels with a BorderLayout or panels with a combination of BorderLayout and FlowLayout. I'll let you fix the code to meet your requirement.
I'm still trying to learn how layout managers work. I made a Frame with two JPanels.
The first one contains a textArea with a boxLayout.
The second one contains a flow layout with a button.
I set the preferredSize of each panel accordingly, packed them, but got unexpected results.
import java.awt.*;
import javax.swing.*;
public class LayoutMgrTest
{
public static void main(String[] args)
{
TableBasic frame = new TableBasic();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.setVisible(true);
frame.getContentPane().setLayout(new GridLayout(2,1));
JPanel controlPane = new JPanel();
JPanel buttonPane = new JPanel();
controlPane.setLayout(new BoxLayout(controlPane, BoxLayout.PAGE_AXIS));
controlPane.setPreferredSize(new Dimension(200, 200));
controlPane.add(new JScrollPane(new JTextArea()));
buttonPane.setLayout(new FlowLayout(FlowLayout.LEFT));
buttonPane.setPreferredSize(new Dimension(100,20));
buttonPane.add(new JButton("Button1"));
buttonPane.add(new JButton("Button2"));
frame.getContentPane().add(controlPane, BorderLayout.NORTH);
frame.getContentPane().add(buttonPane, BorderLayout.SOUTH);
frame.setSize(new Dimension(500,500));
frame.pack();
}
}
Whatever I do, if I use a grid Layout, it seems to always allocate half of the available space to each control. I have been told that:
The height of each row is dependent on the height of each component
added in each row.
The buttonpane's height is 20. It's allocating much more than that to it:
What's wrong with this code?
I would like to leave the two JPanels intact please. It's easy to simply add the textbox and the buttons directly to the frame, but I need to do it with JPanels (because I will be adding borders and other things).
That's the result of using GridLayout as layout manager. Change it to BorderLayout:
frame.getContentPane().setLayout(new BorderLayout());
For example, this code (I changed a little as possible from the original):
import java.awt.*;
import javax.swing.*;
public class LayoutMgrTest
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
//frame.setVisible(true);
//frame.getContentPane().setLayout(new BorderLayout());
JPanel controlPane = new JPanel();
JPanel buttonPane = new JPanel();
controlPane.setLayout(new BoxLayout(controlPane, BoxLayout.PAGE_AXIS));
controlPane.setPreferredSize(new Dimension(200, 200));
controlPane.add(new JScrollPane(new JTextArea()));
buttonPane.setLayout(new FlowLayout(FlowLayout.LEFT));
buttonPane.setPreferredSize(new Dimension(100,40));
buttonPane.add(new JButton("Button1"));
buttonPane.add(new JButton("Button2"));
frame.add(controlPane, BorderLayout.NORTH);
frame.add(buttonPane, BorderLayout.SOUTH);
//frame.setSize(new Dimension(500,500));
frame.pack();
frame.setVisible(true);
}
}
Generates this frame:
I set the preferredSize of each panel accordingly,
That is another problem. You should NOT set the preferred size. That is the job of the layout manager. Just add your components to the panels and let the layout manager do its job.
Most compnents have a default preferred size. For some you need to give it a little tip.
For example when using a text area you would give a "suggested" preferred size by using:
JTextArea textArea = new JTextArea(rows, columns);
If you use LayoutManager, you should not set a size on a component except the frame.
the size for the components is calculated from the different layout managers.
you find more infos at http://download.oracle.com/javase/tutorial/uiswing/layout/howLayoutWorks.html
in your code, you can add the panel with the textarea to BorderLayout.CENTER. this should solve your problem. the component in BorderLayout.CENTER takes the whole space, except the space needed for the components in NORTH, EAST, SOUTH and WEST.