I am using Swing and trying to create another tabbed pane for my GUI. I searched online and found examples, but when I try it out the new tabbed pane is not created. I pasted my tabbed pane code below, thank you
layout = new FormLayout("p,6dlu,p,3dlu,p,10dlu:grow", "p,3dlu,p,3dlu,p,3dlu,p,3dlu,p,10dlu,p,3dlu,p,3dlu,p,3dlu,p,3dlu");
formBuilder = new DefaultFormBuilder(layout);
cc = new CellConstraints();
formBuilder.setBorder(Borders.DLU4_BORDER);
tab = new JTabbedPane();
tab.add("New", formBuilder.getPanel());
How to make a tabbed pane:
Declare The tabs:
JTabbedPane tabs = new JTabbedPane();
Panel chat = new Panel();
Panel vrage = new Panel();
tabs.add("Chat", chat);
tabs.add("Questions", vrage);
Declare the top Panel
JPanel topPanel = new JPanel();
topPanel.add(tabs);
Add The top Panel to JFrame
trFrame.add(topPanel);
Related
I am trying to make a GUI for a game. I am very new to Java, especially GUI. The code below is a snippet which is supposed to make a JFrame with nested panels for organization. It works until I add buttons to the button panel. They end up on the boardBckg panel. If I manage to place them on the correct panel the JTextField disappears or it takes up the entire screen. I have been working on this part of the code for the past two days and I could really use GUI tips.
private void makeWindow()
{
boardPanel = new JPanel();
boardBckg = new JPanel();
menuPanel = new JPanel();
save = new JButton("Save");
save.setSize(Buttons);
load = new JButton("Load");
load.setSize(Buttons);
replay = new JButton ("Replay");
replay.setSize(Buttons);
words = new JTextField();
frame = new JFrame(title);
boardPanel.setSize(PANEL);
boardPanel.setMaximumSize(MAX);
boardPanel.setMinimumSize(MIN);
boardPanel.setLayout(new GridLayout(m,n));
boardBckg.setSize(1000, 1000);
boardBckg.setBackground(Color.cyan);
boardBckg.add(boardPanel, BorderLayout.CENTER);
frame.setSize(1500, 1000);
frame.setResizable(false);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BoxLayout vertical = new BoxLayout(menuPanel, BoxLayout.Y_AXIS);
menuPanel.setSize(500, 1000);
menuPanel.setBackground(Color.blue);
menuPanel.setLayout(vertical);
frame.add(boardBckg);
frame.add(menuPanel);
JPanel iGiveUp = new JPanel();
iGiveUp.setBackground(Color.black);
JPanel buttons = new JPanel();
buttons.setBackground(Color.darkGray);
buttons.add(save);
buttons.add(load);
buttons.add(replay);
menuPanel.add(iGiveUp);
menuPanel.add(buttons);
iGiveUp.add(words);
boardBckg.add(boardPanel, BorderLayout.CENTER);
The default layout of a JPanel is the FlowLayout. You can't just specify a BorderLayout constraint when you add the component to the panel.
frame.add(boardBckg);
frame.add(menuPanel);
The default layout for (the content pane of) the frame is a BorderLayout. If you don't specify a constraint, then the component is added to the BorderLayout.CENTER. Problem is only one component can be added to the CENTER so you only see the last comoponent added.
frame.setVisible(true);
Component should be added to the frame BEFORE the frame is packed and made visible. So the above statement should be the last statement in your constructor.
I have no ideas what your desired layout is but you need to start with something simple and take advantage of the default BorderLayout of the frame.
So your basic logic might be something like:
JPanel menuPanel = new JPanel()
menuPanel.setLayout(new BoxLayout(menuPanel, BoxLayout.Y_AXIS));
menuPanel.add(...);
menuPanel.add(...);
JPanel center = new JPanel();
center.setLayout(...);
center.setBackground( Color.BLUE );
center.add(...);
frame.add(menuPanel, BorderLayout.LINE_START);
frame.add(center, BorderLayout.CENTER);
frame.pack();
frame.setVisible( true );
The main point is to break the panels down logically and add them to the frame one at a time. So first get the menu and its child components added to the frame is the correct position. Then you can add the CENTER panel and its child components.
As looking for a solution, i found almost the same idea in all answers in StackOverflow. But all did not work, i need to help me for a solution for that special part of code.
//Create a JTabbedPane for 2 tabs
mainTabs = new JTabbedPane(JTabbedPane.TOP);
//Create the first tab
reportingTabs = new JTabbedPane(JTabbedPane.TOP);
// editor is an object created from a class inherited form JPanel
editor = new GraphEditor();
//Create a JMenuBar
EditorMenuBar menuBar = new EditorMenuBar(editor);
//Create a JFrame for the editor
editorFrame = editor.createFrame();
//Create a JPanel object to contain bothe the JMenubar and the editor JFrame
JPanel editorPanel = new JPanel(new BorderLayout());
//Here the solution, creating a JScrollPane to contain only the editor JFrame to be scrolled
JScrollPane editorScroll = new JScrollPane();
//Adding the JMenuBar and the editor JFrame to the JPanel
editorPanel.add(menuBar, BorderLayout.NORTH);
editorPanel.add(editorFrame.getContentPane());
//Involve the JPanel into the JScrollPane
editorScroll.add(editorPanel);
//Adding the tabs to the main JFrame
maingui.getContentPane().add(mainTabs);
//Adding the JScrolledPane to a tab
mainTabs.addTab("Editor", editorScroll);
The result, is that thers is no JFrame in maingui. (with no SCrollPane solution, it appreas correctly)
Remove editorScroll variable completely and replace
mainTabs.addTab("Editor", editorScroll);
with
mainTabs.addTab("Editor", new JScrollPane(editorPanel));
It should work...
This is my first time using java accessibility for a swing application. Here is a brief code for drawing the swing application. There are more code for the swing application, but i think that i just want to provide a outline of the layout. mainPanel has a title and a tree in the left side and a content panel in the right.
JPanel mainPanel = new JPanel();
// add title to the main panel
JList list = new JList();
// add list model which will be displayed in a tree.
JScrollPane scrollPane = new JScrollPane(list, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
mainPanel.add(scrollPane);
JPanel contentPanel = new JPanel();
// add components(radio buttons and some text fields) to the content panel
mainPanel.add(contentPanel);
JFrame mainFrame = new JFrame();
mainFrame.add(mainPanel);
When the swing application is launched, i hear a voice reading title from the main panel and some radio buttons in the content panel, but I don't hear anything from the tree in the left side. The tree contains information about the installation step, but it doesn't read anything.
I am not sure if it is related to the code or a layout or a panel focus issue.
It would be great if someone who has done accessibility for swing has had a similar issue provides any tips or help.
Try this hope it will be fine
Add another panel to mainpanel
JPanel listPanel=createPanel();
JPanel contentPanel = new JPanel();
// add components(radio buttons and some text fields) to the content panel
mainPanel.setLayout(new BorderLayout());
mainPanel.add(listPanel,BorderLayout.WEST);
mainPanel.add(contentPanel,BorderLayout.EAST);
JFrame mainFrame = new JFrame();
mainFrame.add(mainPanel);
Then create a function
private JPanel createPanel(){
JPanel listPanel=new JPanel();
JList list=new JList();
JScrollPane scrollPane = new JScrollPane(list,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
listPanel.add(scrollPane);
listPanel.setBorder(BorderFactory.createEmptyBorder());
return listPanel;
}
I am creating a tab pain in a Class called Dashboard which holds 'filler' panels in the tabs. I am wondering if there is a way to create a new Dashboard and change the panels that I stored in the tabs. I'm not sure if this is the right way to explain it, but here is some code.
public class Dashboard{
public Dashboard(){
tabPane = new JTabbedPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel1.add(new JLabel("This is the first panel"));
panel2.add(new JLabel("This is the second panel"));
panel3.add(new JLable("This is the third panel"));
tabPane.add("One", panel1);
tabPane.add("Two", panel2);
tabPane.add("Three", panel3);
}
I now want to make a new class that creates and instance of the Dashboard, but changes what panels show up in the tabs. I was trying something like this:
public class Changer{
public Changer(){
Dashboard d = new Dashboard();
// assuming I have getters and setters in the above class and that the
// panels are fields in Dashboard
JPanel new = new JPanel();
d.setPanel1(new);
}
}
I'm not sure if this is possible or if there is another way of doing so.
Get the index of tab
int index = tabPane.indexOfTab("One");
Set the component at the specified index
tabPane.setComponentAt(index, new Dashboard());
I'm new to Java and the entire swing development. I'm working on a Window with three tabs and adding each component to those tabs. I started by adding a textfield to the 3rd tab, but it's taking up the entire tab. I'm sure if I add other components it will make room, but isn't there a way to make it so it doesn't initially take up the entire Tabbed Pane?
package literature.windows;
import java.awt.*;
import javax.swing.*;
public class MainWindow extends JFrame {
JPanel storiesPanel = new JPanel();
JPanel plotPanel = new JPanel();
JPanel charactersPanel = new JPanel();
JTextField addCharacterTextField = new JTextField("Enter Character's Name", 25);
public MainWindow() {
setSize(800, 600);
JTabbedPane tabs = new JTabbedPane();
tabs.addTab("Stories", storiesPanel);
tabs.addTab("Plot", plotPanel);
tabs.addTab("Characters", charactersPanel);
add(tabs);
tabs.setTabComponentAt(2, addCharacterTextField);
setVisible(true);
}
}
You are currently setting the entire tab component for that tab. Instead you need to add the JTextField to the container/panel for that tab. Replace
tabs.setTabComponentAt(2, addCharacterTextField);
with
charactersPanel.add(addCharacterTextField);