Substitute the currently displayed JPanel with another JPanel within a JFrame - java

I have three Java classes:
a JFrame: HomeView,
a JPanel: TopicListView,
another JPanel: ReplyListView.
In HomeView, I have a menu item that I can click to display TopicListView. In TopicListView, I want to have a button that I can click to display ReplyListView. When the button is clicked, it will invoke the openReplyListView() method. The method will create a new JPanel and replace the current JPanel with it. However, the code in openReplyListView() do not work.
Note: I am not using CardLayout.
Any help would be appreciated on how to get this working.
Thanks in advance.
HomeView class:
public class HomeView extends JFrame {
private JPanel contentPane;
private JMenuBar menuBar;
private JMenu mnForum;
private JMenuItem mntmReply;
private JMenuItem mntmTopic;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
HomeView frame = new HomeView();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public HomeView() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1280, 720);
setJMenuBar(getMenuBar_1());
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}
private JMenuBar getMenuBar_1() {
if (menuBar == null) {
menuBar = new JMenuBar();
menuBar.add(getMnForum());
}
return menuBar;
}
private JMenu getMnForum() {
if (mnForum == null) {
mnForum = new JMenu("Forum");
mnForum.add(getMntmTopic());
mnForum.add(getMntmReply());
}
return mnForum;
}
private JMenuItem getMntmReply() {
if (mntmReply == null) {
mntmReply = new JMenuItem("Reply");
mntmReply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JPanel panel = new ReplyView();
getContentPane().removeAll();
getContentPane().add(panel);
getContentPane().validate();
getContentPane().repaint();
}
});
}
return mntmReply;
}
private JMenuItem getMntmTopic() {
if (mntmTopic == null) {
mntmTopic = new JMenuItem("Topic");
mntmTopic.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JPanel panel = new TopicListView();
getContentPane().removeAll();
getContentPane().add(panel);
getContentPane().validate();
getContentPane().repaint();
}
});
}
return mntmTopic;
}
}
TopicListView class:
public class TopicListView extends JPanel {
private JTable table;
private JScrollPane scrollPane;
private JLabel lblTopics;
/**
* Create the panel.
*/
public TopicListView() {
setLayout(null);
add(getTable());
add(getScrollPane());
add(getLblTopics());
}
**Code snippet for the table and the scrollpane:**
private void openReplyListView(){
JPanel panel = new ReplyListView();
getContentPane().removeAll();
getContentPane().add(panel);
getContentPane().validate();
getContentPane().repaint();
}
ReplyListView class (In general it's the same as TopicListView)
public class ReplyListView extends JPanel {
private JTable table;
private JScrollPane scrollPane;
private JLabel lblTest;
/**
* Create the panel.
*/
public ReplyListView() {
setLayout(null);
add(getTable());
add(getScrollPane());
add(getLblTest());
}
private JTable getTable() {
if (table == null) {
table = new JTable();
table.setBounds(414, 114, 464, 354);
}
return table;
}
private JScrollPane getScrollPane() {
if (scrollPane == null) {
scrollPane = new JScrollPane(getTable());
scrollPane.setBounds(414, 114, 464, 349);
}
return scrollPane;
}
private JLabel getLblTest() {
if (lblTest == null) {
lblTest = new JLabel("TEST");
lblTest.setFont(new Font("Tahoma", Font.PLAIN, 30));
lblTest.setBounds(593, 35, 81, 68);
}
return lblTest;
}
}

Your TopicListView have no LayoutManger (i.e. setLayout(null) in constructor).
It's usually a bad idea. (I'm pretty sure that's the cause of your problem)
Try something like this
private void openReplyListView(){
JPanel panel = new ReplyListView();
removeAll();
setLayout(new BorderLayout());
add(panel, BorderLayout.CENTER);
validate();
repaint();
}

Related

java communication between 2 JFrames

I'm getting an AWT-EventQueue-0 Null Pointer Exception in the below code and I can't get to fix it.
I want to have a main frame, from which by pressing a button
I open a second frame, where I have the option to create new players,
which would show up in the main frame.
I passed the references to the constructors, but I still keep getting the error.
I would be very happy for some help, thanks!
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new App();
}
});
}
}
public class App extends JFrame {
private MainPanel mainPanel;
private SecondPanel secondPanel;
public App() {
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
secondPanel = new SecondPanel(mainPanel);
mainPanel = new MainPanel(secondPanel);
add(mainPanel);
}
}
public class MainPanel extends JPanel {
private JTextArea textArea;
private JScrollPane scrollPane;
private JButton options;
public MainPanel(SecondPanel secondPanel) {
textArea = new JTextArea();
textArea.setColumns(20);
textArea.setRows(5);
textArea.setSize(300, 300);
textArea.setVisible(true);
textArea.setEditable(false);
scrollPane = new JScrollPane(textArea);
scrollPane.setSize(new Dimension(400, 400));
options = new JButton("Options");
options.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
secondPanel.setVisible(true);
}
});
add(scrollPane);
add(options);
}
public JTextArea getTextArea() {
return textArea;
}
public void setTextArea(JTextArea textArea) {
this.textArea = textArea;
}
}
public class SecondPanel extends JFrame {
private JButton create, remove;
private JPanel panel;
public SecondPanel(MainPanel mainPanel) {
setVisible(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
panel = new JPanel();
panel.setLayout(new BorderLayout());
create = new JButton("create");
create.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
mainPanel.getTextArea().append("New Player Created");
}
});
remove = new JButton("remove");
remove.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
mp.getTextArea().setText("Player removed");
}
});
add(panel);
panel.add(create, BorderLayout.EAST);
panel.add(remove, BorderLayout.WEST);
}
}
Well, it has to be "null", because you don't initialise your MainPanel before you hand it over to the secondPanel.
Instead, make an own method for setting the MainPanel on the secondPanel and to set the secondPanel on the MainPanel.
I see that you need the secondPanel for instance in your constructor to set an ActionListener. Do that in your "setSecondPanel(SecondPanel sPanel)"-Method which, as I mentioned before, you should create.

JScrollpane on JPanel doesn't work

I am trying to add jscrollpane on my jframe through the jpanel but it doesn't work. The problem is that if i dont add a scroll bar and the string i want to display on the frame is too long, it won't be displayed and will be cut off.However, when i add the scrollpane the jframe doesn't display the string at all.To "draw" the string on the window, i am using setText(). This is my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.net.*;
import javax.swing.border.LineBorder;
public class LabelFrame extends JFrame {
private final JTextField urlString;
private final JButton loadButton;
String content;
JTextArea textArea = new JTextArea();
public LabelFrame() {
super("WebStalker");
setSize(600, 600);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setLayout(new FlowLayout());
urlString = new JTextField("https:Search",30);
loadButton = new JButton("Load");
JPanel panel = new JPanel();
JLabel label = new JLabel("URL");
panel.add(label);
panel.add(urlString);
panel.add(loadButton);
JScrollPane jp = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel.add(jp);
this.add(panel);
pack(); // JFrame στο ελάχιστο μέγεθος με όλα τα περιεχόμενα
setLocationRelativeTo(null); //τοποθετεί στο κέντρο το παράθυρο
TextFieldHandler tHandler = new TextFieldHandler();
ButtonHandler bHandler = new ButtonHandler();
urlString.addActionListener(tHandler);
loadButton.addActionListener(bHandler);
urlString.addMouseListener(new MouseAdapter(){
#Override
public void mouseClicked(MouseEvent e){
urlString.setText("");
}
});
}
private class TextFieldHandler implements ActionListener {
#Override
public void actionPerformed(ActionEvent event){
try {
content = URLReaderFinal.Reading(event.getActionCommand());
textArea.setText(content);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
textArea.setOpaque(false);
textArea.setEditable(false);
textArea.setFocusable(false);
textArea.setBackgroung(UIManager.getColor("Label.background");
textArea.setFont(UIManager.getFont("Label.font"));
textArea.setBorder(UIManager.getBorder("Label.border"));
getContentPane().add(textArea, BorderLayout.CENTER);
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, "This url doesnt exist","Error", JOptionPane.ERROR_MESSAGE);
}
}
}
private class ButtonHandler implements ActionListener {
#Override
public void actionPerformed(ActionEvent event) {
try {
content = URLReaderFinal.Reading(urlString.getText());
textArea.setText(content);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
textArea.setOpaque(false);
textArea.setEditable(false);
textArea.setFocusable(false);
textArea.setBackground(UIManager.getColor("Label.background"));
textArea.setFont(UIManager.getFont("Label.font"));
textArea.setBorder(UIManager.getBorder("Label.border"));
getContentPane().add(textArea, BorderLayout.CENTER);
} catch (Exception e) {
System.out.println("Unable to load page");
JOptionPane.showMessageDialog(null, "Unable to load page","Error", JOptionPane.ERROR_MESSAGE);
}
}
}
}
In your button handlers you are re-adding a JTextArea to the GUI without its JScrollPane:
getContentPane().add(textArea, BorderLayout.CENTER);
which is essentially removing it from the JScrollPane just when you need it inside of it -- don't do that as this will totally mess you up and will remove the JScrollPane from view. Instead leave the JTextArea where it is, don't try to re-add components to your JFrame's contentPane, and simply add text to the JTextArea as needed.
Also, don't forget to give your JTextArea column and row properties, something that can easily be done by using the constructor that takes two ints. And leave the contentPane's layout as BorderLayout:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.net.*;
import javax.swing.border.LineBorder;
#SuppressWarnings("serial")
public class LabelFrame extends JFrame {
private static final int ROWS = 30;
private static final int COLS = 80;
private final JTextField urlString;
private final JButton loadButton;
String content;
JTextArea textArea = new JTextArea(ROWS, COLS);
public LabelFrame() {
super("WebStalker");
// setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setLayout(new FlowLayout()); // !! no
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
textArea.setOpaque(false);
textArea.setEditable(false);
textArea.setFocusable(false);
textArea.setBackground(UIManager.getColor("Label.background"));
textArea.setFont(UIManager.getFont("Label.font"));
textArea.setBorder(UIManager.getBorder("Label.border"));
urlString = new JTextField("https:Search", 30);
loadButton = new JButton("Load");
JPanel panel = new JPanel();
JLabel label = new JLabel("URL");
panel.add(label);
panel.add(urlString);
panel.add(loadButton);
JScrollPane jp = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// panel.add(jp);
this.add(panel, BorderLayout.PAGE_START);
add(jp);
pack(); // JFrame στο ελάχιστο μέγεθος με όλα τα περιεχόμενα
setLocationRelativeTo(null); // τοποθετεί στο κέντρο το παράθυρο
TextFieldHandler tHandler = new TextFieldHandler();
ButtonHandler bHandler = new ButtonHandler();
urlString.addActionListener(tHandler);
loadButton.addActionListener(bHandler);
urlString.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
urlString.setText("");
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new LabelFrame().setVisible(true);
});
}
private class TextFieldHandler implements ActionListener {
#Override
public void actionPerformed(ActionEvent event) {
try {
content = URLReaderFinal.Reading(event.getActionCommand());
textArea.setText(content);
// !! getContentPane().add(textArea, BorderLayout.CENTER);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "This url doesnt exist", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
private class ButtonHandler implements ActionListener {
#Override
public void actionPerformed(ActionEvent event) {
try {
content = URLReaderFinal.Reading(urlString.getText());
textArea.setText(content);
// getContentPane().add(textArea, BorderLayout.CENTER);
} catch (Exception e) {
System.out.println("Unable to load page");
JOptionPane.showMessageDialog(null, "Unable to load page", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
}
edit: don't use a MouseListener on a JTextField. Perhaps you want to use a FocusListener instead.
Instead do something like:
public class LabelFrame extends JFrame {
private static final int ROWS = 30;
private static final int COLS = 80;
private static final String HTTPS_SEARCH = "https:Search";
// .....
public LabelFrame() {
// ....
urlString = new JTextField(HTTPS_SEARCH, 30);
//.....
urlString.addFocusListener(new FocusAdapter() {
#Override
public void focusGained(FocusEvent e) {
JTextField textField = (JTextField) e.getComponent();
String text = textField.getText();
if (text.equals(HTTPS_SEARCH)) {
textField.setText("");
}
}
});

Using the cut() method in java

I would like to implement the cut method in java to provide the cut and paste functionality.I have already used StringSelection and getSystemClipboard() to provide the cut functionality(code below),but I wanna know how I can use java's inbuilt cut() method for it.
Code for cut functionality.
String t = qu.getText();
StringSelection s = new StringSelection(t);
this.getToolkit().getSystemClipboard().setContents(s, s);
qu.setText("");
I expected something like this to work but it didn't
qu.cut();
Thanks in advance.
If Swing just use the Actions available from the DefaultEditorKit:
new DefaultEditorKit.CutAction()
For example, a small program that uses default actions in a menu, a component-specific pop-up menu, and in buttons:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.text.*;
public class TestActions {
private String[] texts = {
"Hello", "Goodbye", "What the f***?", "Heck if I know", "Peace out man!"
};
private JTextArea textArea = new JTextArea(10, 30);
private Action[] textActions = { new DefaultEditorKit.CutAction(),
new DefaultEditorKit.CopyAction(), new DefaultEditorKit.PasteAction(), };
private JPanel mainPanel = new JPanel();
private JMenuBar menubar = new JMenuBar();
private JPopupMenu popup = new JPopupMenu();
private PopupListener popupListener = new PopupListener();
public TestActions() {
JPanel btnPanel = new JPanel(new GridLayout(1, 0, 5, 5));
JMenu menu = new JMenu("Edit");
for (Action textAction : textActions) {
btnPanel.add(new JButton(textAction));
menu.add(new JMenuItem(textAction));
popup.add(new JMenuItem(textAction));
}
menubar.add(menu);
JPanel textFieldPanel = new JPanel(new GridLayout(0, 1, 5, 5));
for (String text: texts) {
JTextField textField = new JTextField(text, 15);
textField.addMouseListener(popupListener);
textFieldPanel.add(textField);
textField.addFocusListener(new FocusAdapter() {
#Override
public void focusGained(FocusEvent e) {
((JTextComponent)e.getSource()).selectAll();
}
});
}
textArea.addMouseListener(popupListener);
JScrollPane scrollPane = new JScrollPane(textArea);
JPanel textFieldPanelWrapper = new JPanel(new BorderLayout());
textFieldPanelWrapper.add(textFieldPanel, BorderLayout.NORTH);
mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
mainPanel.setLayout(new BorderLayout(5, 5));
mainPanel.add(btnPanel, BorderLayout.NORTH);
mainPanel.add(scrollPane, BorderLayout.CENTER);
mainPanel.add(textFieldPanelWrapper, BorderLayout.EAST);
}
public JComponent getMainPanel() {
return mainPanel;
}
private JMenuBar getMenuBar() {
return menubar;
}
private class PopupListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(e.getComponent(),
e.getX(), e.getY());
}
}
}
private static void createAndShowGui() {
TestActions testActions = new TestActions();
JFrame frame = new JFrame("Test Actions");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(testActions.getMainPanel());
frame.setJMenuBar(testActions.getMenuBar());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
It is because cut() method requires a selection in the text field. Try the following code:
qu.selectAll();
qu.cut();

Can't get CardLayout/button ActionListeners to Work

I know it's something to do with how I've set it up and the actionlistener not being correctly set to the frame or something but I just can't get my hear around it. If someone could point me in the right direction I'd be much obliged. Sorry for noob question.
Here's what I have:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main implements ActionListener {
JPanel cardHolder;
public static final String HOME_CARD = "Home";
public static final String BLUE_PANEL = "Blue Panel";
public static final String RED_PANEL = "Red Panel";
public static final String ORANGE_PANEL = "Orange Panel";
public static JButton home = new JButton("Home");
public static JButton bluePanel = new JButton("Blue Card");
public static JButton redPanel = new JButton("Red Panel");
public static JButton orangePanel = new JButton("Orange Panel");
public static JPanel createCardHolderPanel() {
JPanel cardHolder = new JPanel(new CardLayout());
cardHolder.setBorder(BorderFactory.createTitledBorder("Card Holder Panel"));
cardHolder.add(createHomeCard(), HOME_CARD);
cardHolder.add(createBluePanel(), BLUE_PANEL);
cardHolder.add(createRedPanel(), RED_PANEL);
cardHolder.add(createOrangePanel(), ORANGE_PANEL);
return cardHolder;
}
private static JPanel createOrangePanel() {
JPanel orangePanel = new JPanel();
orangePanel.setBackground(Color.orange);
return orangePanel;
}
private static Component createRedPanel() {
JPanel redPanel = new JPanel();
redPanel.setBackground(Color.red);
return redPanel;
}
private static Component createBluePanel() {
JPanel bluePanel = new JPanel();
bluePanel.setBackground(Color.blue);
return bluePanel;
}
private static Component createHomeCard() {
JPanel homePanel = new JPanel();
homePanel.setBackground(Color.GRAY);
return homePanel;
}
public static JPanel createButtonPanel() {
JPanel buttonPanel = new JPanel(new GridLayout(4, 0, 5, 5));
buttonPanel.setBorder(BorderFactory.createTitledBorder("Button Panel"));
buttonPanel.add(home);
buttonPanel.add(bluePanel);
buttonPanel.add(redPanel);
buttonPanel.add(orangePanel);
return buttonPanel;
}
public static JPanel createContentPane() {
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setBorder(BorderFactory.createTitledBorder("Main Content Pane"));
contentPane.setBackground(Color.WHITE);
contentPane.setPreferredSize(new Dimension(499, 288));
contentPane.add(createButtonPanel(), BorderLayout.WEST);
contentPane.add(createCardHolderPanel(),BorderLayout.CENTER);
return contentPane;
}
public static JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("File");
JMenu users = new JMenu("Users");
JMenu options = new JMenu("Options");
JMenu help = new JMenu("Help");
menuBar.add(file);
menuBar.add(users);
menuBar.add(options);
menuBar.add(help);
return menuBar;
}
public static void createAndShowGUI() {
JFrame frame = new JFrame("Simple CardLayout Program");
frame.setContentPane(createContentPane());
frame.setJMenuBar(createMenuBar());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == home) {
CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
cardLayout.show(cardHolder, HOME_CARD);
}
if (e.getSource() == bluePanel) {
CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
cardLayout.show(cardHolder, BLUE_PANEL);
}
if (e.getSource() == redPanel) {
CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
cardLayout.show(cardHolder, RED_PANEL);
}
if (e.getSource() == orangePanel) {
CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
cardLayout.show(cardHolder, ORANGE_PANEL);
}
}
}
Others have suggested listening to the buttons; in addition:
Prefer the lowest accessibility consistent with use, e.g. private rather than public.
Don't make everything static.
Use static for immutable constants used throughout the class.
Use class variables rather than static members for content.
Don't repeat your self, e.g. initialize cardLayout just once in your actionPerformed)().
Use parameters rather than separate methods for each color, e.g.
private JPanel createColorPanel(Color color) {...}
Revised code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main implements ActionListener {
private static final String HOME_CARD = "Home";
private static final String BLUE_PANEL = "Blue Panel";
private static final String RED_PANEL = "Red Panel";
private static final String ORANGE_PANEL = "Orange Panel";
private JPanel cardHolder;
private JButton homeButton = new JButton("Home");
private JButton blueButton = new JButton("Blue Card");
private JButton redButton = new JButton("Red Panel");
private JButton orangeButton = new JButton("Orange Panel");
public JPanel createCardHolderPanel() {
cardHolder = new JPanel(new CardLayout());
cardHolder.setBorder(BorderFactory.createTitledBorder("Card Holder Panel"));
cardHolder.add(createColorPanel(Color.gray), HOME_CARD);
cardHolder.add(createColorPanel(Color.blue), BLUE_PANEL);
cardHolder.add(createColorPanel(Color.red), RED_PANEL);
cardHolder.add(createColorPanel(Color.orange), ORANGE_PANEL);
return cardHolder;
}
private JPanel createColorPanel(Color color) {
JPanel panel = new JPanel();
panel.setBackground(color);
return panel;
}
public JPanel createButtonPanel() {
JPanel buttonPanel = new JPanel(new GridLayout(4, 0, 5, 5));
buttonPanel.setBorder(BorderFactory.createTitledBorder("Button Panel"));
buttonPanel.add(homeButton);
buttonPanel.add(blueButton);
buttonPanel.add(redButton);
buttonPanel.add(orangeButton);
homeButton.addActionListener(this);
blueButton.addActionListener(this);
redButton.addActionListener(this);
orangeButton.addActionListener(this);
return buttonPanel;
}
public JPanel createContentPane() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createTitledBorder("Main Content Pane"));
panel.setBackground(Color.WHITE);
panel.setPreferredSize(new Dimension(499, 288));
panel.add(createButtonPanel(), BorderLayout.WEST);
panel.add(createCardHolderPanel(), BorderLayout.CENTER);
return panel;
}
public JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("File");
JMenu users = new JMenu("Users");
JMenu options = new JMenu("Options");
JMenu help = new JMenu("Help");
menuBar.add(file);
menuBar.add(users);
menuBar.add(options);
menuBar.add(help);
return menuBar;
}
#Override
public void actionPerformed(ActionEvent e) {
CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
if (e.getSource() == homeButton) {
cardLayout.show(cardHolder, HOME_CARD);
}
if (e.getSource() == blueButton) {
cardLayout.show(cardHolder, BLUE_PANEL);
}
if (e.getSource() == redButton) {
cardLayout.show(cardHolder, RED_PANEL);
}
if (e.getSource() == orangeButton) {
cardLayout.show(cardHolder, ORANGE_PANEL);
}
}
public static void createAndShowGUI() {
JFrame frame = new JFrame("Simple CardLayout Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Main main = new Main();
frame.setJMenuBar(main.createMenuBar());
frame.add(main.createContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
}
You have not set an action listener for any of the buttons. Implementing the actionPerformed method of the interface does not automatically set an action listener for the buttons. You have to call the addActionListener method which in your case would look like the following since your class implements the ActionListener Interface.
public static JButton home = new JButton("Home").addActionListener(this);
public static JButton bluePanel = new JButton("Blue Card").addActionListener(this);
public static JButton redPanel = new JButton("Red Panel").addActionListener(this);
public static JButton orangePanel = new JButton("Orange Panel").addActionListener(this);

JProgressbar width using Grid/FlowLayout

I'm working on a downloader which looks like this at the moment:
The JFrame uses a BorderLayout.
In the NORTH, I have a JPanel(FlowLayout). In the SOUTH there is also a JPanel(FlowLayout), in the WEST I just have a JTextArea (in a JScrollPane). This is all shown correctly. However, in the EAST I currently have a JPanel(GridLayout(10, 1)).
I want to show up to 10 JProgressBars in the EAST section which are added and removed from the panel dynamically. The problem is, I can not get them to look like I want to them to look: I want the JProgressBars' width to fill up the entire EAST section because 1) This gives the app a more symmetrical look and 2) The ProgressBars may contain long strings that don't fit at the moment. I've tried putting the JPanel that contains the GridLayout(10, 1) in a flowlayout and then put that flowlayout in the EAST section, but that didn't work either.
My code (SSCCE) is currently as follows:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
new DownloadFrame();
}
private static class DownloadFrame extends JFrame {
private JButton downloadButton;
private JTextField threadIdTextField;
private JTextArea downloadStatusTextArea;
private JScrollPane scrollPane;
private JTextField downloadLocationTextField;
private JButton downloadLocationButton;
private JPanel North;
private JPanel South;
private JPanel ProgressBarPanel;
private Map<String, JProgressBar> progressBarMap;
public DownloadFrame() {
InitComponents();
InitLayout();
AddComponents();
AddActionListeners();
setVisible(true);
setSize(700, 300);
}
private void InitComponents() {
downloadButton = new JButton("Dowload");
threadIdTextField = new JTextField(6);
downloadStatusTextArea = new JTextArea(10, 30);
scrollPane = new JScrollPane(downloadStatusTextArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
downloadLocationTextField = new JTextField(40);
downloadLocationButton = new JButton("...");
North = new JPanel();
South = new JPanel();
ProgressBarPanel = new JPanel();
progressBarMap = new HashMap<String, JProgressBar>();
}
private void InitLayout() {
North.setLayout(new FlowLayout());
South.setLayout(new FlowLayout());
ProgressBarPanel.setLayout(new GridLayout(10, 1));
}
private void AddComponents() {
North.add(threadIdTextField);
North.add(downloadButton);
add(North, BorderLayout.NORTH);
add(ProgressBarPanel, BorderLayout.EAST);
South.add(downloadLocationTextField);
South.add(downloadLocationButton);
add(South, BorderLayout.SOUTH);
add(scrollPane, BorderLayout.WEST);
}
private void AddActionListeners() {
downloadButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addNewProgessBar(threadIdTextField.getText());
}
});
}
public void addNewProgessBar(String threadId) {
JProgressBar progressBar = new JProgressBar();
progressBar.setStringPainted(true);
progressBarMap.put(threadId, progressBar);
drawProgessBars();
}
void drawProgessBars() {
ProgressBarPanel.removeAll();
for (JProgressBar progressBar : progressBarMap.values()) {
ProgressBarPanel.add(progressBar);
}
validate();
repaint();
}
}
}
Thanks in advance.
EDIT
Easiest solution: change
add(ProgressBarPanel, BorderLayout.EAST);
to
add(ProgressBarPanel, BorderLayout.CENTER);
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
new DownloadFrame();
}
private static class DownloadFrame extends JFrame {
private JButton downloadButton;
private JTextField threadIdTextField;
private JTextArea downloadStatusTextArea;
private JScrollPane scrollPane;
private JTextField downloadLocationTextField;
private JButton downloadLocationButton;
private JPanel North;
private JPanel South;
private JPanel ProgressBarPanel;
private Map<String, JProgressBar> progressBarMap;
public DownloadFrame() {
InitComponents();
AddComponents();
AddActionListeners();
pack();
setVisible(true);
//setSize(700, 300);
}
private void InitComponents() {
setLayout(new BorderLayout());
downloadButton = new JButton("Dowload");
threadIdTextField = new JTextField(6);
downloadStatusTextArea = new JTextArea(10, 30);
scrollPane = new JScrollPane(downloadStatusTextArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
downloadLocationTextField = new JTextField(40);
downloadLocationButton = new JButton("...");
North = new JPanel(new FlowLayout());
South = new JPanel(new FlowLayout());
ProgressBarPanel = new JPanel(new GridLayout(0, 1));
ProgressBarPanel.setBorder(new LineBorder(Color.black));
ProgressBarPanel.setPreferredSize(new Dimension(300,20));
progressBarMap = new HashMap<String, JProgressBar>();
}
private void AddComponents() {
North.add(threadIdTextField);
North.add(downloadButton);
add(North, BorderLayout.NORTH);
add(ProgressBarPanel, BorderLayout.EAST);
South.add(downloadLocationTextField);
South.add(downloadLocationButton);
add(South, BorderLayout.SOUTH);
add(scrollPane, BorderLayout.WEST);
}
private void AddActionListeners() {
downloadButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addNewProgessBar(threadIdTextField.getText());
}
});
}
public void addNewProgessBar(String threadId) {
JProgressBar progressBar = new JProgressBar();
progressBar.setStringPainted(true);
progressBarMap.put(threadId, progressBar);
drawProgessBars();
}
void drawProgessBars() {
ProgressBarPanel.removeAll();
for (JProgressBar progressBar : progressBarMap.values()) {
ProgressBarPanel.add(progressBar);
}
validate();
repaint();
}
}
}
well. that possible, but in your example CENTER area occupated some Rectangle, its hard to reduce/remove CENTER area to the zero Size
to the North JPanel (BorderLayout) place another JPanel and put it to the EAST (with LayoutManager would be GridLayout(1,2,10,10)) and put here two JComponents JTextField - threadIdTextField and JButton - downloadButton, there you are needed setPreferredSize 1) for JComponents (correct way) or 2) for whole JPanel (possible way too)
JScrollPane with JTextArea must be placed to the CENTER area
JPanel with JProgressBars place to EAST, but again set same PreferredSize as for JPanel with JTextField and JButton from the NORTH
SOUTH JPanel remains without changes
Please post a compilable runnable small program for the quickest best help, an SSCCE.
Suggestions include using GridLayout(0, 1) (variable number of rows, one column), or display the JProgressBars in a JList that uses a custom renderer that extends JProgressBar.
Edit 1:
I know that Andrew has already posted the accepted answer (and 1+ for an excellent answer), but I just wanted to demonstrate that this can be done readily with a JList, something like so:
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.util.Random;
import javax.swing.*;
public class EastProgressList extends JPanel {
private DefaultListModel myListModel = new DefaultListModel();
private JList myList = new JList(myListModel);
private JTextField downloadUrlField = new JTextField(10);
public EastProgressList() {
JButton downLoadBtn = new JButton("Download");
downLoadBtn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
downloadAction();
}
});
JPanel northPanel = new JPanel();
northPanel.add(new JLabel("File to Download:"));
northPanel.add(downloadUrlField);
northPanel.add(Box.createHorizontalStrut(15));
northPanel.add(downLoadBtn);
myList.setCellRenderer(new ProgressBarCellRenderer());
JScrollPane eastSPane = new JScrollPane(myList);
eastSPane.setPreferredSize(new Dimension(200, 100));
setLayout(new BorderLayout());
add(new JScrollPane(new JTextArea(20, 30)), BorderLayout.CENTER);
add(northPanel, BorderLayout.NORTH);
add(eastSPane, BorderLayout.EAST);
}
private void downloadAction() {
String downloadUrl = downloadUrlField.getText();
final MyData myData = new MyData(downloadUrl);
myListModel.addElement(myData);
myData.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(MyData.VALUE)) {
myList.repaint();
if (myData.getValue() >= 100) {
myListModel.removeElement(myData);
}
}
}
});
}
private class ProgressBarCellRenderer extends JProgressBar implements ListCellRenderer {
protected ProgressBarCellRenderer() {
setBorder(BorderFactory.createLineBorder(Color.blue));
}
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
//setText(value.toString());
MyData myData = (MyData)value;
setValue(myData.getValue());
setString(myData.getText());
setStringPainted(true);
return this;
}
}
private static void createAndShowUI() {
JFrame frame = new JFrame("EastProgressList");
frame.getContentPane().add(new EastProgressList());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
class MyData {
public static final int TIMER_DELAY = 300;
public static final String VALUE = "value";
protected static final int MAX_DELTA_VALUE = 5;
private String text;
private int value = 0;
private Random random = new Random();
private PropertyChangeSupport pcSupport = new PropertyChangeSupport(this);
public MyData(String text) {
this.text = text;
new Timer(TIMER_DELAY, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
int deltaValue = random.nextInt(MAX_DELTA_VALUE);
int newValue = value + deltaValue;
if (newValue >= 100) {
newValue = 100;
((Timer)e.getSource()).stop();
}
setValue(newValue);
}
}).start();
}
public String getText() {
return text;
}
public int getValue() {
return value;
}
public void setValue(int value) {
int oldValue = this.value;
this.value = value;
PropertyChangeEvent pcEvent = new PropertyChangeEvent(this, VALUE, oldValue, value);
pcSupport.firePropertyChange(pcEvent);
}
public void addPropertyChangeListener(PropertyChangeListener pcListener) {
pcSupport.addPropertyChangeListener(pcListener);
}
}
This results in a GUI looking like so:

Categories

Resources