I am designing an application in NetBeans, as illustrated in the screenshot below.
When the user clicks on a JButton on a JFrame, a JDialog pops-up asking the user to enter a numeric value using a numeric keypad. I would like the JDialog to dynamically add 2 JPanels. JPanel 1 will contain a textbox for input. JPanel 2 will contain a numeric keypad. I designed them this way so that I could reuse the numeric keypad whenever I need it. The problem I am facing is displaying dynamically these 2 JPanels on the JDialog that pops-up. JDialog pops-up empty. Please take a look at my code below. Thank you all, I appreciate your help
This is the sample code of JDialog:
public class MyDialog extends javax.swing.JDialog {
public MyDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {//Add JPanel 2 (Numeric Keypad) to JDialog
Container contentPane = getContentPane();
NumericKeypadPanel nkp = new NumericKeypadPanel();
nkp.setLayout(new java.awt.BorderLayout());
contentPane.removeAll();
contentPane.add(nkp);
contentPane.validate();
contentPane.repaint();
}
});
}
This is the sample code for JPanel 2 (Numeric Keypad):
public class NumericKeypadPanel extends javax.swing.JPanel {
/** Creates new form NumericKeypadPanel */
public NumericKeypadPanel() {
initComponents();//Draws 10 number buttons
}
}
basicall there are two ways
1) add a new JComponent by holding JDialog size (in pixels) on the screen, all JCompoenets
or part of them could be shrinked
2) resize JDialog by calling pack(), then JDialog will be resized
both my a.m. rulles works by using Standard LayoutManagers (excepting AbsoluteLayout)
What is in the initComponents() function of the NumericKeypadPanel? If it's not actually creating components, you're not going to see anything in the dialog. I added a single line to the NumericKeypadPanel's constructor to change the background color of this panel, and indeed, it shows up in the dialog as a green panel.
public NumericKeypadPanel() {
//initComponents();//Draws 10 number buttons
setBackground(Color.green);
}
Related
Okay so I've successfully created a modal JDialog and I used Netbeans GUI Builder to create panels to speed up the design. However, the modal JDialog doesn't show the panels it has, therefore empty. I don't know what to do next and I don't see any reason why it won't show up if the main container does show.
JDialog is expected to come up after 2 mouse clicks on JTable
What comes up is this.
instead of this (Update Curriculum Gui), below.
private void curriculumListJtblMouseClicked(java.awt.event.MouseEvent evt) {
int clickCount = evt.getClickCount();
if (clickCount == 2) {
UpdateCurriculumGui updateCurriculum = new UpdateCurriculumGui();
updateCurriculum.setPreferredSize(new Dimension(1000, 650));
updateCurriculum.setVisible(true);
updateCurriculum.pack();
updateCurriculum.setLocationRelativeTo(null);
}
}
UpdateCurriculumGui on it's own class.
public class UpdateCurriculumGui extends javax.swing.JDialog {
public UpdateCurriculumGui() {
super(null, ModalityType.MODELESS);
setAlwaysOnTop(true);
setTitle("Update Curriculum Information");
}
}
I hope you can help me because I haven't tried to use JDialogs before. I'd appreciate any suggestions.
Thanks.
you have to fill your dialog with content!
public class UpdateCurriculumGui extends javax.swing.JDialog {
public UpdateCurriculumGui() {
super(null, ModalityType.APPLICATION_MODAL);
//setAlwaysOnTop(true); set modal instead
setTitle("Update Curriculum Information");
add(new JLabel("i'm content!")); //this is content!
}
}
see Dialog.ModalityType for details on MODELESS (shouldn't it be APPLICATION_MODAL ?)
i just started to use Java to build a GUI. Now i ran in an error that causes very strange behaviour with the JTextArea. I used this to create the TextArea:
`
public class gui{
JTextArea ausgabe;
public gui(){
//Some other Stuff in here
//
//
ausgabe = new JTextArea("Test \n Text",15,50);
ausgabe.setSize(110, 170);
ausgabe.setVisible(true);
mainFrame.add(ausgabe);
ausgabe.setLocation(170,20);
ausgabe.setEnabled(false);
}
}
Now until this point everything just works fine. But I want another method to change the text of the area (with ausgabe.setText("String");) the area relocates itself to x,y = 0 of the JFrame and layers itself above all other JFrame elements. Thanks for help!
I highly recommend not trying to fight the Layout Managers
For a simple fix, use the default layout manager for a JFrame like so
public class gui
{
JTextArea ausgabe;
public gui()
{
ausgabe = new JTextArea("Test \n Text");
mainFrame.add(ausgabe, BorderLayout.CENTER);
}
}
You also don't need to set Swing Components other than the JFrame itself visible.
You can then call setText() in an action listener or such and the TextArea should stay in the same position.
I am a beginner into Java and OOPS in general. Am studyin Head First Java to start, and studying GUI and Swing concepts in it.
The below code is just for understanding purposes.
On running the code, The frame window is displayed with Button, and when I expand it I can see Radio Button too.
Issues-
Button works till the window size is not more than the button size . As soon as I increase the window size even slightly more than button's dimensions, then the button is displayed only when cursor is on it.
I am changing window size using mouse.
Also even if I set Frame size to be more than button. say frame.setSize(800,800); then the button covers whole contentPane. and still behaves same way on resizing.
And the button responds to clicking on mouse, irrespective of where I click in the contentPane. It should respond only when i click directly on the button.
Please inform me why it is behaving this way.
And if possible,corrections in code or additions to correct this.
import java.awt.Color;
import javax.swing.*;
import java.awt.event.*;
public class Test1 implements ActionListener {
JFrame frame = new JFrame("Frame");
JButton button = new JButton("Button!");
JRadioButton radio = new JRadioButton("VideoKilledTheRadioStar!",true);
int j=0;
public static void main(String[] args) {
Test1 t = new Test1();
t.method1();
}
public void method1()
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.setSize(100,100);
button.setBackground(Color.ORANGE);
frame.add(button);
frame.setSize(100,100);
frame.setVisible(true);
button.addActionListener(this);
frame.getContentPane().add(radio);
radio.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{j++;
button.setText("clicked .. " + j);
if(button.getBackground()==Color.ORANGE)
button.setBackground(Color.BLUE);
else
button.setBackground(Color.ORANGE);
}
}
P.S I did not know which segment of code is important or more relevant to this question, so I have put complete code.
You are trying to add the JButton button and JRadioButton objects in the default layout(BorderLayout) of the JFrame.
Whenevery you add a component to JFrame having BorderLayout the components goes in the Middle Section and BorderLayout center section has tendency to occupy the complete space, so to position elements properly you will need to specify the location as well as set the PreferredSize of the component.
frame.add(radio, BorderLayout.SOUTH);
component.setPreferredSize(Dimension);
You are adding the JButton button and the JRadioButton both in the BorderLayout.CENTER location so only one is being displayed. Components at this location will be sized in the X and Y axis.
The JButton only displays when the cursor is over it due to the fact that it has its own MouseListener used for painting.
Also, the statements
frame.add(myComponent);
and
frame.getContentPane().add(myComponent);
both add the component to the frame's ContentPane & are equivalent but the first is chosen for convenience.
Note that components cannot co-exist in the same position in a BorderLayout. You could place the button at the BorderLayout.SOUTH position (& add directly to the frame):
frame.add(radio, BorderLayout.SOUTH);
BorderLayout disregards any preferred sizes for components so you would have to use a different layout manager such as BoxLayout to maintain a fixed size JButton.
See more about Layout Managers
I'm trying to close a frame yet open a new frame.
My application has page A, a JPanel with some controls and a specific button, and when the user clicks the button, I want page A to disappear and page B to appear (page B has controls that depend on the choices that are made by the user on page A).
This has been asked before, but there was no satisfactory answer. Inside the ActionListener implementation, namely public void ActionPerformed(ActionEvent e) from my jpanelForPageA class, I can comfortably write this.setVisible(false), but how can I set page B to a visible state?
You can do the removal of panel a and then the addition of panel b trick. Another is to use a CardLayout.
When you create your panels, you add them to a containing JPanel that you initialize with a CardLayout:
JPanel container = new JPanel(new CardLayout());
containter.add(getPanelA(), "PANEL_A");
containter.add(getPanelB(), "PANEL_B");
Then, in your actionPerformed, when you want to show panelB, you do this:
CardLayout cl = (CardLayout) container.getLayout();
cl.show("PANEL_B");
Take a look at this tutorial for some more ideas.
For some reason, I can never to get setVisible() to work for me to do what you're describing. Instead, I do this:
frame.remove(panelA);
frame.add(panelB);
"frame" is just the JFrame you want to put the panels in. Try this if the setVisible() method doesn't work :)
To your original question, all you have to do is (like aioobe said):
panelB.setVisible(true);
((btw, posting some of your code would help me figure out what you're trying to ask))
And this is just a guess as to what you're trying to do -- I'm guessing your JPanels are in different classes. Then, you'll need to do this:
class pages extends JFrame implements ActionListener
{
public pages()
{
panelA a = new panelA(this)
}
changeToA(panelB b)
{
remove(panelB);
add(new panelA(this));
}
changeToB(panelA a)
{
remove(panelA);
add(new panelB(this));
}
}
class panelA extends JPanel implements ActionListener
{
pages p;
public panelA(pages p)
{
this.p = p
}
// all that actionlistener code stuff
p.changeToB(this);
}
class panelB extends JPanel implements ActionListener
{
pages p;
public panelB(pages p)
{
this.p = p
}
// all that actionlistener code stuff
p.changeToA(this);
}
You pass the pages class to the panels so the panels can tell the pages class to remove themselves.
((I don't know if there is an easier way, but this is what I do all the time))
I hope I helped :)
You have to remove Panel A from the frame, add Panel B to the frame, and call invalidate on the frame (or containing panel). At least in Swing, I'm not sure about AWT, there you might need repaint or revalidate instead of invalidate.
You could also just create a whole new JFrame and dispose the one containing panel A.
My program have 3 classes. 1) main, 2) frame, 3) drawingBoard. The logic of my program is that, a new drawing will be displayed every times user click on New pattern button (and this working fine).
1st class - main method
public class mainPage {
public static void main(String[]args){
JFrame appFrame = new Frame();
appFrame.setVisible(true);
appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);*/
}
}
2nd class - describe the layout (I use Grid Bag Layout)
public class Frame extends JFrame implements ActionListener {
public Frame (){
GridBagLayout m = new GridBagLayout();
Container c = (Container)getContentPane();
c.setLayout (m);
GridBagConstraints con;
JButton bPattern = new JButton("New Pattern");
....
bPattern.addActionListener(this);
JPanel pDraw = new JPanel();
.....
pDraw.add(new drawingBoard()); //drawing will be placed in this panel
}
public void actionPerformed(ActionEvent e) {
repaint();
}
}
3rd class - run drawing functions e.g. paintComponent (), etc.
public class drawingBoard extends JPanel {
public drawingBoard(){}
public void paintComponent(Graphic g){}
....
}
The problem is that, when I look on the console, it seems that even though the user did not click on the button, the program call the class 'drawingBoard' and repaint. The paint component is in the 3rd class (drawingBoard). Although this seem not to give me a problem (e.g. no drawing displayed on the panel unless the user click the button), I am just curious how this happened. is that because I wrote this code at FRAME class (). My intention to write this code is to make sure the drawing should be place in this specific panel (I have 3 panels) but not to call the 3rd class unless the button has been clicked.
JPanel pDraw = new JPanel();
pDraw.add(new drawingBoard()); //place drawing here
The repaint method (and subsequently, the paintComponent method) is not only called by the JFrame but also by Swing itself as well, when there needs to be a repaint of the contents of the JPanel.
The Painting in AWT and Swing article is a good place to start to get information on how painting works.
In this case, the repaint method is being called by events which the article calls System-triggered Painting:
In a system-triggered painting
operation, the system requests a
component to render its contents,
usually for one of the following
reasons:
The component is first made visible on the screen.
The component is resized.
The component has damage that needs to be repaired. (For example,
something that previously obscured the
component has moved, and a previously
obscured portion of the component has
become exposed).