I am trying to use a layered pane to make a menu for a program I'm working on, but the button won't display. I can't seem to figure out what it is...
public class FlashcardGUI {
public static void main(String[] args)
{
JFrame projectFrame = new JFrame("StudyFast Flashcard");
projectFrame.setName("StudyFast Flashcards");
projectFrame.setSize(1000,600);
projectFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
projectFrame.setVisible(true);
JLayeredPane projectLayeredPane = new JLayeredPane();
projectFrame.setContentPane(projectLayeredPane);
JPanel projectMenu1 = new JPanel();
projectLayeredPane.setLayer(projectMenu1, 0);
final JButton startNow = new JButton();
startNow.setText("Exit");
startNow.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
projectFrame.add(projectLayeredPane);
projectLayeredPane.add(projectMenu1);
projectMenu1.add(startNow);
}
}
Put these two lines at the end of your main method. The order is important in order to make the button display.
projectFrame.pack();
projectFrame.setVisible(true);
(Make sure to remove the projectFrame.setVisible(true); you already have on line 9.)
I have updated your code and it is working now. Please see the inline comments for the issue in your code. Hope this helps.
public class FlashcardGUI2 {
public static void main(String[] args) {
JFrame projectFrame = new JFrame("StudyFast Flashcard");
projectFrame.setName("StudyFast Flashcards");
projectFrame.setSize(1000,600);
projectFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
projectFrame.setVisible(true);
JLayeredPane projectLayeredPane = new JLayeredPane();
LayoutManager layout = new FlowLayout(); //creating a FlowLayout object
projectLayeredPane.setLayout(layout); //adding the layout to JLayeredPane
//because JLayeredPane do not have default layout of
//its own. The reason you were not
//getting the button displayed
projectLayeredPane.setPreferredSize(new Dimension(300, 310));
JPanel projectMenu1 = new JPanel();
final JButton startNow = new JButton();
startNow.setText("Exit");
startNow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
projectLayeredPane.add(projectMenu1,new Integer(50));
projectLayeredPane.add(startNow,new Integer(10));
projectFrame.add(projectLayeredPane);
projectFrame.pack();
}
}
Related
I would like to make a JPanel pop up over an other JPanel in java. However I can not find anything useful on the internet. I tried playing around with absolute positioning but things like buttons from the bottom layer are showing through the top layer.
I uploaded a really ugly drawing of what I want to do.
Is there an easy way of doing this?
Edit:
I tried to make what "Ulkra" suggested. Here is the code and a screenshot:
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(new Dimension(400, 400));
JPanel panel = new JPanel();
JPanel foregroundPanel = new JPanel();
foregroundPanel.setVisible(false);
foregroundPanel.setBackground(Color.BLUE);
foregroundPanel.setLayout(new BoxLayout(foregroundPanel, BoxLayout.Y_AXIS));
JPanel backgroungPanel = new JPanel();
backgroungPanel.setBackground(Color.RED);
backgroungPanel.setLayout(new BoxLayout(backgroungPanel, BoxLayout.Y_AXIS));
for (int i = 0; i < 10; i++) {
backgroungPanel.add(new JButton("BackgroundBtn " + i));
}
foregroundPanel.add(new JButton("ForegroundBtn 1"));
JButton makeVisibleBtn = new JButton("+");
makeVisibleBtn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
foregroundPanel.setVisible(true);
}
});
backgroungPanel.add(makeVisibleBtn);
JButton makeInvisibleBtn = new JButton("-");
makeInvisibleBtn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
foregroundPanel.setVisible(false);
}
});
foregroundPanel.add(makeInvisibleBtn);
panel.add(backgroungPanel);
panel.add(foregroundPanel);
frame.add(panel);
}
}
For this, you have to use a Panel (if you are using NetBeans, you can find it in Swing Containers) and in the constuctor of the Frame, you have to SET IT AS UNVISIBLE, so you need the buttom to see it: (In this example, i call my Panel as "jPanel1" and i put a buttom in it)
public Main() {
initComponents();
jPanel1.setVisible(false);//<-- This!
this.setLocationRelativeTo(null);
}
Then, you create the buttom to "activate it" and but this code in:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jPanel1.setVisible(true);
}
(And of course, if you wanna set it as unvisible you just have to do the same as in the constructor).
I test it and it works:(Imgur is not working so excuse i have to use postimg.org)
https://postimg.org/gallery/1z6fpajva/
Now you could put a border so you can differentiate your new Panel from your other Frame:
https://postimg.org/image/mdpf7hvxx/
Hi I am trying to create Scroll Bar for my JFrame. I created JPanel object and set components into JPanel. Then created a JScrollPane object for the panel. Then add the ScrollPane object to JFrame. I am not seeing any scrollbar. Also I am wondering if there is a option in JPanel that would resize the object inside Jpanel automatically according to the zoom level of the JPanel. Any help would be highly appreciated.
public class xmlottgui {
private JPanel Container;
private JFrame frmCreateXml;
private JTextField titlename;
private JLabel lbltitlename;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
xmlottgui window = new xmlottgui();
window.frmCreateXml.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public xmlottgui() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
Container = new JPanel();
Container.setLayout(null);
//JScrollPane pane=new JScrollPane(Container,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frmCreateXml = new JFrame();
frmCreateXml.setTitle("Create XML");
frmCreateXml.setBounds(100, 100, 1000, 1200);
frmCreateXml.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmCreateXml.getContentPane().setLayout(null);
//Create MenuBar
JMenuBar menuBar = new JMenuBar();
Container.add(menuBar);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
JMenuItem mntmImportFromCsv = new JMenuItem("Import From Excel File");
//Add menu item Exit
JMenuItem mntmexit = new JMenuItem("Exit");
mntmexit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
mnFile.add(mntmexit);
showform();
JScrollPane pane=new JScrollPane(Container,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
pane.setLayout(null);
frmCreateXml.setContentPane(pane);
frmCreateXml.getContentPane().add(pane);
}
private void showform(){
titlename = new JTextField();
titlename.setBounds(164, 27, 749, 26);
Container.add(titlename);
titlename.setColumns(10);
lbltitlename = new JLabel("Title Name");
lbltitlename.setBackground(Color.GRAY);
lbltitlename.setBounds(22, 1000, 90, 16);
Container.add(lbltitlename);
}
This:
pane.setLayout(null);
Will completely disable your JScrollPane and prevent it from working as it will prevent the JScrollPane from displaying and manipulating its view port properly. JScrollPanes have there own very special layout manager, one you never want to muck with unless you are very clever and know what you're doing. As a general rule you should almost never use null layouts.
Also this is not correct:
frmCreateXml.setContentPane(pane);
frmCreateXml.getContentPane().add(pane);
You make pane the contentPane and then add pane to itself.
AND THIS is messing you up:
frmCreateXml.getContentPane().setLayout(null);
You will want to learn about and use the layout managers as it will make your life much easier.
I am working on making a specific UI in java (Programmatically) but i wasn't able to achieve my goal UI. I need help in achieving my goal UI. I even tried SWT Designer and still i didn't succeed . Thanks in Advance.
My Code:
public class Main{
/**
* #param args
*/
static JTextArea lblOne = new JTextArea ("Enter Nb Of Rows");
static JButton btn1 = new JButton("Generate Table");
final static TextField tf1 = new TextField();
static JFrame AFrame;
public static void main(String[] args) {
// TODO Auto-generated method stub
AFrame = new JFrame("Pascal's Table Generator");
AFrame.setLayout(new BorderLayout());
// Add a window listner for close button
AFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
tf1.setText("");
//AFrame.add(lblOne);
//lblOne.setText(" \n ");
//JTextArea textArea = new JTextArea(5, 5);
JScrollPane scrollableTextArea = new JScrollPane(lblOne);
scrollableTextArea.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollableTextArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
AFrame.getContentPane().add(scrollableTextArea);
// AFrame.add(new JScrollPane(lblOne));
AFrame.add(btn1);
AFrame.add(tf1);
AFrame.setSize(800, 800);
btn1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
lblOne.setText("");
genPyrN(Integer.parseInt(tf1.getText().toString()));
}
});
//set the layout of the frame to FlowLayout
//and align the components to the center
AFrame.setLayout(new FlowLayout(FlowLayout.CENTER));
AFrame.setVisible(true);
Current UI:
Target UI:
First of all you should read more about the LayoutManagers in Java, I like this one: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
In this concrete situation the BorderLayout is a good idea. The Frame could have a BorderLayout and the JScrollPane could go into the CENTER part and the buttons to the NORTH.
But this makes no sense after setting the BorderLayout in your code: AFrame.setLayout(new FlowLayout(FlowLayout.CENTER));. I suggest to create a JPanel, add the buttons to it and then put this panel into the BorderLayouts NORTH part.
I have learnt to create a menu with radio buttons using java, but I want to know how to make radio buttons WITHIN radio buttons. The real problem is that I have finally created the radio buttons 'within' a radio button by displaying a new menu with a new group of buttons, but whenever I click on these new radio buttons, nothing happens. It is as if the buttons are not being listened to. Here is my code (example, but still the same idea):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestRadioButtons extends JPanel implements ActionListener
{
private String test = "Test Button 1";
private String random = "RANDOM";
private String test2 = "Button to Click";
private String random2 = "RANDOM MK. 2";
private static boolean menu = false;
public TestRadioButtons()
{
super(new BorderLayout());
//creates first set of radio buttons
JRadioButton testButton = new JRadioButton(test);
testButton.setMnemonic(KeyEvent.VK_T);
testButton.setActionCommand(test);
JRadioButton randomButton = new JRadioButton(random);
randomButton.setMnemonic(KeyEvent.VK_R);
randomButton.setActionCommand(random);
//groups the first set of buttons
ButtonGroup group1 = new ButtonGroup();
group1.add(testButton);
group1.add(randomButton);
//register listener for first radio buttons
testButton.addActionListener(this);
randomButton.addActionListener(this);
//put first radio buttons into a column in a panel
JPanel radioPanel1 = new JPanel(new GridLayout(0, 1));
radioPanel1.add(testButton);
radioPanel1.add(randomButton);
//set first menu border
add(radioPanel1, BorderLayout.LINE_START);
setBorder(BorderFactory.createEmptyBorder(20,20,20,250));
//I also have it so that if the a boolean value equals true, the following menu appears:
if (menu == true)
{
JRadioButton test2Button = new JRadioButton(test2);
test2Button.setMnemonic(KeyEvent.VK_A);
test2Button.setActionCommand(test2);
JRadioButton random2Button = new JRadioButton(random2);
random2Button.setMnemonic(KeyEvent.VK_B);
random2Button.setActionCommand(random2);
ButtonGroup group2 = new ButtonGroup();
group2.add(test2Button);
group2.add(random2Button);
test2Button.addActionListener(this);
random2Button.addActionListener(this);
JPanel radioPanel2 = new JPanel(new GridLayout(0, 1));
radioPanel2.add(test2Button);
radioPanel2.add(random2Button);
add(radioPanel2, BorderLayout.LINE_START);
setBorder(BorderFactory.createEmptyBorder(20,20,20,250));
}
}
public static void menu2()
{
JFrame innerMenu = new JFrame();
innerMenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent innerComponent = new TestRadioButtons();
innerComponent.setOpaque(true);
innerMenu.setContentPane(innerComponent);
innerMenu.pack();
innerMenu.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (test.equals(e.getActionCommand()))
{
menu = true;
menu2();
if (test2.equals(e.getActionCommand()))
{
JOptionPane.showMessageDialog(null, "This is just a TEST!");
}
}
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Radio Button Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent component = new TestRadioButtons();
component.setOpaque(true);
frame.setContentPane(component);
frame.pack();
frame.setVisible(true);
}
public static void main (String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
If anyone is able to help me (as well as fix up my code in the part where I create the new menu), I would be grateful.
Just a } problem
Change your public void actionPerformed(ActionEvent e) like this
public void actionPerformed(ActionEvent e)
{
if (test.equals(e.getActionCommand()))
{
menu = true;
menu2();
}
if (test2.equals(e.getActionCommand()))
{
JOptionPane.showMessageDialog(null, "This is just a TEST!");
}
}
I have one class called TabBuilder which I use to create the interface of my application, but I'm going through a weird problem. If I try to run the code as it's below it will draw the mainScreen but if I request the SearchScreen from the BarMenu it doesn't show up. If I try to execute only SearchScreen by itself (calling its builder) within the public static void main (String[] args) MainString it wont show up too. but If go to the request event and tip TabBuilder tb = new TabBuilder(); tb.requestTab();the screen will show up as it should be. So, what might be wrong? Thanks in advance
MainScreen:
public class MainScreen{
public MainScreen()
{
TabBuilder tb = new TabBuilder();
tb.mainTab();
}
}
SearchScreen:
public class SearchScreen{
public void SearchScreen(){
TabBuilder tb = new TabBuilder();
tb.requestTab();
}
}
TabBuilder:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TabBuilder implements ActionListener {
JTabbedPane tabbedPane = new JTabbedPane();
JMenuItem close, request;
protected JTextField txtrequest = new JTextField();
JButton btrequest = new JButton();
protected JFrame requestFrame = new JFrame();
public void TabBuilder(){
}
public void mainTab(){
JMenuBar bar;
JMenu file, register;
JFrame mainFrame = new JFrame();
bar= new JMenuBar();
file= new JMenu("File");
register= new JMenu("Request");
close= new JMenuItem("Close");
close.addActionListener(this);
request= new JMenuItem("Request Query");
request.addActionListener(this);
bar.add(file);
bar.add(register);
file.add(close);
register.add(request);
mainFrame.setExtendedState(mainFrame.getExtendedState() | mainFrame.MAXIMIZED_BOTH); // Maximized Window or setSize(getMaximumSize());
mainFrame.setTitle("SHST");
mainFrame.setJMenuBar(bar);
mainFrame.setDefaultCloseOperation(0);
mainFrame.setVisible(true);
WindowListener J=new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
};
mainFrame.addWindowListener(J);
}
public void requestTab(){
JLabel lbrequest;
JPanel requestPane;
btrequest= new JButton("request");
lbrequest= new JLabel("Type Keywords in english to be requested below:");
txtrequest= new JTextField();
requestPane=new JPanel();
requestPane.setBackground(Color.gray);
requestPane.add(lbrequest);
requestPane.add(txtrequest);
requestPane.add(btrequest);
requestPane.setLayout(new GridLayout(3,3));
btrequest.setEnabled(true);
requestFrame.add(requestPane);
requestFrame.setTitle("SHST");
requestFrame.setSize(400, 400);
requestFrame.setVisible(true);
requestFrame.setDefaultCloseOperation(1);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==close){
System.exit(0);
}
if(e.getSource()==request){
TabBuilder tb = new TabBuilder();
tb.requestTab();
}
}
public static void main (String[] args){
MainScreen m = new MainScreen();
}
}
The constructor of SearchScreen was settled as void. That caused nothing to return as object when calling the constructor. Newbie failure but simple solution.