How to open a new window by clicking a button - java

As a part of my program, I need to have a button that when the user click on it, it opens a new window.
Well I guess I should have a class that make the frame and call it by the button. but I don't have any idea to start. I just got my button in the program, but it does not work. So can some tell me how to do it? or code it.

Here is a simplified version of what you want to do:
JButton button = new JButton("New Frame");
button.addActionListener( new ActionActionListener()
{
public void actionPerformed(ActionEvent e)
{
// Create a method named "createFrame()", and set up an new frame there
// Call createFrame()
}
});
You would probably want to call some method in the ActionListener rather than make the frame on actionPerformed. Maybe something like this:
public static void createFrame()
{
EventQueue.invokeLater(new Runnable()
{
#Override
public void run()
{
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setOpaque(true);
JTextArea textArea = new JTextArea(15, 50);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
textArea.setFont(Font.getFont(Font.SANS_SERIF));
JScrollPane scroller = new JScrollPane(textArea);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
JPanel inputpanel = new JPanel();
inputpanel.setLayout(new FlowLayout());
JTextField input = new JTextField(20);
JButton button = new JButton("Enter");
DefaultCaret caret = (DefaultCaret) textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
panel.add(scroller);
inputpanel.add(input);
inputpanel.add(button);
panel.add(inputpanel);
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
frame.setResizable(false);
input.requestFocus();
}
});
}
What that frame should look like:

new CLASS_NAME().setVisible(true);
eg. new NewJFrame().setVisible(true);

Related

How to create multiple frames or windows in GUI?

I am new to creating GUI. I want to know how to create multiple windows. I want to show another frame if a button is to be clicked. I have searched how and i saw that some people are making another GUI form and just calling the other form i a button was clicked, but i dont understand how.
There are numerous ways to do this. One of the main ways is to create a new Java Class with its own properties. Here is a nexample:
JButton button = new JButton("Button_Leads_To_This_Window");
button.addActionListener( new ActionActionListener()
{
public void actionPerformed(ActionEvent e)
{
NewFrame();
}
});
This will allow the button to call a new window, similar as the way you called the "My Empire" window. For example NewFrame() class will look like this:
public static void newFrame()
{
EventQueue.invokeLater(new Runnable()
{
#Override
public void run()
{
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setOpaque(true);
JTextArea textArea = new JTextArea(15, 50);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
textArea.setFont(Font.getFont(Font.SANS_SERIF));
JScrollPane scroller = new JScrollPane(textArea);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
JPanel inputpanel = new JPanel();
inputpanel.setLayout(new FlowLayout());
JTextField input = new JTextField(20);
JButton button = new JButton("Enter");
DefaultCaret caret = (DefaultCaret) textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
panel.add(scroller);
inputpanel.add(input);
inputpanel.add(button);
panel.add(inputpanel);
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
frame.setResizable(false);
input.requestFocus();
}
});
}
Here is more information to the matter:
https://www.thoughtco.com/create-a-simple-window-using-jframe-2034069
https://www.youtube.com/watch?v=RMz9LYY2g4A
Good luck.

Change color of JButton without changing its shape

I want to change the color of JButton by:
JButton button = new JButton();
button.setBackground(Color.decode("#00a5ff"));
In order for change to occur, I have to add:
button.setOpaque(true);
button.setBorderPainted(false);
However, this remove the curves around the edges and thus changes the shape of the button. Is there a way to just simply change to color and keep the other properties? Another example is the changing of color (getting darker) when you press a button without having changed its color.
Here is some code that illustrates the difference between the two buttons:
public static void main(String[] args) {
JFrame frame = new JFrame("frame");
frame.setSize(new Dimension(300, 300));
JButton button1 = new JButton();
JButton button2 = new JButton();
button1.setPreferredSize(new Dimension(100,100));
button2.setPreferredSize(new Dimension(100,100));
button2.setBackground(Color.decode("#00a5ff"));
button2.setBorderPainted(false);
button2.setOpaque(true);
JPanel pane1 = new JPanel(new FlowLayout());
JPanel pane2 = new JPanel(new FlowLayout());
pane1.add(button1);
pane2.add(button2);
frame.add(pane1, BorderLayout.WEST);
frame.add(pane2, BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
Thanks
I literally just tested the following:
JFrame frame = new JFrame("frame");
frame.setSize(new Dimension(300, 300));
JButton button = new JButton();
button.setBackground(Color.decode("#00a5ff"));
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
It seemed to work, but I am working on Ubuntu Studio 16.04. If you notice that it does not work, then let me know. Could you please show the result of your white button or your failed button (if it still doesn't work)?
Perhaps you have something else going on in your code. I altered a small example I have and the color changes with only setBackground(Color) using regular Button and using JButton. See the following...
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
Button closeButton = new Button("Close");
closeButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
frame.dispose();
}
});
closeButton.setBackground(Color.decode("#00a5ff"));
panel.add(closeButton);
frame.add(panel);
frame.setSize(200, 100);
frame.setLocation(200, 50);
frame.setVisible(true);
}

Content is being add horizontally to JScrollPane

im starting to work with java Swing and i was trying to make a system to show something like a Map :
But when i try to add another entry to the JSCrollPane it's being added horizontally intead of vertically, i have tried everything i don't what i mithgt be doing wrong but i can't manage do fix it.
Here i create the Frame :
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setResizable(false);
JPanel panel = new JPanel();
final JPanel content = new JPanel();
new DataEntry("", 0).create(content);
final JScrollPane scrollPane = new JScrollPane(content, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setWheelScrollingEnabled(true);
panel.add(scrollPane);
scrollPane.setMinimumSize(new Dimension(400, 60));
final JButton add = new JButton("Add");
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new DataEntry("", 0).create(content);
}
});
panel.add(add);
frame.setContentPane(panel);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.pack();
frame.setVisible(true);
And this is how i create the Entry :
public JPanel create(final JPanel content) {
final JPanel panel = new JPanel();
final JPanel fields = new JPanel();
fields.add(new JLabel("Variable"));
fields.add(variable);
fields.add(new JLabel("Row"));
fields.add(row);
panel.add(fields);
JButton remove = new JButton("Remove");
remove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
content.remove(panel);
content.revalidate();
}
});
panel.add(remove);
content.add(panel, BorderLayout.CENTER);
content.revalidate();
return panel;
}
At the start i was wondering why it wasn't displaying any new Entry, then i tried changing HORIZONTAL_SCROLLBAR_NEVER > HORIZONTAL_SCROLLBAR_AS_NEEDED and then i realized that the variables were being add horizontally.
Here is a gif to see what's going on (Couldn't manage to take a proper photo) : GIF

Calling a JPanel inside a split panel Java

Hi I'm having trouble in my code. I can't call or display my JPanel(TruthTable) inside my splitpane in the Minimize class when I click the button and Keypressed(Enter).The problem is in the class Minimize
*UPDATE: I'have solved my problem regarding to diplaying the panel my next problem is when I call the constructor the text in the panel should be updated.I also change the setText to append 'coz they say that JPanel doesnt support setText.I've already used,validate,repaint method but no luck in updating the textArea.
Heres my code for class Minimize:
package splashdemo;
public class Minimize extends JPanel implements ClipboardOwner
{
simButton.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent ae)
{
LogicGates lg= new LogicGates();
lg.geth();
sim s=new sim();
s.InputExp(lg.g,lg.g1);
s.menu();
s.setSize(700,700);
s.setVisible(false);
Frame f = new Frame();
Panel panel = new Panel();
f.add(BorderLayout.SOUTH, panel);
f.add(BorderLayout.CENTER, new sim());
f.add(BorderLayout.SOUTH, panel);
f.setSize(580, 500);
panel.add(s.button);
f.show();
f.setVisible(false);
JOptionPane.showMessageDialog(null, b);
ex=b;
cAppend();
JOptionPane.showMessageDialog(null, ex1);
InToPost(ex1);
foutput= doTrans();
JOptionPane.showMessageDialog(null, foutput);
TruthTable_1 kl = new TruthTable_1(foutput);
Minimize mi = new Minimize();
s.InputExp(lg.g,lg.g1);
s.menu();
lg.gatessplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, mi, lg.p1);
//here's the splitpane where I should display the TruthTable_1 panel but it doesn't diplay
lg.p1.add(lg.gatessplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT, s,kl));
lg.gatessplit.setOneTouchExpandable(true);
lg.gatessplit.setDividerLocation(250);
lg.gatessplit.setPreferredSize(new Dimension(900, 500));
lg.createAndShowGUI();
}});
}
Heres my code the TruthTable_1:constructors
public TruthTable_1() {
super();
tableArea.append("This is a Test");
//setBackground(Color.black);
createg();
setVisible(true);
}
public void createg(){
setLayout(new BorderLayout());
line= 0;
truth= 'T';
fallacy= 'F';
final JPanel top= new JPanel();
input= new TextField(35);
TFCheck= new Checkbox("Select for \"1/0\" (default\"T/F\")");
check= new JButton("Check");
check.addActionListener(this);
top.add(input);
top.add(TFCheck);
top.add(check);
top.setVisible(false);
tableArea= new JTextArea(16, 40);
tableArea.setEditable(false);
tableArea.setFont(new Font("Lucida Console", Font.PLAIN, 14));
// all letters are the same width in this font
go= new JButton("Do it to it!");
go.addActionListener(this);
input.addActionListener(this);
go.setVisible(false);
add(top, BorderLayout.NORTH);
scrolls= new JScrollPane(tableArea);// add scroll buttons to the area
add(scrolls, BorderLayout.CENTER);
add(go, BorderLayout.SOUTH);
setSize(535, 500);
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(100, 100);
}
public TruthTable_1(String f){
createg();
tableArea.append(run(f));
}

updating a panel

I have a panel on my frame .and by clicking on a button I want to delete the old panel and make the other panel and add that panel to my frame.(also I use netbeans)
would you please help me that how can i do that?thanks
JFrame frame = new JFrame();
final JPanel origPanel = new JPanel();
frame.add(origPanel, BorderLayout.CENTER);
MouseListener ml = new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
// Mouse clicked on panel so remove existing panel and add a new one.
frame.remove(origPanel);
frame.add(createNewPanel(), BorderLayout.CENTER);
// Revalidate frame to cause it to layout the new panel correctly.
frame.revalidate();
// Stop listening to origPanel (prevent dangling reference).
origPanel.removeMouseListener(this);
}
}
origPanel.addMouseListener(ml);
This way:
final JFrame frame = new JFrame();
frame.setSize(200, 200);
final JPanel panelA = new JPanel();
final JPanel panelB = new JPanel();
JButton button = new JButton("Switch");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.remove(panelA);
frame.add(panelB);
frame.show();
}
});
JLabel label = new JLabel("This is panel B. Panel A is gone!");
panelB.add(label);
panelA.add(button);
frame.add(panelB);
frame.add(panelA);
frame.show();

Categories

Resources