Changing visibility of JPanels in JFrame not working as intended - java

I'm having an issue with switching between different JPanels in a JFrame. I am trying to add two different panels to a JFrame that the user can go back and forth between, with mainPanel being displayed when the program is run. mainPanel was showing correctly until I added buyPanel to the frame. Now I'm having issues displaying mainPanel again. When I currently run my program, it comes up as blank. The issue seems to be with the last three lines, however I can't figure out what is wrong.
I've included the relevant code below.
public class TestClass extends JFrame {
private JTextArea welcomeText;
private JComboBox commandsMenu;
private JPanel mainPanel;
private JPanel buyPanel;
private JTextArea buyType;
public TestClass() {
super("TestProgram");
setDefaultLookAndFeelDecorated(true);
setSize(550, 350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Setup elements for main panel (includes commands menu)
mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
welcomeText = new JTextArea(10, 10);
welcomeText.setEditable(false);
welcomeText.setLineWrap(true);
welcomeText.setWrapStyleWord(true);
welcomeText.setText(menu());
commandsMenu = new JComboBox();
commandsMenu.setEditable(true);
commandsMenu.setPrototypeDisplayValue("Commands");
commandsMenu.setSize(50, 50);
commandsMenu.addItem("Buy");
commandsMenu.addItem("Quit");
mainPanel.add(commandsMenu);
mainPanel.add(welcomeText);
add(mainPanel);
// Setup elements for buy panel
buyPanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
buyType = new JTextArea();
buyType.setText("Type");
buyType.setEditable(false);
buyPanel.add(commandsMenu);
buyPanel.add(buyType);
add(buyPanel);
buyPanel.setVisible(false);
mainPanel.setVisible(true);

Related

Contents of JScrollPane are not visible

I have a main class:
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Hex");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent inputs = new InputPanel();
JComponent hexGrid = new HexGridPanel(10,10,30);
JComponent outputs = new OutputPanel();
JComponent toolbar = new ToolbarPanel(); // This one is having problems
Container pane = frame.getContentPane();
pane.add(inputs, BorderLayout.LINE_START);
pane.add(hexGrid, BorderLayout.CENTER);
pane.add(outputs, BorderLayout.LINE_END);
pane.add(toolbar, BorderLayout.PAGE_END); // This one is having problems
frame.pack();
frame.setVisible(true);
}
}
And all of my other panels work except for ToolbarPanel that for some reason does not show its content:
public ToolbarPanel(){
JPanel content = new JPanel();
content.setLayout(new BoxLayout(content, BoxLayout.X_AXIS));
ButtonGroup buttonGroup = new ButtonGroup();
JRadioButton button = new JRadioButton("Test");
buttonGroup.add(button );
content.add(button );
JScrollPane scroll = new JScrollPane(content);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
content.setBorder(new LineBorder(Color.RED));
scroll.setBorder(new LineBorder(Color.GREEN));
this.add(scroll);
this.setPreferredSize(new Dimension(900, 200));
this.setBorder(new LineBorder(Color.BLACK)); // Only this is showing up in the UI
}
The ToolbarPanel itself shows up, but not the scroll pane or the radio buttons. It should show up inside of the black rectangle at the bottom of this image:
Well, you didn't include a MRE and I don't see the declaration of your class but I'm guessing you are using:
public class ToolbarPanel extends JComponent
The problem is that by default a JComponent doesn't have a layout manager so you won't see your components.
If you use:
public class ToolbarPanel extends JPanel
It will be a little better, but all the components will be displayed in a small square.
So you will also want to add
setLayout( new BorderLayout() );
to your constructor.
Note:
This is why a minimal reproducible example should be included with every question. We should not have to spend time guessing what you may or may not be doing.

Trying to create a GUI using java but not able to implement them the way i wanted

import javax.swing.*;
import java.awt.*;
public class SQlUI {
public static void main(String[] args){
SQlUI user=new SQlUI();
user.go();
}
public void go(){
//Creating a Frame
JFrame frame=new JFrame();
//Creating three Panels
JPanel panel0=new JPanel();
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
//Creating three Buttons
JButton button0=new JButton("INSERT");
JButton button1=new JButton("UPDATE");
JButton button2=new JButton("DELETE");
//Adding panel0 to the frame which contains three butoon objects
frame.getContentPane().add(BorderLayout.SOUTH,panel0);
panel0.add(button0);
panel0.add(button1);
panel0.add(button2);
//Creating four textbox
JTextField textbox0 = new JTextField(120);
JTextField textbox1 = new JTextField(120);
JTextField textbox2 = new JTextField(120);
JTextField textbox3 = new JTextField(120);
//Adding panel1 to the frame which contains four textbox objects
frame.getContentPane().add(BorderLayout.EAST,panel1);
//Using BoxLayout managaer for panel1 objects
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
panel1.add(textbox0);
panel1.add(textbox1);
panel1.add(textbox2);
panel1.add(textbox3);
//Adding panel2 to the frame which contains four label objects
frame.getContentPane().add(BorderLayout.WEST,panel2);
//Using BoxLayout managaer for panel1 objects
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
//Creating four labels
JLabel label0=new JLabel("Name");
label0.setSize(50,50);
label0.setVisible(true);
JLabel label1=new JLabel("ID");
label1.setSize(50,50);
label1.setVisible(true);
JLabel label2=new JLabel("AGE");
label2.setSize(50,50);
label2.setVisible(true);
JLabel label3=new JLabel("ADDRESS");
label3.setSize(50,50);
label3.setVisible(true);
//Adding labels to panel2
panel2.add(label0);
panel2.add(label1);
panel2.add(label2);
panel2.add(label3);
//Setting frame size and visiblity
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Details about the GUI- I am trying to create a GUI which have four textbox where user can enter his details and there are three buttons available on GUI by which user can send the data or store the data. The GUI also contains labels which tells the user which textbox is for which type of data.
Now the allignment of the objects are not coming the way i wanted it. All the labels are coming at one place and the size of box is very big. Tried to find a solution on stalkoverflow but got nothing.
i want something like this----
Name- textbox0
ID- textbox1
AGE- textbox2
ADDRESS- textbox3
Insert Update Delete
but getting something like----
textbox0
textbox1
Name
ID
AGE
Address textbox2
textbox3
Insert Update Delete
This is not the best looking thing you can do, but it's simple and a good start for you.
public class SQlUI {
public static void main(String[] args) {
SQlUI user = new SQlUI();
user.go();
}
public void go() {
JFrame frame = new JFrame();
JPanel buttonsPanel = new JPanel();
JButton insert = new JButton("INSERT");
JButton update = new JButton("UPDATE");
JButton delete = new JButton("DELETE");
buttonsPanel.add(insert);
buttonsPanel.add(update);
buttonsPanel.add(delete);
frame.getContentPane().add(buttonsPanel, BorderLayout.SOUTH);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
JPanel namePanel = new JPanel();
JLabel nameLabel = new JLabel("Name");
JTextField nameField = new JTextField(120);
namePanel.add(nameLabel);
namePanel.add(nameField);
JPanel idPanel = new JPanel();
JLabel idLabel = new JLabel("ID");
JTextField idField = new JTextField(120);
idPanel.add(idLabel);
idPanel.add(idField);
centerPanel.add(namePanel);
centerPanel.add(idPanel);
frame.getContentPane().add(centerPanel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Notice that I call frame.getContentPane().add(buttonsPanel, BorderLayout.SOUTH); with the panel as the first argument and the position second, you did it the other way around.
Not sure why you need text fields of length 120.
Call frame.pack() instead of setting the size yourself.
See what indentation does to the code.
You can complete the rest of the panels by yourself.
I suggest reading up on different layouts here. It'll give you more of an idea how you can lay your items out. You can then experiment to find out which one you actually want.
That said I find it far easier to design a GUI graphically, and IntelliJ has a pretty good GUI builder that I've often used. You'll then be able to play around with layout managers and JPanels to get the effect you actually want.

drag and drop of jcomponents in java in rum time

I can do this:
I have a JFrame with 2 JPanel objects. In the panel on the left, I want to put the components of Java Swing useful for the construction of an user interface. The panel on the right is empty.
On run-time, my user should to be able to copy with drag and drop drag from one panel to another the selected component. But the copied component on the right panel, can be moved or deleted.
I have created the panel, but I don't know if is better inserire the component as image, label or true component.. and how to make this possible..
This is my panel for the customization.. I have to insert the component in the JTabbedPane in the PanelSx..
public class Customize extends JFrame {
private JPanel panelSx, panelCx, panelMobile;
private JButton buttonSave;
private TabbedPaneComponents tpc;
public Customize(){
Container c = getContentPane();
c.setLayout(new BorderLayout());
setResizable(true);
setTitle("Design Preview");
setSize(800, 650);
setLocation(250,50);
panelSx = new JPanel();
panelSx.setPreferredSize(new Dimension(300, 200));
panelSx.setOpaque(true);
panelSx.setBackground(Color.RED.darker());
panelCx = new JPanel();
panelCx.setPreferredSize(new Dimension(200, 200));
panelCx.setOpaque(true);
panelCx.setBackground(Color.BLUE);
// display panel
panelMobile = new JPanel();
panelMobile.setPreferredSize(new Dimension(300,500));
panelMobile.setOpaque(true);
panelMobile.setBackground(Color.PINK.darker());
panelMobile.setFocusable(false);
buttonSave = new JButton("Save");
panelCx.add(panelMobile);
c.add(panelSx, BorderLayout.WEST);
c.add(panelCx, BorderLayout.CENTER);
tpc = new TabbedPaneComponents();
panelSx.add(tpc);
}
}
public class TabbedPaneComponents extends JTabbedPane{
private JPanel panel1,panel2,panel3;
public TabbedPaneComponents(){
panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(200,300));
addTab("Form Widgets", panel1);
panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(200,300));
addTab("Text Field", panel2);
panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(200,300));
addTab("Other", panel3);
}
}
I would have to suggest using a JToolBar because of it's built in drag'n'drop features. I would put the component inside of the toolbar.

How to align two JButtons to be right aligned?

So currently my program shows only one of the buttons in the bottom right hand of the GUI. But I want to show both buttons in the bottom right hand corner. Any ideas how to set both buttons to the right corner? Here is my code so far:
import javax.swing.*;
import java.awt.*;
public class Other extends JFrame{
private static final long serialVersionUID = 1L;
public Other() {
super("Buttons");
final Container mainPanel = getContentPane();
mainPanel.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BorderLayout());
JPanel inputPanel = new JPanel();
inputPanel.add(new JLabel("RANDOM TEXT HERE"));
inputPanel.add(new JLabel("RANDOM TEXT HERE"));
inputPanel.add(new JLabel("RANDOM TEXT HERE"));
JButton s = new JButton("first");
JButton l = new JButton("second");
buttonPanel.add(s,BorderLayout.LINE_END);
buttonPanel.add(l,BorderLayout.LINE_END); //<-- not working
mainPanel.add(inputPanel,BorderLayout.PAGE_START);
mainPanel.add(buttonPanel,BorderLayout.PAGE_END);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args){
Other o = new Other();
}
}
buttonPanel.setLayout(new FlowLayout(FlowLayout.TRAILING));
While the BorderLayout will only accept one component per layout area, FlowLayout will display as many as are added (within viewable bounds).
you can design GUI better and easy with Netbeans 7.1 .. you can align the swing components wherever you like and even make dependent on size of the frame ... you can get it here https://netbeans.org/downloads/index.html

Adding components into JPanel inside a JFrame

Since im a beginner and i don't want to get involved with the layout managers, i was simply adding a JPanel into my main JFrame and giving spesific location to each component in the panel. But somehow the output appears way too wrong..
frame = new JFrame(email + " (Offline)");
frame.setSize(400, 400);
frame.setLocation(0, 0);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
// out.println("BYE");
// out.flush();
frame.dispose();
thread.stop();
}
});
panel = new JPanel();
frame.add(panel);
chat = new JTextArea();
chat.setSize(400, 200);
chat.setLocation(0, 0);
chat.setEditable(false);
panel.add(chat);
panel.validate();
JLabel you = new JLabel("You:");
you.setSize(you.getPreferredSize());
you.setLocation(0, 210);
panel.add(you);
panel.validate();
input = new JTextArea();
input.setSize(200, 200);
input.setLocation(0, 220 + chat.getSize().height);
panel.add(input);
panel.validate();
send = new JButton("Send");
send.setSize(send.getPreferredSize());
send.setLocation(210, 220 + chat.getSize().height);
panel.add(send);
panel.validate();
frame.setVisible(true);
The outcome of this frame is that text areas are invisible, a You: label in the middle and next to the right of it the button.. What am i missing here?
Again, don't use null layout since it makes updating and maintaining your GUI much more difficult than it should be, and can lead to ugly GUI's if you plan on having them run on multiple platforms. Instead
Use several JPanels, each one holding a core group of components and each using its best layout manager
Nest these JPanels in other JPanels that use the best layout manager to display them
and that will allow your GUI to be resizeable without need of extra code.
Put your JTextAreas in JScrollPanes so that you can see all text even if it goes beyond the text area.
Never set the size of the JTextArea as that will not allow it to scroll. Instead set its columns and rows.
As a very simple example, run this to see what I mean:
import java.awt.*;
import javax.swing.*;
public class FooSwing2 {
public static void main(String[] args) {
JTextArea chatArea = new JTextArea(8, 40);
chatArea.setEditable(false);
chatArea.setFocusable(false);
JScrollPane chatScroll = new JScrollPane(chatArea);
JPanel chatPanel = new JPanel(new BorderLayout());
chatPanel.add(new JLabel("Chat:", SwingConstants.LEFT), BorderLayout.PAGE_START);
chatPanel.add(chatScroll);
JTextField inputField = new JTextField(40);
JButton sendBtn = new JButton("Send");
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.LINE_AXIS));
inputPanel.add(inputField);
inputPanel.add(sendBtn);
JPanel youLabelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
youLabelPanel.add(new JLabel("You:"));
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
mainPanel.add(chatPanel);
mainPanel.add(Box.createVerticalStrut(10));
mainPanel.add(youLabelPanel);
mainPanel.add(inputPanel);
JFrame frame = new JFrame("Foo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
This would result in a simple (non-functioning) GUI that looked like this:
Now say you want to change this and add another button, an "exit" JButton to the right of the send JButton. If you used null layout, you'd have to resize your GUI, you'd have to move the send button over to the left and make sure that your math was without error, etc. If you used layout managers, you'd need just two new lines of code (to change the display, not the functionality of course):
JTextField inputField = new JTextField(40);
JButton sendBtn = new JButton("Send");
JButton exitBtn = new JButton("Exit"); // ***** added
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.LINE_AXIS));
inputPanel.add(inputField);
inputPanel.add(sendBtn);
inputPanel.add(exitBtn); // ***** added
That's it, and this would display:

Categories

Resources