Resize window when adding Swing components - java

I have a JFrame which consists of some Swing components. One button has an ActionListener to add an extra button to the frame (so the user can add more information).
Now I want the window (jframe) to resize whenever a new component is added. Now the components get smaller whenever a new one is added, but the frame stays the same size.
Here is the code of the actionlistener:
addAnswerButtonMA.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
answerFieldsMA.add(new JTextField());
checkBoxesMA.add(new JCheckBox());
multipleanswerPanel.add(answerFieldsMA.get(answerFieldsMA.size() - 1));
multipleanswerPanel.add(checkBoxesMA.get(checkBoxesMA.size() - 1));
multipleanswerPanel.revalidate();
validate();
}
});
Some background (not sure if needed):
I'm making a quiz program, the administrator can add questions to the quiz by using a separate gui. If he wants to add a Multiple-answer question, he can add an extra answer by clicking the addAnswerbuttonMA to make an extra field and checkbox appear. The field represents the answer and the checkbox represents whether the answer is correct or not.

Packing the frame solves it.
addAnswerButtonMA.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
answerFieldsMA.add(new JTextField());
checkBoxesMA.add(new JCheckBox());
multipleanswerPanel.add(answerFieldsMA.get(answerFieldsMA.size() - 1));
multipleanswerPanel.add(checkBoxesMA.get(checkBoxesMA.size() - 1));
multipleanswerPanel.revalidate();
validate();
pack()
}
});

Related

JTextField to not gain focus unless clicked

I have a JTextField that is defined as follows:
JTextField chatTextField = new JTextField();
chatTextField.setRequestFocusEnabled(false);
chatTextField.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
chatTextField.requestFocusInWindow();
}
});
chatTextField.setFocusTraversalKeysEnabled(false);
chatTextField.setVisible(true);
There are some other method calls I have removed for simplicity purposes.
My chatTextField lives in a JPanel that is added to 'theDesktop'.
JFrame frame = new JFrame("Test");
JLayeredPane theDesktop = new JDesktopPane();
frame.getContentPane().add(theDesktop);
frame.setFocusable(true);
I have many other JPanels that are also added to 'theDesktop' that represent other windows (backpack, bank, etc.). I only want chatTextField to gain focus when I click the mouse into it (or when I press enter which I also have wired up via an action).
Sometimes it is getting focus, when closing other windows, and driving me insane. You are unable to close the chat panel or the button panel. Can anyone see anything that is wrong? I am having trouble coming up with SSCCE because my gui stuff is huge and this is a networked game. Any help would be greatly appreciated.
Here is how I solved this. Kinda hacky, but it works I suppose.
Anytime the focus was transferred to the chat text box (i.e., closing a certain window) I switched the focus to my button bar (always visible):
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
MyClient.buttonBarFrame.requestFocusInWindow(); //so chat does not request focus
}
});

could not remove/add jpanel in jframe

I'm stuck with an issue that is, I have a JFrame with 2 JPanels added in it as showed in Figure :
in figure above, one JPanel have some JButtons and second JPanel have some form fields, I want to change/(remove old and add new JPanel) when I click on JButtons in first JPanel accordingly as shown bellow :
I have code snippet :
myPanel.clickListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
MainFrame.this.getContentPane().remove(((BorderLayout)getLayout()).getLayoutComponent(BorderLayout.CENTER));
MainFrame.this.getContentPane().add(twoPanel, BorderLayout.CENTER);
MainFrame.this.invalidate();
MainFrame.this.validate();
}
});
myPanel.clickListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
MainFrame.this.getContentPane().remove(((BorderLayout)getLayout()).getLayoutComponent(BorderLayout.CENTER));
MainFrame.this.getContentPane().add(customerPanel, BorderLayout.CENTER);
MainFrame.this.invalidate();
MainFrame.this.validate();
}
});
MainFrame.this.setMaximumSize(new Dimension(600, 550));
MainFrame.this.setMinimumSize(new Dimension(599, 549));
MainFrame.this.setSize(600, 550);
MainFrame.this.setResizable(false);
MainFrame.this.setVisible(true);
}
});
through above code I'm able to add new JPanel but unable to remove first JPanel.
in my opinion you should use CardLayout.
It allows you to change visibility of JPanel, so that is actually what you want to do.
You define two JPanels for the right side and then in listner just toggle them.
Look here for the example:
https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html

Using JButtons as Tabs

Could I use JButtons as tabs? Since the JTabbedPane cannot hold the same component in multiple tabs, would there be a way for a JButton to be a tab? I know it looks like tiDE(Website) uses the JButtons as a tab. How would I do that?
I could make something like this
JButton newTab = new JButton("New Tab");
newTab.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JButton tab = new JButton("Tab 1");
JToolBar.add(tab)
tab.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
makeTextAreaTab();
}
}
);
}
}
);
But, how would I make the method makeTextAreaTab()? It would have to be the same component as my other editor(JTextArea), and have the same functionality as a JTabbedPane.
You state in a comment:
I would like to be able to have buttons serve the functionality of tabs. Click on one button, it moves to one editor. Click on another button, it moves to another editor.
Consider using a CardLayout for this where your JButtons (or perhaps better, a JComboBox) tells the CardLayout-using container which "card" (which component -- here a JScrollPane/JTextArea combination) to display.

How action a button to open a new pane in java for a GUI

I am creating a GUI in which my home page has a button labelled "Welcome to the Panel"
The point is that when you press on this button, it will navigate to a new page where I will have other functions. My only problem is that I dont know the syntax or how that when clicking a button, it will navigate to new page.
JButton btn = new JButton("Welcome to the Panel");
btn.setActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
// Here you open the other window. You can use JFrame, JOptionPane or JDialog
}
});
button.addActionListener(new ActionListner()
{
public void actionPerformed(ActionEvent ae)
{
//code to show pane
}
});
You need to register an ActionListener on your button and inside that action listener you make that panel (the page) visible.
How you do that depends on your layout, i.e. with a CardLayout you'd show the corresponding card (here's the doc). Using other layouts you might have to replace a component, e.g. if you use a BorderLayout and your content is placed in the center, replace the center component with the panel you want to show.
Note that if you're not familiar with layout managers yet, you should first have a look at those before doing dynamic changes to the ui (like navigation etc.).

Java Refreshing a screen

I have a screen in which one of its components is made invisible depending on a boolean value. If the boolean changes after the screen has been created, how do I refresh the screen to take this into account?
I think revalidate() is more appropriate here if you are dealing with JComponents.
From the JavaDoc:
Supports deferred automatic layout.
Calls invalidate and then adds this component's validateRoot to a list of components that need to be validated. Validation will occur after all currently pending events have been dispatched. In other words after this method is called, the first validateRoot (if any) found when walking up the containment hierarchy of this component will be validated. By default, JRootPane, JScrollPane, and JTextField return true from isValidateRoot.
This method will automatically be called on this component when a property value changes such that size, location, or internal layout of this component has been affected. This automatic updating differs from the AWT because programs generally no longer need to invoke validate to get the contents of the GUI to update.
Call the validate() method on the container that needs to be laid out -- probably your window's content pane.
Try calling repaint() which in turn will call paintComponent().
I thought that (with Java 6?) you need not do anything... This should happen automatically - no?
With the following example, it does happen automatically...
public class TT extends JFrame
{
public TT()
{
setLayout(new FlowLayout());
JLabel label = new JLabel();
label.setText("Label:");
add(label);
final JTextField textField = new JTextField();
add(textField);
JButton button = new JButton();
button.setText("Button");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if (textField.isVisible())
{
textField.setVisible(false);
}
else
{
textField.setVisible(true);
}
}
});
add(button);
setSize(100,100);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
TT frame = new TT();
frame.setDefaultCloseOperation(TT.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
[Add] And using a layout manager like GridBagLayout would also solve the problem of "Re-Laying out" the page.

Categories