Create scrollable JFrame using JScrollPane and JPanel - java

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...

Related

How to add container to a frame?

I am trying to make a minesweeper that has a different space for a smiley icon that we can click and the buttons which we have to click to play.
public final class testFrame extends JFrame implements MouseListener, ActionListener {
private JFrame screen = null;
private JPanel composite = new JPanel();
public testFrame() {
screen = new JFrame();
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen.setVisible(true);
screen.setResizable(true);
composite.setLayout(new BorderLayout());
//this button is not showing also
JButton button = new JButton("Text goes here");
composite.add(button);
Container cp = screen.getContentPane(); // JFrame's content-pane
cp.setLayout(new GridLayout(5, 5, 2, 2)); // in 10x10 GridLayout
//codes to add buttons
}
So here I am trying to add the container cp to the screen. But it opened
two screen
Sorry if this seems like minor things but I am really new to this java GUI so please help me.
EDIT:
I removed the extends JFrame and used the screen instead. It kinda works but I can't seem to put the container cp to a panel. The requirement is that I have to use container cp. So I cannot change. Thank you
public final class TestFrame implements MouseListener, ActionListener {
private JFrame screen = null;
private JPanel composite = new JPanel();
private JPanel topPanel = new JPanel();
public TestFrame() {
screen = new JFrame("TestFrame");
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen.setVisible(true);
topPanel.setLayout(new BorderLayout());
//composite.setLayout(new BorderLayout());
//button in topPanel
JButton button = new JButton("Text goes here");
topPanel.add(button, BorderLayout.PAGE_START);
//Content Pane
Container cp = screen.getContentPane();// JFrame's content-pane
cp.setLayout(new GridLayout(5, 5, 2, 2)); // in 10x10 GridLayout
//composite.add(cp, BorderLayout.CENTER);
screen.add(topPanel);
// screen.add(composite);
}
Now it looks like
this
There are a few things.
You are extending JFrame as well as using it as an attribute of the same class so you can ether use this instead of screen or you remove the extends JFrame as it is redundant and not needed
To answer your question. A JFrame contains a Panel called ContentPane this pane is were you add you panels to (you already did the layout thing right). So the solution is:
cp.add(composite);
You've got too many JFrames, the testFrame class which extends JFrame (and which should be re-named TestFrame to comply with Java naming conventions) and the screen variable. Use only one.
You can and should nest JPanels to achieve your desired result. For instance if you want a grid as well as some control buttons, create a JPanel, give it a BorderLayout, put your grid JPanel in the BorderLayout.CENTER postion and a JPanel with control JButtons in a different position, say BorderLayout.PAGE_START

Is there a way to move a jMenuBar to somewhere else than the top of my jFrame?

I'm creating an undecorated jFrame, and I wanted to add a jMenuBar to this jFrame to allow me to use drop-down menus for various things.
But, the jMenuBar always defaults to the very top of my jFrame, and it being undecorated, it pushes down a jPanel I had set there with the "Minimize" and "Close" buttons, and trying to move it anywhere else is unsuccessful.
I've tried using a jToolBar, and while it allows for more customization in terms of placement, I can't find a way to add drop-down menus onto the buttons there.
Is there a way to move a jMenuBar somewhere else than the top or to add a drop-down menu to a jToolBar?
You should have a look at Layout Managers.
Here is a simple example to achieve what you want, using a BorderLayout:
public static void main(String[] args) {
JFrame jf = new JFrame();
JPanel container = new JPanel();
container.setLayout(new BorderLayout());
JLabel label = new JLabel("North");
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("A south menu");
menu.add(new JMenuItem("A south item"));
menuBar.add(menu);
container.add(label, BorderLayout.NORTH);
container.add(menuBar, BorderLayout.SOUTH);
jf.add(container);
jf.setPreferredSize(new Dimension(200,100));
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
jf.pack();
}
result:

Create a main program with two tabs from two different JFrame classes [duplicate]

I Have an already given JFrame frame, that I want to show it (insert it) into a tab of JTabbedPane, but that was not possible explicitely like that:
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainTabs.addTab("Editor", null, frame, null);
And the error was:
java.lang.IllegalArgumentException: adding a window to a container
I tried some solutions like to insert the frame into a JPanel but also in vain. I have the idea to convert it into an InternalJFrame but I don't have any idea about that.
Is that any solution to insert that frame?
UPDATE:
I tried that soluion:
mainTabs.addTab("Editor", null, frame.getContentPane(), null);
But i lost the JMenuBar that I added.
You can't add JFrame(or another top-level component) to another component/container, but you can use getContentPane() method of frame, to get main panel of your frame and add that to JTabbedPane tab. Like next:
JTabbedPane tabs = new JTabbedPane();
JFrame frame = new JFrame();
frame.add(new JButton("button"));
tabs.addTab("1", frame.getContentPane());
Also you can change JFrame to JPanel and use that.
Read about JInternalFrame, top-level containers.
EDIT: getContentPane() doesn't return any decorations or JMenuBar, this components you need to add manually, like in next example with menu:
JTabbedPane tabs = new JTabbedPane();
JFrame frame = new JFrame();
JMenuBar bar = new JMenuBar();
bar.add(new JMenu("menu"));
frame.setJMenuBar(bar);
frame.add(new JButton("button"));
JPanel tab1 = new JPanel(new BorderLayout());
tab1.add(frame.getJMenuBar(),BorderLayout.NORTH);
tab1.add(frame.getContentPane());
tabs.addTab("1", tab1);

Swing creating a tabbed pane

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);

java accessibility for nested panel inside a main panel in swing

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;
}

Categories

Resources