Java Swing: Add tabs into JPanels - java

I have a series of tabs, but I want to then add further tabs within the panel it displays, as if to replicate a ribbon menu. I roughly know what needs to be done (changing the return type of the makeTextPanel function) but don't know how to so any help would be appreciated.
package components;
import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
public class ribbonMenu extends JPanel {
public ribbonMenu() {
super(new GridLayout(1,1));
JTabbedPane tabbedPane = new JTabbedPane();
JTabbedPane tabbedPane2 = new JTabbedPane();
JComponent panel1 = makeTextPanel("Panel #1");
tabbedPane.addTab("Tab 1", panel1);
panel1.setPreferredSize(new Dimension(600, 400));
JComponent panel2 = makeTextPanel("Panel #2");
tabbedPane.addTab("Tab 2", panel2);
JComponent panel3 = makeTextPanel("Panel #3");
tabbedPane.addTab("Tab 3", panel3);
add(tabbedPane);
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
protected JComponent makeTextPanel (String text) {
JPanel panel = new JPanel(false);
JLabel filler = new JLabel(text);
filler.setHorizontalAlignment(JLabel.CENTER);
panel.setLayout (new GridLayout(1,1));
panel.add(filler);
return panel;
}
private static void createAndShowGUI() {
JFrame frame = new JFrame ("ribbonMenu");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ribbonMenu(), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
UIManager.put("swing.boldmetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
Thanks

Why You need to change the return type?
Instead of this
JComponent panel1 = makeTextPanel("Panel #1");
use
JPanel panel1 = (JPanel)makeTextPanel("Panel #1");
And for further tabs You can again add JTabbedPane in that panel

Related

JButton not resizing with setMaximumSize()?

I have been trying to learn Buttons but it refuses to resize. Button1 (code below) just takes up the whole screen. I have seen other posts who's problem was that they didn't use
setMaximumSize();
but I'm using it and it still isn't working! I didn't make a JPanel yet. Here is my JFrame:
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Frame extends JFrame {
private JButton button1;
private JButton button2;
public Frame()
{
button1 = new JButton("Hello button1");
button2 = new JButton("Hello button2");
button2.setMaximumSize(new Dimension(100,100));
button1.setMaximumSize(new Dimension(100,100));
add(button2);
add(button1);
}
}
My main class is plain and simple:
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Panel extends JPanel{
public static void main(String args [])
{
Frame frame = new Frame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
It's because the frame('s contentPane) has a BorderLayout by default. You add the buttons to BorderLayout.CENTER, so the layout manager ignores the minimum-, preferred- and maximumSize.
I just want them to come up small and side by side
For that you could use a simple FlowLayout. (And if you want them to be centered on the frame, a parent JPanel with a GridBagLayout)
If you want a custom width & height for the buttons, override their getPreferredSize method. Overriding this method is safer than calling setPreferredSize.
Example:
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Example {
public Example() {
JButton button1 = new JButton("Hello button1");
JButton button2 = new JButton("Hello button2") {
#Override
public Dimension getPreferredSize() {
int width = super.getPreferredSize().width;
return new Dimension(width, width);
}
};;
JPanel buttonPanel = new JPanel();
buttonPanel.add(button1);
buttonPanel.add(button2);
JPanel contentPanel = new JPanel(new GridBagLayout());
contentPanel.add(buttonPanel);
JFrame frame = new JFrame();
frame.setContentPane(contentPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Example();
}
});
}
}
I added a flowlayout and changed setMaximumSize to setPreferredSize. That should fix your problem.
Here try this:
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Frame extends JFrame {
private JButton button1;
private JButton button2;
public Frame()
{
button1 = new JButton("Hello button1");
button2 = new JButton("Hello button2");
button2.setPreferredSize(new Dimension(100,100));
button1.setPreferredSize(new Dimension(100,100));
add(button2);
add(button1);
}
}
<----now the other class--->
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Panel extends JPanel{
public static void main(String args [])
{
Frame frame = new Frame();
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
}

Components in JTabbedPane not showing up

I'm using a tabbed pane and can't get the tab to show the GUI that I want. I plan to have different Panel objects for each different tab so that I can setup their layouts with more versatility. Right now I don't have any listeners or functions, and am strictly trying to get the components to show up.
Edit: Code is now in the question, not a link.
Here is the code for the UI for the "General":
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
public class GeneralGUI extends JPanel{
public GeneralGUI() {
JPanel topPanel = new JPanel();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JLabel subjectNum = new JLabel("Subject Street #:");
JLabel subjectStreet = new JLabel("Subject Street Name:");
JTable compTable = new JTable();
JTable subjectTable = new JTable();
JButton getRPT = new JButton("Get RPT file");
JButton getOrder = new JButton("Get Order/Contract");
JButton subjectDocs = new JButton("Get Subject Docs");
JButton compDocs = new JButton("Get Comp Docs");
panel1.add(subjectNum);
panel1.add(subjectStreet);
panel1.add(compTable);
panel2.add(getRPT);
panel2.add(getOrder);
panel2.add(subjectDocs);
panel2.add(compDocs);
panel2.add(subjectTable);
topPanel.add(panel1);
topPanel.add(panel2);
topPanel.setVisible(true);
}
}
Here is the code for the tabbed pane code:
import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
public class AppraisalTabs extends JPanel {
public AppraisalTabs() {
super(new GridLayout(1, 1));
JTabbedPane tabbedPane = new JTabbedPane();
GeneralGUI genGUI = new GeneralGUI();
// JComponent panel1 = makeTextPanel("General");
tabbedPane.addTab("General", genGUI);
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
JComponent panel2 = makeTextPanel("Docs");
tabbedPane.addTab("Docs", panel2);
tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
JComponent panel3 = makeTextPanel("Subject");
tabbedPane.addTab("Subject", panel3);
tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);
JComponent panel4 = makeTextPanel("Comps");
panel4.setPreferredSize(new Dimension(410, 300));
tabbedPane.addTab("Comps", panel4);
tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);
JComponent panel5 = makeTextPanel("Report");
panel4.setPreferredSize(new Dimension(800, 800));
tabbedPane.addTab("Report", panel5);
tabbedPane.setMnemonicAt(4, KeyEvent.VK_5);
//Add the tabbed pane to this panel.
add(tabbedPane);
//The following line enables to use scrolling tabs.
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
protected JComponent makeTextPanel(String text) {
JPanel panel = new JPanel(false);
JLabel filler = new JLabel(text);
filler.setHorizontalAlignment(JLabel.CENTER);
panel.setLayout(new GridLayout(1, 1));
panel.add(filler);
return panel;
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Appraisal Helper");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add content to the window.
frame.add(new AppraisalTabs(), BorderLayout.CENTER);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
My problem is that once I run the code the tabbed pane shows up, as well as the correctly-titled tabs, but the "General" tab isn't showing anything at all. I tried to setup the buttons and everything in it but it's still blank.
Any ideas?

JPanel's border make JLabel disappear

There is a frame. There a panel in that frame with BoxLayout. In that panel there is a ScrollPane. There is another panel in ScrollPane with SpringLayout. There is a label in that inner panel.
Here is a code:
package test;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SpringLayout;
import javax.swing.SwingUtilities;
class MainPanel extends JPanel {
MainPanel() {
JPanel panel = new JPanel();
BoxLayout boxLayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
panel.setLayout(boxLayout);
panel.setBorder(BorderFactory.createLineBorder(Color.black));
JPanel innerPanel = new JPanel();
SpringLayout springLayout = new SpringLayout();
innerPanel.setLayout(springLayout);
JLabel label = new JLabel("test");
innerPanel.add(label);
springLayout.putConstraint(SpringLayout.WEST, label, 5, SpringLayout.WEST, innerPanel);
springLayout.putConstraint(SpringLayout.NORTH, label, 5, SpringLayout.NORTH, innerPanel);
//innerPanel.setBorder(BorderFactory.createLineBorder(Color.black));
innerPanel.setPreferredSize(new Dimension(300, 100));
panel.add(innerPanel);
JScrollPane scrollPane = new JScrollPane(panel);
this.add(scrollPane);
}
}
class MainFrame extends JFrame {
MainFrame() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new MainPanel());
this.pack();
this.setVisible(true);
}
}
public class Test {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new MainFrame();
}
});
}
}
Label "test" is visible as expected. But when I add a border to inner panel (uncomment the line, see below) the label is disappeare.
//innerPanel.setBorder(BorderFactory.createLineBorder(Color.black));
Can someone explane why?
Try to change
innerPanel.setBorder(BorderFactory.createLineBorder(Color.black));
to
panel.setBorder(BorderFactory.createLineBorder(Color.black));
I hope that's what you meant to do & it fixed your problem :)

Java - Add two tabs into JPanel

I have problem with tabs in the JPanel. I know how to make new tabs in Mainframe, but I don't know how to make tabs into JPanel which is located in Mainframe.
Here are the pictures:
I have program looking like this -
http://www.bildites.lv/viewer.php?file=vklfhvfdfpwpcxllfqv.png
But I want to make it look like this -
http://www.bildites.lv/viewer.php?file=bvbrp4qfx2krn9bkx30j.png
And Here is my code of the blue JPanel:
package gui;
import java.awt.Color;
import javax.swing.JPanel;
public class CallsPanel extends JPanel {
private MainFrame frame;
Color color = new Color(99, 184, 255); // steelblue
public CallsPanel(MainFrame frame) {
this.frame = frame;
this.setLocation(0, 0);
this.setSize(300, 380);
this.setLayout(null);
this.setBackground(color);
this.initContent();
}
// -------------------------------------------------------------------------
// Declare New Things
private void initContent() {
// Add New Things
}
// -------------------------------------------------------------------------
}
Thanks a lot to people that will help!
JTabbedPane tabPane = new JTabbedPane();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JLabel label1 = new JLabel("Tab 1");
JLabel label2 = new JLabel("Tab 2");
panel1.add(label1);
panel2.add(label2);
tabPane.add("Tab 1", panel1);
tabPane.add("Tab 2", panel2);
this.add(tabPane);
Play around with the size/color/shape of the tabPane and see what works for you. But this is the basic of a tabPane.
See this simple runnable example
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class MyPanel extends JPanel {
JButton button = new JButton("Button");
JTabbedPane tabPane = new JTabbedPane();
public MyPanel(){
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
tabPane.add("Panel 1", panel1);
tabPane.add("Panel 2", panel2);
tabPane.setBorder(new EmptyBorder(10, 10, 10, 10));
setLayout(new BorderLayout());
add(tabPane, BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new MyPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setSize(300, 300);
frame.setVisible(true);
}
});
}
}

How To Change the JPanel in a JFrame at Runtime

I want to know how to change the content of a JFrame at runtime. Like adding a new JPanel and removing the old JPanel.
You can consider using CardLayout to change the active panel in a frame.
Changing JPanel at runtime here is the Code :
package stack;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class RemoveAndAddPanel implements ActionListener{
JFrame frame;
JPanel firstPanel;
JPanel secondPanel;
JPanel controlPanel;
JButton nextButton;
JPanel panelContainer;
JButton preButton;
JPanel contentPane;
public RemoveAndAddPanel() {
JFrame.setDefaultLookAndFeelDecorated(true);
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
firstPanel = new JPanel();
firstPanel.add(new JLabel("FirstPanel"));
firstPanel.setPreferredSize(new Dimension(100,100));
secondPanel = new JPanel();
secondPanel.add(new JLabel("Second panel"));
secondPanel.setPreferredSize(new Dimension(100,100));
panelContainer = new JPanel();
contentPane = new JPanel(new BorderLayout());
nextButton = new JButton("Next panel");
preButton = new JButton("PreButton");
controlPanel = new JPanel();
nextButton.addActionListener(this);
preButton.addActionListener(this);
preButton.setEnabled(false);
controlPanel.add(preButton);
controlPanel.add(nextButton);
panelContainer.setLayout(new BorderLayout());
panelContainer.add(firstPanel,BorderLayout.CENTER);
contentPane.add(controlPanel, BorderLayout.SOUTH);
contentPane.add(panelContainer,BorderLayout.CENTER);
frame.setContentPane(contentPane);
frame.setVisible(true);
frame.setSize(300,100);
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == nextButton) {
panelContainer.removeAll();
panelContainer.setSize(0,0);
panelContainer.setSize(secondPanel.getSize());
panelContainer.add(secondPanel,BorderLayout.CENTER);
panelContainer.revalidate();
nextButton.setEnabled(false);
preButton.setEnabled(true);
}
if (e.getSource() == preButton) {
panelContainer.removeAll();
panelContainer.setSize(0,0);
panelContainer.setSize(firstPanel.getSize());
panelContainer.add(firstPanel,BorderLayout.CENTER);
nextButton.setEnabled(true);
preButton.setEnabled(false);
}
}
public static void main(String args[]) {
new RemoveAndAddPanel();
}
}
JFrame.setContentPane()

Categories

Resources